]> git.lizzy.rs Git - rust.git/blob - src/librustc_traits/normalize_erasing_regions.rs
Auto merge of #51485 - estebank:dehighlight-secondary-msgs, r=GuillaumeGomez
[rust.git] / src / librustc_traits / normalize_erasing_regions.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc::traits::{Normalized, ObligationCause};
12 use rustc::traits::query::NoSolution;
13 use rustc::ty::query::Providers;
14 use rustc::ty::{self, ParamEnvAnd, Ty, TyCtxt};
15 use std::sync::atomic::Ordering;
16
17 crate fn provide(p: &mut Providers) {
18     *p = Providers {
19         normalize_ty_after_erasing_regions,
20         ..*p
21     };
22 }
23
24 fn normalize_ty_after_erasing_regions<'tcx>(
25     tcx: TyCtxt<'_, 'tcx, 'tcx>,
26     goal: ParamEnvAnd<'tcx, Ty<'tcx>>,
27 ) -> Ty<'tcx> {
28     debug!("normalize_ty_after_erasing_regions(goal={:#?})", goal);
29
30     let ParamEnvAnd { param_env, value } = goal;
31     tcx.sess.perf_stats.normalize_ty_after_erasing_regions.fetch_add(1, Ordering::Relaxed);
32     tcx.infer_ctxt().enter(|infcx| {
33         let cause = ObligationCause::dummy();
34         match infcx.at(&cause, param_env).normalize(&value) {
35             Ok(Normalized {
36                 value: normalized_value,
37                 obligations: normalized_obligations,
38             }) => {
39                 // We don't care about the `obligations`; they are
40                 // always only region relations, and we are about to
41                 // erase those anyway:
42                 debug_assert_eq!(
43                     normalized_obligations
44                         .iter()
45                         .find(|p| not_outlives_predicate(&p.predicate)),
46                     None,
47                 );
48
49                 let normalized_value = infcx.resolve_type_vars_if_possible(&normalized_value);
50                 let normalized_value = infcx.tcx.erase_regions(&normalized_value);
51                 tcx.lift_to_global(&normalized_value).unwrap()
52             }
53             Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
54         }
55     })
56 }
57
58 fn not_outlives_predicate(p: &ty::Predicate<'_>) -> bool {
59     match p {
60         ty::Predicate::RegionOutlives(..) | ty::Predicate::TypeOutlives(..) => false,
61         ty::Predicate::Trait(..)
62         | ty::Predicate::Projection(..)
63         | ty::Predicate::WellFormed(..)
64         | ty::Predicate::ObjectSafe(..)
65         | ty::Predicate::ClosureKind(..)
66         | ty::Predicate::Subtype(..)
67         | ty::Predicate::ConstEvaluatable(..) => true,
68     }
69 }