|
1 | 1 | use rustc_hir::def_id::DefId;
|
2 |
| -use rustc_middle::ty::{self, TyCtxt, fold_regions}; |
| 2 | +use rustc_infer::infer::TyCtxtInferExt; |
| 3 | +use rustc_infer::infer::canonical::query_response::make_query_region_constraints; |
| 4 | +use rustc_infer::traits::{Obligation, ObligationCause}; |
| 5 | +use rustc_middle::ty::{self, Ty, TyCtxt, fold_regions}; |
| 6 | +use rustc_trait_selection::traits::{ObligationCtxt, with_replaced_escaping_bound_vars}; |
3 | 7 |
|
4 | 8 | /// Return the set of types that should be taken into account when checking
|
5 | 9 | /// trait bounds on a coroutine's internal state. This properly replaces
|
@@ -29,8 +33,50 @@ pub(crate) fn coroutine_hidden_types<'tcx>(
|
29 | 33 | ty
|
30 | 34 | }),
|
31 | 35 | );
|
| 36 | + |
| 37 | + let assumptions = compute_assumptions(tcx, def_id, bound_tys); |
| 38 | + |
32 | 39 | ty::EarlyBinder::bind(ty::Binder::bind_with_vars(
|
33 |
| - ty::CoroutineWitnessTypes { types: bound_tys, assumptions: ty::List::empty() }, |
| 40 | + ty::CoroutineWitnessTypes { types: bound_tys, assumptions }, |
34 | 41 | tcx.mk_bound_variable_kinds(&vars),
|
35 | 42 | ))
|
36 | 43 | }
|
| 44 | + |
| 45 | +fn compute_assumptions<'tcx>( |
| 46 | + tcx: TyCtxt<'tcx>, |
| 47 | + def_id: DefId, |
| 48 | + bound_tys: &'tcx ty::List<Ty<'tcx>>, |
| 49 | +) -> &'tcx ty::List<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>> { |
| 50 | + let infcx = tcx.infer_ctxt().build(ty::TypingMode::Analysis { |
| 51 | + defining_opaque_types_and_generators: ty::List::empty(), |
| 52 | + }); |
| 53 | + with_replaced_escaping_bound_vars(&infcx, &mut vec![None], bound_tys, |bound_tys| { |
| 54 | + let param_env = tcx.param_env(def_id); |
| 55 | + let ocx = ObligationCtxt::new(&infcx); |
| 56 | + |
| 57 | + ocx.register_obligations(bound_tys.iter().map(|ty| { |
| 58 | + Obligation::new( |
| 59 | + tcx, |
| 60 | + ObligationCause::dummy(), |
| 61 | + param_env, |
| 62 | + ty::ClauseKind::WellFormed(ty.into()), |
| 63 | + ) |
| 64 | + })); |
| 65 | + let _errors = ocx.select_all_or_error(); |
| 66 | + |
| 67 | + let region_obligations = infcx.take_registered_region_obligations(); |
| 68 | + let region_constraints = infcx.take_and_reset_region_constraints(); |
| 69 | + tcx.mk_outlives_from_iter( |
| 70 | + make_query_region_constraints( |
| 71 | + tcx, |
| 72 | + region_obligations |
| 73 | + .iter() |
| 74 | + .map(|r_o| (r_o.sup_type, r_o.sub_region, r_o.origin.to_constraint_category())), |
| 75 | + ®ion_constraints, |
| 76 | + ) |
| 77 | + .outlives |
| 78 | + .into_iter() |
| 79 | + .map(|(o, _)| o), |
| 80 | + ) |
| 81 | + }) |
| 82 | +} |
0 commit comments