normalize_erasing_regions.rs 2.9 KB
Newer Older
1
use rustc_infer::infer::TyCtxtInferExt;
M
Mazdak Farrokhzad 已提交
2 3
use rustc_middle::traits::query::NoSolution;
use rustc_middle::ty::query::Providers;
4
use rustc_middle::ty::{self, ParamEnvAnd, TyCtxt, TypeFoldable};
C
Camille GILLOT 已提交
5 6
use rustc_trait_selection::traits::query::normalize::AtExt;
use rustc_trait_selection::traits::{Normalized, ObligationCause};
7
use std::sync::atomic::Ordering;
8

9
crate fn provide(p: &mut Providers) {
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    *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)
        },
        ..*p
    };
N
Niko Matsakis 已提交
25 26
}

27 28
#[instrument(level = "debug", skip(tcx))]
fn normalize_after_erasing_regions<'tcx, T: TypeFoldable<'tcx> + PartialEq + Copy>(
29
    tcx: TyCtxt<'tcx>,
30 31
    goal: ParamEnvAnd<'tcx, T>,
) -> T {
32 33 34
    let ParamEnvAnd { param_env, value } = goal;
    tcx.infer_ctxt().enter(|infcx| {
        let cause = ObligationCause::dummy();
B
Bastian Kauschke 已提交
35
        match infcx.at(&cause, param_env).normalize(value) {
M
Mark Rousskov 已提交
36
            Ok(Normalized { value: normalized_value, obligations: normalized_obligations }) => {
37 38 39 40
                // We don't care about the `obligations`; they are
                // always only region relations, and we are about to
                // erase those anyway:
                debug_assert_eq!(
B
Bastian Kauschke 已提交
41
                    normalized_obligations.iter().find(|p| not_outlives_predicate(&p.predicate)),
42 43 44
                    None,
                );

45 46 47 48 49 50 51 52
                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
53 54 55 56 57
            }
            Err(NoSolution) => bug!("could not fully normalize `{:?}`", value),
        }
    })
}
58

B
Bastian Kauschke 已提交
59
fn not_outlives_predicate(p: &ty::Predicate<'tcx>) -> bool {
J
Jack Huey 已提交
60 61 62 63 64 65 66 67 68 69 70
    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(..)
        | ty::PredicateKind::ConstEvaluatable(..)
        | ty::PredicateKind::ConstEquate(..)
        | ty::PredicateKind::TypeWellFormedFromEnv(..) => true,
71 72
    }
}