Repository: rust-lang/rust
File: compiler/rustc_trait_selection/src/traits/wf.rs (Rust)

Code before:
                    let cause = self.cause(traits::WellFormed(None));
                    self.out.push(traits::Obligation::with_depth(
                        cause,
                        self.recursion_depth,
                        param_env,
                        ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into()))
                            .to_predicate(self.tcx()),
                    ));
                }
            }
        }
    }

    #[instrument(level = "debug", skip(self))]
    fn nominal_obligations(
        &mut self,
        def_id: DefId,
        substs: SubstsRef<'tcx>,
    ) -> Vec<traits::PredicateObligation<'tcx>> {
        let predicates = self.tcx.predicates_of(def_id);
        let mut origins = vec![def_id; predicates.predicates.len()];
        let mut head = predicates;
        while let Some(parent) = head.parent {
            head = self.tcx.predicates_of(parent);
            origins.extend(iter::repeat(parent).take(head.predicates.len()));
        }

        let predicates = predicates.instantiate(self.tcx, substs);
        trace!("{:#?}", predicates);
        debug_assert_eq!(predicates.predicates.len(), origins.len());

        iter::zip(iter::zip(predicates.predicates, predicates.spans), origins.into_iter().rev())
            .map(|((pred, span), origin_def_id)| {
                let code = if span.is_dummy() {
                    traits::MiscObligation
                } else {
                    traits::BindingObligation(origin_def_id, span)
                };
                let cause = self.cause(code);
                traits::Obligation::with_depth(cause, self.recursion_depth, self.param_env, pred)
            })
            .filter(|pred| !pred.has_escaping_bound_vars())
            .collect()
    }

    fn from_object_ty(
        &mut self,
        ty: Ty<'tcx>,
        data: &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
        region: ty::Region<'tcx>,
    ) {

Review comment (suggestion):
Rename this function, make it private and create two wrappers around it. One with the original name, passing false, and one with a trailing with_constness `` or similar

Code after:
                //    from `Vec<$0>: WF` to `$0: WF`), so we can
                //    register a pending obligation and keep
                //    moving. (Goal is that an "inductive hypothesis"
                //    is satisfied to ensure termination.)
                // See also the comment on `fn obligations`, describing "livelock"
                // prevention, which happens before this can be reached.
                ty::Infer(_) => {
                    let cause = self.cause(traits::WellFormed(None));
                    self.out.push(traits::Obligation::with_depth(
                        cause,
                        self.recursion_depth,
                        param_env,
                        ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into()))
                            .to_predicate(self.tcx()),
                    ));
                }
            }
        }
    }

    #[instrument(level = "debug", skip(self))]
    fn nominal_obligations_inner(
        &mut self,
        def_id: DefId,
        substs: SubstsRef<'tcx>,
        remap_constness: bool,
    ) -> Vec<traits::PredicateObligation<'tcx>> {
        let predicates = self.tcx.predicates_of(def_id);
        let mut origins = vec![def_id; predicates.predicates.len()];
        let mut head = predicates;
        while let Some(parent) = head.parent {
            head = self.tcx.predicates_of(parent);
            origins.extend(iter::repeat(parent).take(head.predicates.len()));
        }

        let predicates = predicates.instantiate(self.tcx, substs);
        trace!("{:#?}", predicates);
        debug_assert_eq!(predicates.predicates.len(), origins.len());

        iter::zip(iter::zip(predicates.predicates, predicates.spans), origins.into_iter().rev())
            .map(|((mut pred, span), origin_def_id)| {
                let code = if span.is_dummy() {
                    traits::MiscObligation
                } else {
                    traits::BindingObligation(origin_def_id, span)
                };
                let cause = self.cause(code);
                if remap_constness {
                    pred = pred.without_const(self.tcx);
                }
                traits::Obligation::with_depth(cause, self.recursion_depth, self.param_env, pred)