Skip to main content

rustc_traits/
coroutine_witnesses.rs

1use rustc_infer::infer::TyCtxtInferExt;
2use rustc_infer::infer::canonical::QueryRegionConstraint;
3use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
4use rustc_infer::traits::{Obligation, ObligationCause};
5use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, fold_regions};
6use rustc_span::def_id::DefId;
7use rustc_trait_selection::traits::{ObligationCtxt, with_replaced_escaping_bound_vars};
8
9/// Return the set of types that should be taken into account when checking
10/// trait bounds on a coroutine's internal state. This properly replaces
11/// `ReErased` with new existential bound lifetimes.
12pub(crate) fn coroutine_hidden_types<'tcx>(
13    tcx: TyCtxt<'tcx>,
14    def_id: DefId,
15) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
16    let coroutine_layout = tcx.mir_coroutine_witnesses(def_id);
17    let mut vars = ::alloc::vec::Vec::new()vec![];
18    let bound_tys = tcx.mk_type_list_from_iter(
19        coroutine_layout
20            .as_ref()
21            .map_or_else(|| [].iter(), |l| l.field_tys.iter())
22            .filter(|decl| !decl.ignore_for_traits)
23            .map(|decl| {
24                let ty = fold_regions(tcx, decl.ty, |re, debruijn| {
25                    {
    match (&re, &tcx.lifetimes.re_erased) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(re, tcx.lifetimes.re_erased);
26                    let var = ty::BoundVar::from_usize(vars.len());
27                    vars.push(ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon));
28                    ty::Region::new_bound(
29                        tcx,
30                        debruijn,
31                        ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon },
32                    )
33                });
34                ty
35            }),
36    );
37
38    let assumptions = compute_assumptions(tcx, def_id, bound_tys);
39
40    ty::EarlyBinder::bind(
41        tcx,
42        ty::Binder::bind_with_vars(
43            ty::CoroutineWitnessTypes { types: bound_tys, assumptions },
44            tcx.mk_bound_variable_kinds(&vars),
45        ),
46    )
47}
48
49// FIXME: The assumptions are only used in the old solver when `-Zhigher-ranked-assumptions`
50// is true. `-Zhigher-ranked-assumptions` is superseded by `assumptions-on-binders`.
51// We can remove this function soon.
52fn compute_assumptions<'tcx>(
53    tcx: TyCtxt<'tcx>,
54    def_id: DefId,
55    bound_tys: &'tcx ty::List<Ty<'tcx>>,
56) -> &'tcx ty::List<ty::ArgOutlivesClause<'tcx>> {
57    if tcx.next_trait_solver_globally() || !tcx.sess.opts.unstable_opts.higher_ranked_assumptions {
58        return &ty::List::empty();
59    }
60
61    let infcx = tcx
62        .infer_ctxt()
63        .build(ty::TypingMode::Typeck { defining_opaque_types_and_generators: ty::List::empty() });
64    with_replaced_escaping_bound_vars(&infcx, &mut ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [None]))vec![None], bound_tys, |bound_tys| {
65        let param_env = tcx.param_env(def_id);
66        let ocx = ObligationCtxt::new(&infcx);
67
68        ocx.register_obligations(bound_tys.iter().map(|ty| {
69            Obligation::new(
70                tcx,
71                ObligationCause::dummy(),
72                param_env,
73                ty::ClauseKind::WellFormed(ty.into()),
74            )
75        }));
76        let _errors = ocx.evaluate_obligations_error_on_ambiguity();
77
78        let region_obligations = infcx.take_registered_region_obligations();
79        let region_assumptions = infcx.take_registered_region_assumptions();
80        let region_constraints = infcx.take_and_reset_region_constraints();
81
82        let constraints = infcx.deeply_resolve_via_unification_table(
83            make_query_region_constraints(
84                region_obligations,
85                &region_constraints,
86                region_assumptions,
87            )
88            .constraints,
89        );
90
91        tcx.mk_outlives_from_iter(
92            constraints
93                .into_iter()
94                .flat_map(|QueryRegionConstraint { constraint, .. }| constraint.iter_outlives())
95                // FIXME(higher_ranked_auto): We probably should deeply resolve these before
96                // filtering out infers which only correspond to unconstrained infer regions
97                // which we can sometimes get.
98                .filter(|o| !o.has_infer()),
99        )
100    })
101}