rustc_traits/
normalize_erasing_regions.rs

1use 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            // We don't care about the `obligations`; they are
30            // always only region relations, and we are about to
31            // erase those anyway:
32            // This has been seen to fail in RL, so making it a non-debug assertion to better catch
33            // those cases.
34            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            // It's unclear when `resolve_vars` would have an effect in a
41            // fresh `InferCtxt`. If this assert does trigger, it will give
42            // us a test case.
43            debug_assert_eq!(normalized_value, resolved_value);
44            let erased = infcx.tcx.erase_regions(resolved_value);
45            debug_assert!(!erased.has_infer(), "{erased:?}");
46            Ok(erased)
47        }
48        Err(NoSolution) => Err(NoSolution),
49    }
50}
51
52fn not_outlives_predicate(p: ty::Predicate<'_>) -> bool {
53    match p.kind().skip_binder() {
54        ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(..))
55        | ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(..)) => false,
56        ty::PredicateKind::Clause(ty::ClauseKind::Trait(..))
57        | ty::PredicateKind::Clause(ty::ClauseKind::Projection(..))
58        | ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(..))
59        | ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
60        | ty::PredicateKind::NormalizesTo(..)
61        | ty::PredicateKind::AliasRelate(..)
62        | ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
63        | ty::PredicateKind::DynCompatible(..)
64        | ty::PredicateKind::Subtype(..)
65        | ty::PredicateKind::Coerce(..)
66        | ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
67        | ty::PredicateKind::ConstEquate(..)
68        | ty::PredicateKind::Ambiguous => true,
69    }
70}