rustc_traits/
coroutine_witnesses.rs1use 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
9pub(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
49fn 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 ®ion_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 .filter(|o| !o.has_infer()),
99 )
100 })
101}