rustc_traits/
normalize_erasing_regions.rs1use rustc_infer::infer::TyCtxtInferExt;
2use rustc_middle::query::Providers;
3use rustc_middle::traits::query::NoSolution;
4use rustc_middle::ty::{self, PseudoCanonicalInput, TyCtxt, TypeFoldable, TypeVisitableExt};
5use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
6use rustc_trait_selection::traits::{Normalized, ObligationCause};
7use tracing::debug;
8
9pub(crate) fn provide(p: &mut Providers) {
10    *p = Providers {
11        try_normalize_generic_arg_after_erasing_regions: |tcx, goal| {
12            debug!("try_normalize_generic_arg_after_erasing_regions(goal={:#?}", goal);
13
14            try_normalize_after_erasing_regions(tcx, goal)
15        },
16        ..*p
17    };
18}
19
20fn try_normalize_after_erasing_regions<'tcx, T: TypeFoldable<TyCtxt<'tcx>> + PartialEq + Copy>(
21    tcx: TyCtxt<'tcx>,
22    goal: PseudoCanonicalInput<'tcx, T>,
23) -> Result<T, NoSolution> {
24    let PseudoCanonicalInput { typing_env, value } = goal;
25    let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
26    let cause = ObligationCause::dummy();
27    match infcx.at(&cause, param_env).query_normalize(value) {
28        Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => {
29            assert_eq!(
35                normalized_obligations.iter().find(|p| not_outlives_predicate(p.predicate)),
36                None,
37            );
38
39            let resolved_value = infcx.resolve_vars_if_possible(normalized_value);
40            debug_assert_eq!(normalized_value, resolved_value);
44            let erased = infcx.tcx.erase_and_anonymize_regions(resolved_value);
45            if infcx.next_trait_solver() {
46                debug_assert!(!erased.has_infer(), "{erased:?}");
47            } else {
48                if erased.has_infer() {
54                    return Err(NoSolution);
55                }
56            }
57            Ok(erased)
58        }
59        Err(NoSolution) => Err(NoSolution),
60    }
61}
62
63fn not_outlives_predicate(p: ty::Predicate<'_>) -> bool {
64    match p.kind().skip_binder() {
65        ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(..))
66        | ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(..)) => false,
67        ty::PredicateKind::Clause(ty::ClauseKind::Trait(..))
68        | ty::PredicateKind::Clause(ty::ClauseKind::Projection(..))
69        | ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(..))
70        | ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
71        | ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(_))
72        | ty::PredicateKind::NormalizesTo(..)
73        | ty::PredicateKind::AliasRelate(..)
74        | ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
75        | ty::PredicateKind::DynCompatible(..)
76        | ty::PredicateKind::Subtype(..)
77        | ty::PredicateKind::Coerce(..)
78        | ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
79        | ty::PredicateKind::ConstEquate(..)
80        | ty::PredicateKind::Ambiguous => true,
81    }
82}