]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_traits/src/normalize_erasing_regions.rs
Rollup merge of #81112 - m-ou-se:alloc-std-ops-reexport, r=KodrAus
[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::subst::GenericArg;
5 use rustc_middle::ty::{self, ParamEnvAnd, TyCtxt, TypeFoldable};
6 use rustc_trait_selection::traits::query::normalize::AtExt;
7 use rustc_trait_selection::traits::{Normalized, ObligationCause};
8 use std::sync::atomic::Ordering;
9
10 crate fn provide(p: &mut Providers) {
11     *p = Providers { normalize_generic_arg_after_erasing_regions, ..*p };
12 }
13
14 fn normalize_generic_arg_after_erasing_regions<'tcx>(
15     tcx: TyCtxt<'tcx>,
16     goal: ParamEnvAnd<'tcx, GenericArg<'tcx>>,
17 ) -> GenericArg<'tcx> {
18     debug!("normalize_generic_arg_after_erasing_regions(goal={:#?})", goal);
19
20     let ParamEnvAnd { param_env, value } = goal;
21     tcx.sess.perf_stats.normalize_generic_arg_after_erasing_regions.fetch_add(1, Ordering::Relaxed);
22     tcx.infer_ctxt().enter(|infcx| {
23         let cause = ObligationCause::dummy();
24         match infcx.at(&cause, param_env).normalize(value) {
25             Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => {
26                 // We don't care about the `obligations`; they are
27                 // always only region relations, and we are about to
28                 // erase those anyway:
29                 debug_assert_eq!(
30                     normalized_obligations.iter().find(|p| not_outlives_predicate(&p.predicate)),
31                     None,
32                 );
33
34                 let resolved_value = infcx.resolve_vars_if_possible(normalized_value);
35                 // It's unclear when `resolve_vars` would have an effect in a
36                 // fresh `InferCtxt`. If this assert does trigger, it will give
37                 // us a test case.
38                 debug_assert_eq!(normalized_value, resolved_value);
39                 let erased = infcx.tcx.erase_regions(resolved_value);
40                 debug_assert!(!erased.needs_infer(), "{:?}", erased);
41                 erased
42             }
43             Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
44         }
45     })
46 }
47
48 fn not_outlives_predicate(p: &ty::Predicate<'tcx>) -> bool {
49     match p.kind().skip_binder() {
50         ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::TypeOutlives(..) => false,
51         ty::PredicateKind::Trait(..)
52         | ty::PredicateKind::Projection(..)
53         | ty::PredicateKind::WellFormed(..)
54         | ty::PredicateKind::ObjectSafe(..)
55         | ty::PredicateKind::ClosureKind(..)
56         | ty::PredicateKind::Subtype(..)
57         | ty::PredicateKind::ConstEvaluatable(..)
58         | ty::PredicateKind::ConstEquate(..)
59         | ty::PredicateKind::TypeWellFormedFromEnv(..) => true,
60     }
61 }