Querify coroutine_hidden_types

This commit is contained in:
Michael Goulet 2025-05-20 10:28:31 +00:00
parent f8e9e7636a
commit 6555ef7f09
10 changed files with 67 additions and 41 deletions

View file

@ -6,7 +6,7 @@ use rustc_span::ErrorGuaranteed;
use crate::query::CyclePlaceholder;
use crate::ty::adjustment::CoerceUnsizedInfo;
use crate::ty::{self, Ty};
use crate::ty::{self, Ty, TyCtxt};
use crate::{mir, traits};
#[derive(Copy, Clone)]
@ -207,6 +207,11 @@ impl EraseType for ty::Binder<'_, ty::FnSig<'_>> {
type Result = [u8; size_of::<ty::Binder<'static, ty::FnSig<'static>>>()];
}
impl EraseType for ty::Binder<'_, ty::CoroutineWitnessTypes<TyCtxt<'_>>> {
type Result =
[u8; size_of::<ty::Binder<'static, ty::CoroutineWitnessTypes<TyCtxt<'static>>>>()];
}
impl EraseType for ty::Binder<'_, &'_ ty::List<Ty<'_>>> {
type Result = [u8; size_of::<ty::Binder<'static, &'static ty::List<Ty<'static>>>>()];
}

View file

@ -922,6 +922,12 @@ rustc_queries! {
separate_provide_extern
}
query coroutine_hidden_types(
def_id: DefId
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
desc { "looking up the hidden types stored across await points in a coroutine" }
}
/// Gets a map with the variances of every item in the local crate.
///
/// <div class="warning">

View file

@ -340,7 +340,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
fn coroutine_hidden_types(
self,
def_id: DefId,
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, &'tcx ty::List<Ty<'tcx>>>> {
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
self.coroutine_hidden_types(def_id)
}

View file

@ -26,7 +26,7 @@ use crate::query::Providers;
use crate::ty::layout::{FloatExt, IntegerExt};
use crate::ty::{
self, Asyncness, FallibleTypeFolder, GenericArgKind, GenericArgsRef, Ty, TyCtxt, TypeFoldable,
TypeFolder, TypeSuperFoldable, TypeVisitableExt, Upcast, fold_regions,
TypeFolder, TypeSuperFoldable, TypeVisitableExt, Upcast,
};
#[derive(Copy, Clone, Debug)]
@ -737,40 +737,6 @@ impl<'tcx> TyCtxt<'tcx> {
}
}
/// Return the set of types that should be taken into account when checking
/// trait bounds on a coroutine's internal state. This properly replaces
/// `ReErased` with new existential bound lifetimes.
pub fn coroutine_hidden_types(
self,
def_id: DefId,
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, &'tcx ty::List<Ty<'tcx>>>> {
let coroutine_layout = self.mir_coroutine_witnesses(def_id);
let mut vars = vec![];
let bound_tys = self.mk_type_list_from_iter(
coroutine_layout
.as_ref()
.map_or_else(|| [].iter(), |l| l.field_tys.iter())
.filter(|decl| !decl.ignore_for_traits)
.map(|decl| {
let ty = fold_regions(self, decl.ty, |re, debruijn| {
assert_eq!(re, self.lifetimes.re_erased);
let var = ty::BoundVar::from_usize(vars.len());
vars.push(ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon));
ty::Region::new_bound(
self,
debruijn,
ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon },
)
});
ty
}),
);
ty::EarlyBinder::bind(ty::Binder::bind_with_vars(
bound_tys,
self.mk_bound_variable_kinds(&vars),
))
}
/// Expands the given impl trait type, stopping if the type is recursive.
#[instrument(skip(self), level = "debug", ret)]
pub fn try_expand_impl_trait_type(

View file

@ -83,7 +83,7 @@ where
.cx()
.coroutine_hidden_types(def_id)
.instantiate(cx, args)
.map_bound(|tys| tys.to_vec())),
.map_bound(|bound| bound.types.to_vec())),
ty::UnsafeBinder(bound_ty) => Ok(bound_ty.map_bound(|ty| vec![ty])),
@ -249,7 +249,7 @@ where
.cx()
.coroutine_hidden_types(def_id)
.instantiate(ecx.cx(), args)
.map_bound(|tys| tys.to_vec())),
.map_bound(|bound| bound.types.to_vec())),
}
}

View file

@ -2866,7 +2866,7 @@ fn rebind_coroutine_witness_types<'tcx>(
let shifted_coroutine_types =
tcx.shift_bound_var_indices(bound_vars.len(), bound_coroutine_types.skip_binder());
ty::Binder::bind_with_vars(
ty::EarlyBinder::bind(shifted_coroutine_types.to_vec()).instantiate(tcx, args),
ty::EarlyBinder::bind(shifted_coroutine_types.types.to_vec()).instantiate(tcx, args),
tcx.mk_bound_variable_kinds_from_iter(
bound_vars.iter().chain(bound_coroutine_types.bound_vars()),
),

View file

@ -0,0 +1,37 @@
use rustc_hir::def_id::DefId;
use rustc_middle::ty::{self, TyCtxt, fold_regions};
/// Return the set of types that should be taken into account when checking
/// trait bounds on a coroutine's internal state. This properly replaces
/// `ReErased` with new existential bound lifetimes.
pub(crate) fn coroutine_hidden_types<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: DefId,
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
let coroutine_layout = tcx.mir_coroutine_witnesses(def_id);
let mut vars = vec![];
let bound_tys = tcx.mk_type_list_from_iter(
coroutine_layout
.as_ref()
.map_or_else(|| [].iter(), |l| l.field_tys.iter())
.filter(|decl| !decl.ignore_for_traits)
.map(|decl| {
let ty = fold_regions(tcx, decl.ty, |re, debruijn| {
assert_eq!(re, tcx.lifetimes.re_erased);
let var = ty::BoundVar::from_usize(vars.len());
vars.push(ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon));
ty::Region::new_bound(
tcx,
debruijn,
ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon },
)
});
ty
}),
);
ty::EarlyBinder::bind(ty::Binder::bind_with_vars(
ty::CoroutineWitnessTypes { types: bound_tys },
tcx.mk_bound_variable_kinds(&vars),
))
}

View file

@ -5,6 +5,7 @@
// tidy-alphabetical-end
mod codegen;
mod coroutine_witnesses;
mod dropck_outlives;
mod evaluate_obligation;
mod implied_outlives_bounds;
@ -24,4 +25,5 @@ pub fn provide(p: &mut Providers) {
normalize_erasing_regions::provide(p);
type_op::provide(p);
p.codegen_select_candidate = codegen::codegen_select_candidate;
p.coroutine_hidden_types = coroutine_witnesses::coroutine_hidden_types;
}

View file

@ -210,7 +210,7 @@ pub trait Interner:
fn coroutine_hidden_types(
self,
def_id: Self::DefId,
) -> ty::EarlyBinder<Self, ty::Binder<Self, Self::Tys>>;
) -> ty::EarlyBinder<Self, ty::Binder<Self, ty::CoroutineWitnessTypes<Self>>>;
fn fn_sig(
self,

View file

@ -1163,3 +1163,13 @@ pub struct FnHeader<I: Interner> {
pub safety: I::Safety,
pub abi: I::Abi,
}
#[derive_where(Clone, Copy, Debug, PartialEq, Eq, Hash; I: Interner)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
)]
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
pub struct CoroutineWitnessTypes<I: Interner> {
pub types: I::Tys,
}