2020-03-29 10:19:48 -05:00
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::traits::query::NoSolution;
|
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::subst::GenericArg;
|
2020-10-06 10:17:29 -05:00
|
|
|
use rustc_middle::ty::{self, ParamEnvAnd, TyCtxt, TypeFoldable};
|
2020-02-11 14:19:40 -06:00
|
|
|
use rustc_trait_selection::traits::query::normalize::AtExt;
|
|
|
|
use rustc_trait_selection::traits::{Normalized, ObligationCause};
|
2018-04-01 01:17:25 -05:00
|
|
|
use std::sync::atomic::Ordering;
|
2018-02-21 10:24:13 -06:00
|
|
|
|
2020-07-05 15:00:14 -05:00
|
|
|
crate fn provide(p: &mut Providers) {
|
2020-03-23 13:22:19 -05:00
|
|
|
*p = Providers { normalize_generic_arg_after_erasing_regions, ..*p };
|
2018-06-27 08:42:00 -05:00
|
|
|
}
|
|
|
|
|
2020-03-23 13:22:19 -05:00
|
|
|
fn normalize_generic_arg_after_erasing_regions<'tcx>(
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-03-23 13:22:19 -05:00
|
|
|
goal: ParamEnvAnd<'tcx, GenericArg<'tcx>>,
|
|
|
|
) -> GenericArg<'tcx> {
|
|
|
|
debug!("normalize_generic_arg_after_erasing_regions(goal={:#?})", goal);
|
2018-04-26 12:31:24 -05:00
|
|
|
|
2018-02-21 10:24:13 -06:00
|
|
|
let ParamEnvAnd { param_env, value } = goal;
|
2020-03-23 13:22:19 -05:00
|
|
|
tcx.sess.perf_stats.normalize_generic_arg_after_erasing_regions.fetch_add(1, Ordering::Relaxed);
|
2018-02-21 10:24:13 -06:00
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
let cause = ObligationCause::dummy();
|
2020-10-23 19:21:18 -05:00
|
|
|
match infcx.at(&cause, param_env).normalize(value) {
|
2019-12-22 16:42:04 -06:00
|
|
|
Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => {
|
2018-03-09 15:44:20 -06:00
|
|
|
// 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 05:26:17 -05:00
|
|
|
normalized_obligations.iter().find(|p| not_outlives_predicate(&p.predicate)),
|
2018-03-09 15:44:20 -06:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
2020-10-06 10:17:29 -05:00
|
|
|
let resolved_value = infcx.resolve_vars_if_possible(normalized_value);
|
|
|
|
// It's unclear when `resolve_vars` would have an effect in a
|
|
|
|
// fresh `InferCtxt`. If this assert does trigger, it will give
|
|
|
|
// us a test case.
|
|
|
|
debug_assert_eq!(normalized_value, resolved_value);
|
|
|
|
let erased = infcx.tcx.erase_regions(resolved_value);
|
|
|
|
debug_assert!(!erased.needs_infer(), "{:?}", erased);
|
|
|
|
erased
|
2018-02-21 10:24:13 -06:00
|
|
|
}
|
|
|
|
Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-03-09 15:44:20 -06:00
|
|
|
|
2020-06-21 05:26:17 -05:00
|
|
|
fn not_outlives_predicate(p: &ty::Predicate<'tcx>) -> bool {
|
2020-07-08 17:35:55 -05: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 10:58:34 -05:00
|
|
|
| ty::PredicateAtom::ConstEquate(..)
|
|
|
|
| ty::PredicateAtom::TypeWellFormedFromEnv(..) => true,
|
2018-03-09 15:44:20 -06:00
|
|
|
}
|
|
|
|
}
|