1
Fork 0
rust/compiler/rustc_traits/src/normalize_erasing_regions.rs

56 lines
2.3 KiB
Rust
Raw Normal View History

2020-03-29 17:19:48 +02:00
use rustc_infer::infer::TyCtxtInferExt;
2020-03-29 16:41:09 +02:00
use rustc_middle::traits::query::NoSolution;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::subst::GenericArg;
use rustc_middle::ty::{self, ParamEnvAnd, TyCtxt};
2020-02-11 21:19:40 +01:00
use rustc_trait_selection::traits::query::normalize::AtExt;
use rustc_trait_selection::traits::{Normalized, ObligationCause};
use std::sync::atomic::Ordering;
crate fn provide(p: &mut Providers) {
*p = Providers { normalize_generic_arg_after_erasing_regions, ..*p };
2018-06-27 09:42:00 -04:00
}
fn normalize_generic_arg_after_erasing_regions<'tcx>(
2019-06-14 00:48:52 +03:00
tcx: TyCtxt<'tcx>,
goal: ParamEnvAnd<'tcx, GenericArg<'tcx>>,
) -> GenericArg<'tcx> {
debug!("normalize_generic_arg_after_erasing_regions(goal={:#?})", goal);
2018-04-26 13:31:24 -04:00
let ParamEnvAnd { param_env, value } = goal;
tcx.sess.perf_stats.normalize_generic_arg_after_erasing_regions.fetch_add(1, Ordering::Relaxed);
tcx.infer_ctxt().enter(|infcx| {
let cause = ObligationCause::dummy();
2020-10-24 02:21:18 +02:00
match infcx.at(&cause, param_env).normalize(value) {
2019-12-22 17:42:04 -05:00
Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => {
// We don't care about the `obligations`; they are
// always only region relations, and we are about to
// erase those anyway:
debug_assert_eq!(
2020-06-21 12:26:17 +02:00
normalized_obligations.iter().find(|p| not_outlives_predicate(&p.predicate)),
None,
);
2020-10-24 02:21:18 +02:00
let normalized_value = infcx.resolve_vars_if_possible(normalized_value);
infcx.tcx.erase_regions(normalized_value)
}
Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
}
})
}
2020-06-21 12:26:17 +02:00
fn not_outlives_predicate(p: &ty::Predicate<'tcx>) -> bool {
2020-07-09 00:35:55 +02:00
match p.skip_binders() {
ty::PredicateAtom::RegionOutlives(..) | ty::PredicateAtom::TypeOutlives(..) => false,
ty::PredicateAtom::Trait(..)
| ty::PredicateAtom::Projection(..)
| ty::PredicateAtom::WellFormed(..)
| ty::PredicateAtom::ObjectSafe(..)
| ty::PredicateAtom::ClosureKind(..)
| ty::PredicateAtom::Subtype(..)
| ty::PredicateAtom::ConstEvaluatable(..)
2020-09-01 17:58:34 +02:00
| ty::PredicateAtom::ConstEquate(..)
| ty::PredicateAtom::TypeWellFormedFromEnv(..) => true,
}
}