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