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