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;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, ParamEnvAnd, TyCtxt, TypeFoldable, TypeVisitableExt};
|
2022-11-25 17:11:15 +00:00
|
|
|
use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
|
2020-02-11 21:19:40 +01:00
|
|
|
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
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn provide(p: &mut Providers) {
|
2021-03-30 14:26:40 +00:00
|
|
|
*p = Providers {
|
2021-12-10 17:31:40 +01:00
|
|
|
try_normalize_generic_arg_after_erasing_regions: |tcx, goal| {
|
|
|
|
debug!("try_normalize_generic_arg_after_erasing_regions(goal={:#?}", goal);
|
2021-03-30 14:26:40 +00:00
|
|
|
|
|
|
|
tcx.sess
|
|
|
|
.perf_stats
|
|
|
|
.normalize_generic_arg_after_erasing_regions
|
|
|
|
.fetch_add(1, Ordering::Relaxed);
|
2021-12-08 18:11:13 +01:00
|
|
|
|
2021-11-26 17:41:22 +01:00
|
|
|
try_normalize_after_erasing_regions(tcx, goal)
|
|
|
|
},
|
2021-03-30 14:26:40 +00:00
|
|
|
..*p
|
|
|
|
};
|
2018-06-27 09:42:00 -04:00
|
|
|
}
|
|
|
|
|
2023-02-22 02:18:40 +00:00
|
|
|
fn try_normalize_after_erasing_regions<'tcx, T: TypeFoldable<TyCtxt<'tcx>> + PartialEq + Copy>(
|
2021-11-26 17:41:22 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
goal: ParamEnvAnd<'tcx, T>,
|
|
|
|
) -> Result<T, NoSolution> {
|
|
|
|
let ParamEnvAnd { param_env, value } = goal;
|
|
|
|
let infcx = tcx.infer_ctxt().build();
|
|
|
|
let cause = ObligationCause::dummy();
|
2022-11-25 16:45:33 +00:00
|
|
|
match infcx.at(&cause, param_env).query_normalize(value) {
|
2021-11-26 17:41:22 +01: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:
|
2022-11-17 09:06:15 +01:00
|
|
|
// This has been seen to fail in RL, so making it a non-debug assertion to better catch
|
|
|
|
// those cases.
|
|
|
|
assert_eq!(
|
2022-01-27 17:00:16 +11:00
|
|
|
normalized_obligations.iter().find(|p| not_outlives_predicate(p.predicate)),
|
2021-11-26 17:41:22 +01:00
|
|
|
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);
|
2022-12-19 10:31:55 +01:00
|
|
|
debug_assert!(!erased.needs_infer(), "{erased:?}");
|
2021-11-26 17:41:22 +01:00
|
|
|
Ok(erased)
|
|
|
|
}
|
|
|
|
Err(NoSolution) => Err(NoSolution),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 22:10:40 +01:00
|
|
|
fn not_outlives_predicate(p: ty::Predicate<'_>) -> bool {
|
2021-01-07 11:20:28 -05:00
|
|
|
match p.kind().skip_binder() {
|
2022-11-24 18:14:58 -03:00
|
|
|
ty::PredicateKind::Clause(ty::Clause::RegionOutlives(..))
|
|
|
|
| ty::PredicateKind::Clause(ty::Clause::TypeOutlives(..)) => false,
|
|
|
|
ty::PredicateKind::Clause(ty::Clause::Trait(..))
|
|
|
|
| ty::PredicateKind::Clause(ty::Clause::Projection(..))
|
2023-02-16 11:55:58 +00:00
|
|
|
| ty::PredicateKind::Clause(ty::Clause::ConstArgHasType(..))
|
2023-03-21 22:11:40 +00:00
|
|
|
| ty::PredicateKind::AliasRelate(..)
|
2021-01-07 11:20:28 -05:00
|
|
|
| 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(..)
|
2022-11-02 15:10:05 +00:00
|
|
|
| ty::PredicateKind::Ambiguous
|
2021-01-07 11:20:28 -05:00
|
|
|
| ty::PredicateKind::TypeWellFormedFromEnv(..) => true,
|
2018-03-09 16:44:20 -05:00
|
|
|
}
|
|
|
|
}
|