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
This commit is contained in:
Matthias Krüger 2024-02-15 09:20:23 +01:00 committed by GitHub
commit 829b59a47d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 2 deletions

View File

@ -98,6 +98,26 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
} }
} }
} }
// Enforce that coroutine-closure layouts are identical.
if let Some(layout) = body.coroutine_layout()
&& let Some(by_move_body) = body.coroutine_by_move_body()
&& let Some(by_move_layout) = by_move_body.coroutine_layout()
{
if layout != by_move_layout {
// If this turns out not to be true, please let compiler-errors know.
// It is possible to support, but requires some changes to the layout
// computation code.
cfg_checker.fail(
Location::START,
format!(
"Coroutine layout differs from by-move coroutine layout:\n\
layout: {layout:#?}\n\
by_move_layout: {by_move_layout:#?}",
),
);
}
}
} }
} }

View File

@ -86,7 +86,8 @@ pub struct UnsafetyCheckResult {
pub struct CoroutineSavedLocal {} pub struct CoroutineSavedLocal {}
} }
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)] #[derive(Clone, Debug, PartialEq, Eq)]
#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
pub struct CoroutineSavedTy<'tcx> { pub struct CoroutineSavedTy<'tcx> {
pub ty: Ty<'tcx>, pub ty: Ty<'tcx>,
/// Source info corresponding to the local in the original MIR body. /// Source info corresponding to the local in the original MIR body.
@ -96,7 +97,8 @@ pub struct CoroutineSavedTy<'tcx> {
} }
/// The layout of coroutine state. /// The layout of coroutine state.
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)] #[derive(Clone, PartialEq, Eq)]
#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
pub struct CoroutineLayout<'tcx> { pub struct CoroutineLayout<'tcx> {
/// The type of every local stored inside the coroutine. /// The type of every local stored inside the coroutine.
pub field_tys: IndexVec<CoroutineSavedLocal, CoroutineSavedTy<'tcx>>, pub field_tys: IndexVec<CoroutineSavedLocal, CoroutineSavedTy<'tcx>>,