]> git.lizzy.rs Git - rust.git/blob - src/librustc_traits/normalize_erasing_regions.rs
Auto merge of #50097 - glandium:box_free, r=nikomatsakis
[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::{self, ParamEnvAnd, Ty, TyCtxt};
14 use std::sync::atomic::Ordering;
15
16 crate fn normalize_ty_after_erasing_regions<'tcx>(
17     tcx: TyCtxt<'_, 'tcx, 'tcx>,
18     goal: ParamEnvAnd<'tcx, Ty<'tcx>>,
19 ) -> Ty<'tcx> {
20     debug!("normalize_ty_after_erasing_regions(goal={:#?})", goal);
21
22     let ParamEnvAnd { param_env, value } = goal;
23     tcx.sess.perf_stats.normalize_ty_after_erasing_regions.fetch_add(1, Ordering::Relaxed);
24     tcx.infer_ctxt().enter(|infcx| {
25         let cause = ObligationCause::dummy();
26         match infcx.at(&cause, param_env).normalize(&value) {
27             Ok(Normalized {
28                 value: normalized_value,
29                 obligations: normalized_obligations,
30             }) => {
31                 // We don't care about the `obligations`; they are
32                 // always only region relations, and we are about to
33                 // erase those anyway:
34                 debug_assert_eq!(
35                     normalized_obligations
36                         .iter()
37                         .find(|p| not_outlives_predicate(&p.predicate)),
38                     None,
39                 );
40
41                 let normalized_value = infcx.resolve_type_vars_if_possible(&normalized_value);
42                 let normalized_value = infcx.tcx.erase_regions(&normalized_value);
43                 tcx.lift_to_global(&normalized_value).unwrap()
44             }
45             Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
46         }
47     })
48 }
49
50 fn not_outlives_predicate(p: &ty::Predicate<'_>) -> bool {
51     match p {
52         ty::Predicate::RegionOutlives(..) | ty::Predicate::TypeOutlives(..) => false,
53         ty::Predicate::Trait(..)
54         | ty::Predicate::Projection(..)
55         | ty::Predicate::WellFormed(..)
56         | ty::Predicate::ObjectSafe(..)
57         | ty::Predicate::ClosureKind(..)
58         | ty::Predicate::Subtype(..)
59         | ty::Predicate::ConstEvaluatable(..) => true,
60     }
61 }