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;
|
2020-10-06 11:17:29 -04:00
|
|
|
use rustc_middle::ty::{self, ParamEnvAnd, TyCtxt, TypeFoldable};
|
2020-02-11 21:19:40 +01:00
|
|
|
use rustc_trait_selection::traits::query::normalize::AtExt;
|
|
|
|
use rustc_trait_selection::traits::{Normalized, ObligationCause};
|
2018-04-01 08:17:25 +02:00
|
|
|
use std::sync::atomic::Ordering;
|
2018-02-21 11:24:13 -05:00
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
crate fn provide(p: &mut Providers) {
|
2021-03-30 14:26:40 +00:00
|
|
|
*p = Providers {
|
|
|
|
normalize_generic_arg_after_erasing_regions: |tcx, goal| {
|
|
|
|
debug!("normalize_generic_arg_after_erasing_regions(goal={:#?})", goal);
|
|
|
|
|
|
|
|
tcx.sess
|
|
|
|
.perf_stats
|
|
|
|
.normalize_generic_arg_after_erasing_regions
|
|
|
|
.fetch_add(1, Ordering::Relaxed);
|
|
|
|
normalize_after_erasing_regions(tcx, goal)
|
|
|
|
},
|
|
|
|
normalize_mir_const_after_erasing_regions: |tcx, goal| {
|
|
|
|
normalize_after_erasing_regions(tcx, goal)
|
|
|
|
},
|
2021-11-26 17:41:22 +01:00
|
|
|
try_normalize_generic_arg_after_erasing_regions: |tcx, goal| {
|
|
|
|
debug!("try_normalize_generic_arg_after_erasing_regions(goal={:#?}", goal);
|
|
|
|
|
|
|
|
try_normalize_after_erasing_regions(tcx, goal)
|
|
|
|
},
|
|
|
|
try_normalize_mir_const_after_erasing_regions: |tcx, goal| {
|
|
|
|
try_normalize_after_erasing_regions(tcx, goal)
|
|
|
|
},
|
2021-03-30 14:26:40 +00:00
|
|
|
..*p
|
|
|
|
};
|
2018-06-27 09:42:00 -04:00
|
|
|
}
|
|
|
|
|
2021-03-30 14:26:40 +00:00
|
|
|
#[instrument(level = "debug", skip(tcx))]
|
|
|
|
fn normalize_after_erasing_regions<'tcx, T: TypeFoldable<'tcx> + PartialEq + Copy>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-03-30 14:26:40 +00:00
|
|
|
goal: ParamEnvAnd<'tcx, T>,
|
|
|
|
) -> T {
|
2018-02-21 11:24:13 -05:00
|
|
|
let ParamEnvAnd { param_env, value } = goal;
|
|
|
|
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 }) => {
|
2018-03-09 16:44:20 -05: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 12:26:17 +02:00
|
|
|
normalized_obligations.iter().find(|p| not_outlives_predicate(&p.predicate)),
|
2018-03-09 16:44:20 -05:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
2020-10-06 11:17:29 -04: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 11:24:13 -05:00
|
|
|
}
|
|
|
|
Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-03-09 16:44:20 -05:00
|
|
|
|
2021-11-26 17:41:22 +01:00
|
|
|
#[instrument(level = "debug", skip(tcx))]
|
|
|
|
fn try_normalize_after_erasing_regions<'tcx, T: TypeFoldable<'tcx> + PartialEq + Copy>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
goal: ParamEnvAnd<'tcx, T>,
|
|
|
|
) -> Result<T, NoSolution> {
|
|
|
|
let ParamEnvAnd { param_env, value } = goal;
|
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
let cause = ObligationCause::dummy();
|
|
|
|
match infcx.at(&cause, param_env).normalize(value) {
|
|
|
|
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!(
|
|
|
|
normalized_obligations.iter().find(|p| not_outlives_predicate(&p.predicate)),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
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);
|
|
|
|
Ok(erased)
|
|
|
|
}
|
|
|
|
Err(NoSolution) => Err(NoSolution),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-21 12:26:17 +02:00
|
|
|
fn not_outlives_predicate(p: &ty::Predicate<'tcx>) -> bool {
|
2021-01-07 11:20:28 -05:00
|
|
|
match p.kind().skip_binder() {
|
|
|
|
ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::TypeOutlives(..) => false,
|
|
|
|
ty::PredicateKind::Trait(..)
|
|
|
|
| ty::PredicateKind::Projection(..)
|
|
|
|
| ty::PredicateKind::WellFormed(..)
|
|
|
|
| ty::PredicateKind::ObjectSafe(..)
|
|
|
|
| ty::PredicateKind::ClosureKind(..)
|
|
|
|
| ty::PredicateKind::Subtype(..)
|
2020-11-21 07:06:16 -05:00
|
|
|
| ty::PredicateKind::Coerce(..)
|
2021-01-07 11:20:28 -05:00
|
|
|
| ty::PredicateKind::ConstEvaluatable(..)
|
|
|
|
| ty::PredicateKind::ConstEquate(..)
|
|
|
|
| ty::PredicateKind::TypeWellFormedFromEnv(..) => true,
|
2018-03-09 16:44:20 -05:00
|
|
|
}
|
|
|
|
}
|