]> git.lizzy.rs Git - rust.git/blob - src/librustc_traits/normalize_erasing_regions.rs
Auto merge of #73316 - Dylan-DPC:rollup-zgouwou, r=Dylan-DPC
[rust.git] / src / librustc_traits / 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};
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 normalized_value = infcx.resolve_vars_if_possible(&normalized_value);
35                 infcx.tcx.erase_regions(&normalized_value)
36             }
37             Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
38         }
39     })
40 }
41
42 fn not_outlives_predicate(p: &ty::Predicate<'_>) -> bool {
43     match p.kind() {
44         ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::TypeOutlives(..) => false,
45         ty::PredicateKind::Trait(..)
46         | ty::PredicateKind::Projection(..)
47         | ty::PredicateKind::WellFormed(..)
48         | ty::PredicateKind::ObjectSafe(..)
49         | ty::PredicateKind::ClosureKind(..)
50         | ty::PredicateKind::Subtype(..)
51         | ty::PredicateKind::ConstEvaluatable(..)
52         | ty::PredicateKind::ConstEquate(..) => true,
53     }
54 }