Skip to content

Commit ea49d7c

Browse files
Stop using prefix_tys
This function hints at an early commitment to coroutine memory layout. We should not give promises on how upvars are allocated. Signed-off-by: Xiangfei Ding <dingxiangfei2009@protonmail.ch>
1 parent f977dfc commit ea49d7c

File tree

8 files changed

+32
-71
lines changed

8 files changed

+32
-71
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use rustc_middle::traits::query::NoSolution;
2525
use rustc_middle::ty::adjustment::PointerCoercion;
2626
use rustc_middle::ty::cast::CastTy;
2727
use rustc_middle::ty::{
28-
self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, CoroutineArgsExt,
29-
GenericArgsRef, Ty, TyCtxt, TypeVisitableExt, UserArgs, UserTypeAnnotationIndex, fold_regions,
28+
self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, GenericArgsRef, Ty, TyCtxt,
29+
TypeVisitableExt, UserArgs, UserTypeAnnotationIndex, fold_regions,
3030
};
3131
use rustc_middle::{bug, span_bug};
3232
use rustc_mir_dataflow::move_paths::MoveData;
@@ -2165,14 +2165,15 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21652165
}
21662166
}
21672167
AggregateKind::Coroutine(_, args) => {
2168-
// It doesn't make sense to look at a field beyond the prefix;
2169-
// these require a variant index, and are not initialized in
2170-
// aggregate rvalues.
2171-
match args.as_coroutine().prefix_tys().get(field_index.as_usize()) {
2172-
Some(ty) => Ok(*ty),
2173-
None => Err(FieldAccessError::OutOfRange {
2174-
field_count: args.as_coroutine().prefix_tys().len(),
2175-
}),
2168+
// It doesn't make sense to look at a field beyond the captured
2169+
// upvars.
2170+
// Otherwise it require a variant index, and are not initialized
2171+
// in aggregate rvalues.
2172+
let upvar_tys = &args.as_coroutine().upvar_tys();
2173+
if let Some(ty) = upvar_tys.get(field_index.as_usize()) {
2174+
Ok(*ty)
2175+
} else {
2176+
Err(FieldAccessError::OutOfRange { field_count: upvar_tys.len() })
21762177
}
21772178
}
21782179
AggregateKind::CoroutineClosure(_, args) => {

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ use rustc_middle::bug;
1515
use rustc_middle::ty::layout::{
1616
HasTypingEnv, LayoutOf, TyAndLayout, WIDE_PTR_ADDR, WIDE_PTR_EXTRA,
1717
};
18-
use rustc_middle::ty::{
19-
self, AdtKind, CoroutineArgsExt, ExistentialTraitRef, Instance, Ty, TyCtxt, Visibility,
20-
};
18+
use rustc_middle::ty::{self, AdtKind, ExistentialTraitRef, Instance, Ty, TyCtxt, Visibility};
2119
use rustc_session::config::{self, DebugInfo, Lto};
2220
use rustc_span::{
2321
DUMMY_SP, FileName, FileNameDisplayPreference, SourceFile, Span, Symbol, hygiene,
@@ -1152,7 +1150,7 @@ fn build_upvar_field_di_nodes<'ll, 'tcx>(
11521150
closure_or_coroutine_di_node: &'ll DIType,
11531151
) -> SmallVec<&'ll DIType> {
11541152
let (&def_id, up_var_tys) = match closure_or_coroutine_ty.kind() {
1155-
ty::Coroutine(def_id, args) => (def_id, args.as_coroutine().prefix_tys()),
1153+
ty::Coroutine(def_id, args) => (def_id, args.as_coroutine().upvar_tys()),
11561154
ty::Closure(def_id, args) => (def_id, args.as_closure().upvar_tys()),
11571155
ty::CoroutineClosure(def_id, args) => (def_id, args.as_coroutine_closure().upvar_tys()),
11581156
_ => {

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ use rustc_codegen_ssa::debuginfo::type_names::{compute_debuginfo_type_name, cpp_
55
use rustc_codegen_ssa::debuginfo::{tag_base_type, wants_c_like_enum_debuginfo};
66
use rustc_codegen_ssa::traits::MiscCodegenMethods;
77
use rustc_hir::def::CtorKind;
8-
use rustc_index::IndexSlice;
98
use rustc_middle::bug;
109
use rustc_middle::mir::CoroutineLayout;
1110
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1211
use rustc_middle::ty::{self, AdtDef, CoroutineArgs, CoroutineArgsExt, Ty, VariantDef};
13-
use rustc_span::{Span, Symbol};
12+
use rustc_span::Span;
1413

1514
use super::type_map::{DINodeCreationResult, UniqueTypeId};
1615
use super::{SmallVec, size_and_align_of};
@@ -287,7 +286,6 @@ fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
287286
coroutine_type_and_layout: TyAndLayout<'tcx>,
288287
coroutine_type_di_node: &'ll DIType,
289288
coroutine_layout: &CoroutineLayout<'tcx>,
290-
common_upvar_names: &IndexSlice<FieldIdx, Symbol>,
291289
) -> &'ll DIType {
292290
let variant_name = CoroutineArgs::variant_name(variant_index);
293291
let unique_type_id = UniqueTypeId::for_enum_variant_struct_type(
@@ -298,11 +296,6 @@ fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
298296

299297
let variant_layout = coroutine_type_and_layout.for_variant(cx, variant_index);
300298

301-
let coroutine_args = match coroutine_type_and_layout.ty.kind() {
302-
ty::Coroutine(_, args) => args.as_coroutine(),
303-
_ => unreachable!(),
304-
};
305-
306299
type_map::build_type_with_children(
307300
cx,
308301
type_map::stub(
@@ -317,7 +310,7 @@ fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
317310
),
318311
|cx, variant_struct_type_di_node| {
319312
// Fields that just belong to this variant/state
320-
let state_specific_fields: SmallVec<_> = (0..variant_layout.fields.count())
313+
(0..variant_layout.fields.count())
321314
.map(|field_index| {
322315
let coroutine_saved_local = coroutine_layout.variant_fields[variant_index]
323316
[FieldIdx::from_usize(field_index)];
@@ -340,29 +333,7 @@ fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
340333
None,
341334
)
342335
})
343-
.collect();
344-
345-
// Fields that are common to all states
346-
let common_fields: SmallVec<_> = coroutine_args
347-
.prefix_tys()
348-
.iter()
349-
.zip(common_upvar_names)
350-
.enumerate()
351-
.map(|(index, (upvar_ty, upvar_name))| {
352-
build_field_di_node(
353-
cx,
354-
variant_struct_type_di_node,
355-
upvar_name.as_str(),
356-
cx.layout_of(upvar_ty),
357-
coroutine_type_and_layout.fields.offset(index),
358-
DIFlags::FlagZero,
359-
type_di_node(cx, upvar_ty),
360-
None,
361-
)
362-
})
363-
.collect();
364-
365-
state_specific_fields.into_iter().chain(common_fields).collect()
336+
.collect()
366337
},
367338
|cx| build_generic_type_param_di_nodes(cx, coroutine_type_and_layout.ty),
368339
)

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ impl<'tcx> PlaceTy<'tcx> {
156156
.get(f.index())
157157
.copied()
158158
.unwrap_or_else(|| bug!("field {f:?} out of range: {self_ty:?}")),
159-
// Only prefix fields (upvars and current state) are
160-
// accessible without a variant index.
161-
ty::Coroutine(_, args) => {
162-
args.as_coroutine().prefix_tys().get(f.index()).copied().unwrap_or_else(|| {
163-
bug!("field {f:?} out of range of prefixes for {self_ty}")
164-
})
165-
}
159+
// Only upvars are accessible without a variant index.
160+
ty::Coroutine(_, args) => args
161+
.as_coroutine()
162+
.upvar_tys()
163+
.get(f.index())
164+
.copied()
165+
.unwrap_or_else(|| bug!("field {f:?} out of range: {self_ty:?}")),
166166
ty::Tuple(tys) => tys
167167
.get(f.index())
168168
.copied()

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,9 +956,10 @@ where
956956
),
957957
Variants::Multiple { tag, tag_field, .. } => {
958958
if FieldIdx::from_usize(i) == tag_field {
959-
return TyMaybeWithLayout::TyAndLayout(tag_layout(tag));
959+
TyMaybeWithLayout::TyAndLayout(tag_layout(tag))
960+
} else {
961+
TyMaybeWithLayout::Ty(args.as_coroutine().upvar_tys()[i])
960962
}
961-
TyMaybeWithLayout::Ty(args.as_coroutine().prefix_tys()[i])
962963
}
963964
},
964965

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,6 @@ impl<'tcx> ty::CoroutineArgs<TyCtxt<'tcx>> {
150150
})
151151
})
152152
}
153-
154-
/// This is the types of the fields of a coroutine which are not stored in a
155-
/// variant.
156-
#[inline]
157-
fn prefix_tys(self) -> &'tcx List<Ty<'tcx>> {
158-
self.upvar_tys()
159-
}
160153
}
161154

