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.
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.
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;
16 crate fn normalize_ty_after_erasing_regions<'tcx>(
17 tcx: TyCtxt<'_, 'tcx, 'tcx>,
18 goal: ParamEnvAnd<'tcx, Ty<'tcx>>,
20 debug!("normalize_ty_after_erasing_regions(goal={:#?})", goal);
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) {
28 value: normalized_value,
29 obligations: normalized_obligations,
31 // We don't care about the `obligations`; they are
32 // always only region relations, and we are about to
33 // erase those anyway:
35 normalized_obligations
37 .find(|p| not_outlives_predicate(&p.predicate)),
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()
45 Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
50 fn not_outlives_predicate(p: &ty::Predicate<'_>) -> bool {
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,