]> git.lizzy.rs Git - rust.git/blob - src/librustc_traits/normalize_erasing_regions.rs
Rollup merge of #67507 - Mark-Simulacrum:purge-uninit, r=Centril
[rust.git] / src / librustc_traits / normalize_erasing_regions.rs
1 use rustc::traits::query::NoSolution;
2 use rustc::traits::{Normalized, ObligationCause};
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 { normalize_ty_after_erasing_regions, ..*p };
9 }
10
11 fn normalize_ty_after_erasing_regions<'tcx>(
12     tcx: TyCtxt<'tcx>,
13     goal: ParamEnvAnd<'tcx, Ty<'tcx>>,
14 ) -> Ty<'tcx> {
15     debug!("normalize_ty_after_erasing_regions(goal={:#?})", goal);
16
17     let ParamEnvAnd { param_env, value } = goal;
18     tcx.sess.perf_stats.normalize_ty_after_erasing_regions.fetch_add(1, Ordering::Relaxed);
19     tcx.infer_ctxt().enter(|infcx| {
20         let cause = ObligationCause::dummy();
21         match infcx.at(&cause, param_env).normalize(&value) {
22             Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => {
23                 // We don't care about the `obligations`; they are
24                 // always only region relations, and we are about to
25                 // erase those anyway:
26                 debug_assert_eq!(
27                     normalized_obligations.iter().find(|p| not_outlives_predicate(&p.predicate)),
28                     None,
29                 );
30
31                 let normalized_value = infcx.resolve_vars_if_possible(&normalized_value);
32                 infcx.tcx.erase_regions(&normalized_value)
33             }
34             Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
35         }
36     })
37 }
38
39 fn not_outlives_predicate(p: &ty::Predicate<'_>) -> bool {
40     match p {
41         ty::Predicate::RegionOutlives(..) | ty::Predicate::TypeOutlives(..) => false,
42         ty::Predicate::Trait(..)
43         | ty::Predicate::Projection(..)
44         | ty::Predicate::WellFormed(..)
45         | ty::Predicate::ObjectSafe(..)
46         | ty::Predicate::ClosureKind(..)
47         | ty::Predicate::Subtype(..)
48         | ty::Predicate::ConstEvaluatable(..) => true,
49     }
50 }