2020-03-29 15:19:48 +00:00
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
2023-05-15 04:24:45 +00:00
|
|
|
use rustc_middle::query::Providers;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::traits::query::NoSolution;
|
2024-11-15 12:53:31 +00:00
|
|
|
use rustc_middle::ty::{self, PseudoCanonicalInput, TyCtxt, TypeFoldable, TypeVisitableExt};
|
2022-11-25 17:11:15 +00:00
|
|
|
use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
|
2020-02-11 20:19:40 +00:00
|
|
|
use rustc_trait_selection::traits::{Normalized, ObligationCause};
|
2024-04-29 06:24:06 +00:00
|
|
|
use tracing::debug;
|
2018-02-21 16:24:13 +00:00
|
|
|
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) fn provide(p: &mut Providers) {
|
2021-03-30 14:26:40 +00:00
|
|
|
*p = Providers {
|
2021-12-10 16:31:40 +00: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
|
|
|
|
2021-11-26 16:41:22 +00:00
|
|
|
try_normalize_after_erasing_regions(tcx, goal)
|
|
|
|
},
|
2021-03-30 14:26:40 +00:00
|
|
|
..*p
|
|
|
|
};
|
2018-06-27 13:42:00 +00: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 16:41:22 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2024-11-15 12:53:31 +00:00
|
|
|
goal: PseudoCanonicalInput<'tcx, T>,
|
2021-11-26 16:41:22 +00:00
|
|
|
) -> Result<T, NoSolution> {
|
2024-11-15 12:53:31 +00:00
|
|
|
let PseudoCanonicalInput { typing_env, value } = goal;
|
|
|
|
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
|
2021-11-26 16:41:22 +00:00
|
|
|
let cause = ObligationCause::dummy();
|
2022-11-25 16:45:33 +00:00
|
|
|
match infcx.at(&cause, param_env).query_normalize(value) {
|
2021-11-26 16:41:22 +00: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 08:06:15 +00: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 06:00:16 +00:00
|
|
|
normalized_obligations.iter().find(|p| not_outlives_predicate(p.predicate)),
|
2021-11-26 16:41:22 +00: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);
|
2023-04-27 07:34:11 +00:00
|
|
|
debug_assert!(!erased.has_infer(), "{erased:?}");
|
2021-11-26 16:41:22 +00:00
|
|
|
Ok(erased)
|
|
|
|
}
|
|
|
|
Err(NoSolution) => Err(NoSolution),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:10:40 +00:00
|
|
|
fn not_outlives_predicate(p: ty::Predicate<'_>) -> bool {
|
2021-01-07 16:20:28 +00:00
|
|
|
match p.kind().skip_binder() {
|
2023-06-16 05:59:42 +00:00
|
|
|
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(..))
|
|
|
|
| ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(..)) => false,
|
|
|
|
ty::PredicateKind::Clause(ty::ClauseKind::Trait(..))
|
|
|
|
| ty::PredicateKind::Clause(ty::ClauseKind::Projection(..))
|
2024-10-20 19:49:11 +00:00
|
|
|
| ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(..))
|
2023-06-16 05:59:42 +00:00
|
|
|
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
|
2023-12-07 16:52:51 +00:00
|
|
|
| ty::PredicateKind::NormalizesTo(..)
|
2023-03-21 22:11:40 +00:00
|
|
|
| ty::PredicateKind::AliasRelate(..)
|
2023-06-16 05:59:42 +00:00
|
|
|
| ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
|
2024-09-25 08:38:40 +00:00
|
|
|
| ty::PredicateKind::DynCompatible(..)
|
2021-01-07 16:20:28 +00:00
|
|
|
| ty::PredicateKind::Subtype(..)
|
2020-11-21 12:06:16 +00:00
|
|
|
| ty::PredicateKind::Coerce(..)
|
2023-06-16 05:59:42 +00:00
|
|
|
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
|
2021-01-07 16:20:28 +00:00
|
|
|
| ty::PredicateKind::ConstEquate(..)
|
2023-07-03 15:27:41 +00:00
|
|
|
| ty::PredicateKind::Ambiguous => true,
|
2018-03-09 21:44:20 +00:00
|
|
|
}
|
|
|
|
}
|