1 use rustc_infer::infer::TyCtxtInferExt;
2 use rustc_middle::traits::query::NoSolution;
3 use rustc_middle::ty::query::Providers;
4 use rustc_middle::ty::{self, ParamEnvAnd, TyCtxt, TypeFoldable};
5 use rustc_trait_selection::traits::query::normalize::AtExt;
6 use rustc_trait_selection::traits::{Normalized, ObligationCause};
7 use std::sync::atomic::Ordering;
9 pub(crate) fn provide(p: &mut Providers) {
11 try_normalize_generic_arg_after_erasing_regions: |tcx, goal| {
12 debug!("try_normalize_generic_arg_after_erasing_regions(goal={:#?}", goal);
16 .normalize_generic_arg_after_erasing_regions
17 .fetch_add(1, Ordering::Relaxed);
19 try_normalize_after_erasing_regions(tcx, goal)
25 fn try_normalize_after_erasing_regions<'tcx, T: TypeFoldable<'tcx> + PartialEq + Copy>(
27 goal: ParamEnvAnd<'tcx, T>,
28 ) -> Result<T, NoSolution> {
29 let ParamEnvAnd { param_env, value } = goal;
30 let infcx = tcx.infer_ctxt().build();
31 let cause = ObligationCause::dummy();
32 match infcx.at(&cause, param_env).normalize(value) {
33 Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => {
34 // We don't care about the `obligations`; they are
35 // always only region relations, and we are about to
36 // erase those anyway:
38 normalized_obligations.iter().find(|p| not_outlives_predicate(p.predicate)),
42 let resolved_value = infcx.resolve_vars_if_possible(normalized_value);
43 // It's unclear when `resolve_vars` would have an effect in a
44 // fresh `InferCtxt`. If this assert does trigger, it will give
46 debug_assert_eq!(normalized_value, resolved_value);
47 let erased = infcx.tcx.erase_regions(resolved_value);
48 debug_assert!(!erased.needs_infer(), "{:?}", erased);
51 Err(NoSolution) => Err(NoSolution),
55 fn not_outlives_predicate<'tcx>(p: ty::Predicate<'tcx>) -> bool {
56 match p.kind().skip_binder() {
57 ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::TypeOutlives(..) => false,
58 ty::PredicateKind::Trait(..)
59 | ty::PredicateKind::Projection(..)
60 | ty::PredicateKind::WellFormed(..)
61 | ty::PredicateKind::ObjectSafe(..)
62 | ty::PredicateKind::ClosureKind(..)
63 | ty::PredicateKind::Subtype(..)
64 | ty::PredicateKind::Coerce(..)
65 | ty::PredicateKind::ConstEvaluatable(..)
66 | ty::PredicateKind::ConstEquate(..)
67 | ty::PredicateKind::TypeWellFormedFromEnv(..) => true,