Skip to content

Commit 829b59a

Browse files
authored
Rollup merge of #121122 - compiler-errors:identical-layouts, r=oli-obk
Enforce coroutine-closure layouts are identical Enforce that for an async closure, the by-ref and by-move coroutine layouts are identical. This is just a sanity check to make sure that optimizations aren't doing anything fishy. r? oli-obk
2 parents 4899108 + e6a21f5 commit 829b59a

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

compiler/rustc_const_eval/src/transform/validate.rs

+20
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ impl<'tcx> MirPass<'tcx> for Validator {
9898
}
9999
}
100100
}
101+
102+
// Enforce that coroutine-closure layouts are identical.
103+
if let Some(layout) = body.coroutine_layout()
104+
&& let Some(by_move_body) = body.coroutine_by_move_body()
105+
&& let Some(by_move_layout) = by_move_body.coroutine_layout()
106+
{
107+
if layout != by_move_layout {
108+
// If this turns out not to be true, please let compiler-errors know.
109+
// It is possible to support, but requires some changes to the layout
110+
// computation code.
111+
cfg_checker.fail(
112+
Location::START,
113+
format!(
114+
"Coroutine layout differs from by-move coroutine layout:\n\
115+
layout: {layout:#?}\n\
116+
by_move_layout: {by_move_layout:#?}",
117+
),
118+
);
119+
}
120+
}
101121
}
102122
}
103123

compiler/rustc_middle/src/mir/query.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ rustc_index::newtype_index! {
8686
pub struct CoroutineSavedLocal {}
8787
}
8888

89-
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
89+
#[derive(Clone, Debug, PartialEq, Eq)]
90+
#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
9091
pub struct CoroutineSavedTy<'tcx> {
9192
pub ty: Ty<'tcx>,
9293
/// Source info corresponding to the local in the original MIR body.
@@ -96,7 +97,8 @@ pub struct CoroutineSavedTy<'tcx> {
9697
}
9798

9899
/// The layout of coroutine state.
99-
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
100+
#[derive(Clone, PartialEq, Eq)]
101+
#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
100102
pub struct CoroutineLayout<'tcx> {
101103
/// The type of every local stored inside the coroutine.
102104
pub field_tys: IndexVec<CoroutineSavedLocal, CoroutineSavedTy<'tcx>>,

0 commit comments

Comments
 (0)