]> git.lizzy.rs Git - rust.git/blob - src/librustc_traits/normalize_erasing_regions.rs
Rollup merge of #61333 - varkor:apit-const-param-ice, r=estebank
[rust.git] / src / librustc_traits / normalize_erasing_regions.rs
1 use rustc::traits::{Normalized, ObligationCause};
2 use rustc::traits::query::NoSolution;
3 use rustc::ty::query::Providers;
4 use rustc::ty::{self, ParamEnvAnd, Ty, TyCtxt};
5 use std::sync::atomic::Ordering;
6
7 crate fn provide(p: &mut Providers<'_>) {
8     *p = Providers {
9         normalize_ty_after_erasing_regions,
10         ..*p
11     };
12 }
13
14 fn normalize_ty_after_erasing_regions<'tcx>(
15     tcx: TyCtxt<'_, 'tcx, 'tcx>,
16     goal: ParamEnvAnd<'tcx, Ty<'tcx>>,
17 ) -> Ty<'tcx> {
18     debug!("normalize_ty_after_erasing_regions(goal={:#?})", goal);
19
20     let ParamEnvAnd { param_env, value } = goal;
21     tcx.sess.perf_stats.normalize_ty_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 {
26                 value: normalized_value,
27                 obligations: normalized_obligations,
28             }) => {
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                 debug_assert_eq!(
33                     normalized_obligations
34                         .iter()
35                         .find(|p| not_outlives_predicate(&p.predicate)),
36                     None,
37                 );
38
39                 let normalized_value = infcx.resolve_vars_if_possible(&normalized_value);
40                 let normalized_value = infcx.tcx.erase_regions(&normalized_value);
41                 tcx.lift_to_global(&normalized_value).unwrap()
42             }
43             Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
44         }
45     })
46 }
47
48 fn not_outlives_predicate(p: &ty::Predicate<'_>) -> bool {
49     match p {
50         ty::Predicate::RegionOutlives(..) | ty::Predicate::TypeOutlives(..) => false,
51         ty::Predicate::Trait(..)
52         | ty::Predicate::Projection(..)
53         | ty::Predicate::WellFormed(..)
54         | ty::Predicate::ObjectSafe(..)
55         | ty::Predicate::ClosureKind(..)
56         | ty::Predicate::Subtype(..)
57         | ty::Predicate::ConstEvaluatable(..) => true,
58     }
59 }