Skip to content

Commit 7608881

Browse files
committed
Auto merge of rust-lang#141286 - compiler-errors:querify-coroutine, r=<try>
Querify `coroutine_hidden_types` r? `@ghost`
2 parents f8e9e76 + 2f28598 commit 7608881

File tree

10 files changed

+67
-41
lines changed

10 files changed

+67
-41
lines changed

compiler/rustc_middle/src/query/erase.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_span::ErrorGuaranteed;
66

77
use crate::query::CyclePlaceholder;
88
use crate::ty::adjustment::CoerceUnsizedInfo;
9-
use crate::ty::{self, Ty};
9+
use crate::ty::{self, Ty, TyCtxt};
1010
use crate::{mir, traits};
1111

1212
#[derive(Copy, Clone)]
@@ -207,6 +207,11 @@ impl EraseType for ty::Binder<'_, ty::FnSig<'_>> {
207207
type Result = [u8; size_of::<ty::Binder<'static, ty::FnSig<'static>>>()];
208208
}
209209

210+
impl EraseType for ty::Binder<'_, ty::CoroutineWitnessTypes<TyCtxt<'_>>> {
211+
type Result =
212+
[u8; size_of::<ty::Binder<'static, ty::CoroutineWitnessTypes<TyCtxt<'static>>>>()];
213+
}
214+
210215
impl EraseType for ty::Binder<'_, &'_ ty::List<Ty<'_>>> {
211216
type Result = [u8; size_of::<ty::Binder<'static, &'static ty::List<Ty<'static>>>>()];
212217
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,12 @@ rustc_queries! {
922922
separate_provide_extern
923923
}
924924

925+
query coroutine_hidden_types(
926+
def_id: DefId,
927+
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
928+
desc { "looking up the hidden types stored across await points in a coroutine" }
929+
}
930+
925931
/// Gets a map with the variances of every item in the local crate.
926932
///
927933
/// <div class="warning">

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
340340
fn coroutine_hidden_types(
341341
self,
342342
def_id: DefId,
343-
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, &'tcx ty::List<Ty<'tcx>>>> {
343+
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
344344
self.coroutine_hidden_types(def_id)
345345
}
346346

compiler/rustc_middle/src/ty/util.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::query::Providers;
2626
use crate::ty::layout::{FloatExt, IntegerExt};
2727
use crate::ty::{
2828
self, Asyncness, FallibleTypeFolder, GenericArgKind, GenericArgsRef, Ty, TyCtxt, TypeFoldable,
29-
TypeFolder, TypeSuperFoldable, TypeVisitableExt, Upcast, fold_regions,
29+
TypeFolder, TypeSuperFoldable, TypeVisitableExt, Upcast,
3030
};
3131

3232
#[derive(Copy, Clone, Debug)]
@@ -737,40 +737,6 @@ impl<'tcx> TyCtxt<'tcx> {
737737
}
738738
}
739739

740-
/// Return the set of types that should be taken into account when checking
741-
/// trait bounds on a coroutine's internal state. This properly replaces
742-
/// `ReErased` with new existential bound lifetimes.
743-
pub fn coroutine_hidden_types(
744-
self,
745-
def_id: DefId,
746-
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, &'tcx ty::List<Ty<'tcx>>>> {
747-
let coroutine_layout = self.mir_coroutine_witnesses(def_id);
748-
let mut vars = vec![];
749-
let bound_tys = self.mk_type_list_from_iter(
750-
coroutine_layout
751-
.as_ref()
752-
.map_or_else(|| [].iter(), |l| l.field_tys.iter())
753-
.filter(|decl| !decl.ignore_for_traits)
754-
.map(|decl| {
755-
let ty = fold_regions(self, decl.ty, |re, debruijn| {
756-
assert_eq!(re, self.lifetimes.re_erased);
757-
let var = ty::BoundVar::from_usize(vars.len());
758-
vars.push(ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon));
759-
ty::Region::new_bound(
760-
self,
761-
debruijn,
762-
ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon },
763-
)
764-
});
765-
ty
766-
}),
767-
);
768-
ty::EarlyBinder::bind(ty::Binder::bind_with_vars(
769-
bound_tys,
770-
self.mk_bound_variable_kinds(&vars),
771-
))
772-
}
773-
774740
/// Expands the given impl trait type, stopping if the type is recursive.
775741
#[instrument(skip(self), level = "debug", ret)]
776742
pub fn try_expand_impl_trait_type(

compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ where
8383
.cx()
8484
.coroutine_hidden_types(def_id)
8585
.instantiate(cx, args)
86-
.map_bound(|tys| tys.to_vec())),
86+
.map_bound(|bound| bound.types.to_vec())),
8787

