]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_traits/src/normalize_erasing_regions.rs
Rollup merge of #107344 - compiler-errors:new-solver-tweaks, r=lcnr
[rust.git] / compiler / rustc_traits / src / normalize_erasing_regions.rs
1 use rustc_infer::infer::TyCtxtInferExt;
2 use rustc_middle::traits::query::NoSolution;
3 use rustc_middle::ty::query::Providers;
4 use rustc_middle::ty::{self, ParamEnvAnd, TyCtxt, TypeFoldable};
5 use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
6 use rustc_trait_selection::traits::{Normalized, ObligationCause};
7 use std::sync::atomic::Ordering;
8
9 pub(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             tcx.sess
15                 .perf_stats
16                 .normalize_generic_arg_after_erasing_regions
17                 .fetch_add(1, Ordering::Relaxed);
18
19             try_normalize_after_erasing_regions(tcx, goal)
20         },
21         ..*p
22     };
23 }
24
25 fn try_normalize_after_erasing_regions<'tcx, T: TypeFoldable<'tcx> + PartialEq + Copy>(
26     tcx: TyCtxt<'tcx>,
27     goal: ParamEnvAnd<'tcx, T>,
28 ) -> Result<T, NoSolution> {
29     let ParamEnvAnd { param_env, value } = goal;
30     let infcx = tcx.infer_ctxt().build();
31     let cause = ObligationCause::dummy();
32     match infcx.at(&cause, param_env).query_normalize(value) {
33         Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => {
34             // We don't care about the `obligations`; they are
35             // always only region relations, and we are about to
36             // erase those anyway:
37             // This has been seen to fail in RL, so making it a non-debug assertion to better catch
38             // those cases.
39             assert_eq!(
40                 normalized_obligations.iter().find(|p| not_outlives_predicate(p.predicate)),
41                 None,
42             );
43
44             let resolved_value = infcx.resolve_vars_if_possible(normalized_value);
45             // It's unclear when `resolve_vars` would have an effect in a
46             // fresh `InferCtxt`. If this assert does trigger, it will give
47             // us a test case.
48             debug_assert_eq!(normalized_value, resolved_value);
49             let erased = infcx.tcx.erase_regions(resolved_value);
50             debug_assert!(!erased.needs_infer(), "{erased:?}");
51             Ok(erased)
52         }
53         Err(NoSolution) => Err(NoSolution),
54     }
55 }
56
57 fn not_outlives_predicate(p: ty::Predicate<'_>) -> bool {
58     match p.kind().skip_binder() {
59         ty::PredicateKind::Clause(ty::Clause::RegionOutlives(..))
60         | ty::PredicateKind::Clause(ty::Clause::TypeOutlives(..)) => false,
61         ty::PredicateKind::Clause(ty::Clause::Trait(..))
62         | ty::PredicateKind::Clause(ty::Clause::Projection(..))
63         | ty::PredicateKind::WellFormed(..)
64         | ty::PredicateKind::ObjectSafe(..)
65         | ty::PredicateKind::ClosureKind(..)
66         | ty::PredicateKind::Subtype(..)
67         | ty::PredicateKind::Coerce(..)
68         | ty::PredicateKind::ConstEvaluatable(..)
69         | ty::PredicateKind::ConstEquate(..)
70         | ty::PredicateKind::Ambiguous
71         | ty::PredicateKind::TypeWellFormedFromEnv(..) => true,
72     }
73 }