162155
#[derive(Debug, Copy, Clone, HashStable, TypeFoldable, TypeVisitable)]

compiler/rustc_mir_transform/src/validate.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::mir::*;
1414
use rustc_middle::ty::adjustment::PointerCoercion;
1515
use rustc_middle::ty::print::with_no_trimmed_paths;
1616
use rustc_middle::ty::{
17-
self, CoroutineArgsExt, InstanceKind, ScalarInt, Ty, TyCtxt, TypeVisitableExt, Upcast, Variance,
17+
self, InstanceKind, ScalarInt, Ty, TyCtxt, TypeVisitableExt, Upcast, Variance,
1818
};
1919
use rustc_middle::{bug, span_bug};
2020
use rustc_mir_dataflow::debuginfo::debuginfo_locals;
@@ -786,14 +786,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
786786
};
787787

788788
ty::EarlyBinder::bind(f_ty.ty).instantiate(self.tcx, args)
789-
} else {
790-
let Some(&f_ty) = args.as_coroutine().prefix_tys().get(f.index())
791-
else {
792-
fail_out_of_bounds(self, location);
793-
return;
794-
};
795-
789+
} else if let Some(&f_ty) = args.as_coroutine().upvar_tys().get(f.index()) {
796790
f_ty
791+
} else {
792+
fail_out_of_bounds(self, location);
793+
return;
797794
};
798795

799796
check_equal(self, location, f_ty);

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ fn layout_of_uncached<'tcx>(
528528

529529
let prefix_layouts = args
530530
.as_coroutine()
531-
.prefix_tys()
531+
.upvar_tys()
532532
.iter()
533533
.map(|ty| cx.layout_of(ty))
534534
.try_collect::<IndexVec<_, _>>()?;

0 commit comments

Comments
 (0)