Skip to content

Querify coroutine_hidden_types #141286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_middle/src/query/erase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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>>>>()];
}
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
36 changes: 1 addition & 35 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])),

Expand Down Expand Up @@ -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())),
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
),
Expand Down
37 changes: 37 additions & 0 deletions compiler/rustc_traits/src/coroutine_witnesses.rs
Original file line number Diff line number Diff line change
@@ -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),
))
}
2 changes: 2 additions & 0 deletions compiler/rustc_traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// tidy-alphabetical-end

mod codegen;
mod coroutine_witnesses;
mod dropck_outlives;
mod evaluate_obligation;
mod implied_outlives_bounds;
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion compiler/rustc_type_ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_type_ir/src/ty_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Loading