8888
ty::UnsafeBinder(bound_ty) => Ok(bound_ty.map_bound(|ty| vec![ty])),
8989

@@ -249,7 +249,7 @@ where
249249
.cx()
250250
.coroutine_hidden_types(def_id)
251251
.instantiate(ecx.cx(), args)
252-
.map_bound(|tys| tys.to_vec())),
252+
.map_bound(|bound| bound.types.to_vec())),
253253
}
254254
}
255255

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2866,7 +2866,7 @@ fn rebind_coroutine_witness_types<'tcx>(
28662866
let shifted_coroutine_types =
28672867
tcx.shift_bound_var_indices(bound_vars.len(), bound_coroutine_types.skip_binder());
28682868
ty::Binder::bind_with_vars(
2869-
ty::EarlyBinder::bind(shifted_coroutine_types.to_vec()).instantiate(tcx, args),
2869+
ty::EarlyBinder::bind(shifted_coroutine_types.types.to_vec()).instantiate(tcx, args),
28702870
tcx.mk_bound_variable_kinds_from_iter(
28712871
bound_vars.iter().chain(bound_coroutine_types.bound_vars()),
28722872
),
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use rustc_hir::def_id::DefId;
2+
use rustc_middle::ty::{self, TyCtxt, fold_regions};
3+
4+
/// Return the set of types that should be taken into account when checking
5+
/// trait bounds on a coroutine's internal state. This properly replaces
6+
/// `ReErased` with new existential bound lifetimes.
7+
pub(crate) fn coroutine_hidden_types<'tcx>(
8+
tcx: TyCtxt<'tcx>,
9+
def_id: DefId,
10+
) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
11+
let coroutine_layout = tcx.mir_coroutine_witnesses(def_id);
12+
let mut vars = vec![];
13+
let bound_tys = tcx.mk_type_list_from_iter(
14+
coroutine_layout
15+
.as_ref()
16+
.map_or_else(|| [].iter(), |l| l.field_tys.iter())
17+
.filter(|decl| !decl.ignore_for_traits)
18+
.map(|decl| {
19+
let ty = fold_regions(tcx, decl.ty, |re, debruijn| {
20+
assert_eq!(re, tcx.lifetimes.re_erased);
21+
let var = ty::BoundVar::from_usize(vars.len());
22+
vars.push(ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon));
23+
ty::Region::new_bound(
24+
tcx,
25+
debruijn,
26+
ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon },
27+
)
28+
});
29+
ty
30+
}),
31+
);
32+
33+
ty::EarlyBinder::bind(ty::Binder::bind_with_vars(
34+
ty::CoroutineWitnessTypes { types: bound_tys },
35+
tcx.mk_bound_variable_kinds(&vars),
36+
))
37+
}

compiler/rustc_traits/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// tidy-alphabetical-end
66

77
mod codegen;
8+
mod coroutine_witnesses;
89
mod dropck_outlives;
910
mod evaluate_obligation;
1011
mod implied_outlives_bounds;
@@ -24,4 +25,5 @@ pub fn provide(p: &mut Providers) {
2425
normalize_erasing_regions::provide(p);
2526
type_op::provide(p);
2627
p.codegen_select_candidate = codegen::codegen_select_candidate;
28+
p.coroutine_hidden_types = coroutine_witnesses::coroutine_hidden_types;
2729
}

compiler/rustc_type_ir/src/interner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub trait Interner:
210210
fn coroutine_hidden_types(
211211
self,
212212
def_id: Self::DefId,
213-
) -> ty::EarlyBinder<Self, ty::Binder<Self, Self::Tys>>;
213+
) -> ty::EarlyBinder<Self, ty::Binder<Self, ty::CoroutineWitnessTypes<Self>>>;
214214

215215
fn fn_sig(
216216
self,

compiler/rustc_type_ir/src/ty_kind.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,3 +1163,13 @@ pub struct FnHeader<I: Interner> {
11631163
pub safety: I::Safety,
11641164
pub abi: I::Abi,
11651165
}
1166+
1167+
#[derive_where(Clone, Copy, Debug, PartialEq, Eq, Hash; I: Interner)]
1168+
#[cfg_attr(
1169+
feature = "nightly",
1170+
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
1171+
)]
1172+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
1173+
pub struct CoroutineWitnessTypes<I: Interner> {
1174+
pub types: I::Tys,
1175+
}

0 commit comments

Comments
 (0)