From a48cebc4b833bc0df9242ae9845a79299b4cf079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Tue, 12 Dec 2023 00:00:00 +0000 Subject: [PATCH] Coroutine variant fields can be uninitialized Wrap coroutine variant fields in MaybeUninit to indicate that they might be uninitialized. Otherwise an uninhabited field will make the entire variant uninhabited and introduce undefined behaviour. The analogous issue in the prefix of coroutine layout was addressed by 6fae7f807146e400fa2bbd1c44768d9bcaa57c4c. --- compiler/rustc_ty_utils/src/layout.rs | 5 ++- .../future-sizes/async-awaiting-fut.stdout | 6 +++ .../indirect-recursion-issue-112047.stderr | 8 ++-- tests/ui/coroutine/uninhabited-field.rs | 37 +++++++++++++++++++ .../coroutine_discr_placement.stdout | 6 +++ 5 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 tests/ui/coroutine/uninhabited-field.rs diff --git a/compiler/rustc_ty_utils/src/layout.rs b/compiler/rustc_ty_utils/src/layout.rs index d896873fadda9..7918965e04baa 100644 --- a/compiler/rustc_ty_utils/src/layout.rs +++ b/compiler/rustc_ty_utils/src/layout.rs @@ -831,7 +831,10 @@ fn coroutine_layout<'tcx>( Assigned(_) => bug!("assignment does not match variant"), Ineligible(_) => false, }) - .map(|local| subst_field(info.field_tys[*local].ty)); + .map(|local| { + let field_ty = subst_field(info.field_tys[*local].ty); + Ty::new_maybe_uninit(tcx, field_ty) + }); let mut variant = univariant_uninterned( cx, diff --git a/tests/ui/async-await/future-sizes/async-awaiting-fut.stdout b/tests/ui/async-await/future-sizes/async-awaiting-fut.stdout index b0447a5826119..47b39e5246dde 100644 --- a/tests/ui/async-await/future-sizes/async-awaiting-fut.stdout +++ b/tests/ui/async-await/future-sizes/async-awaiting-fut.stdout @@ -52,10 +52,16 @@ print-type-size variant `Panicked`: 1024 bytes print-type-size upvar `.arg`: 1024 bytes print-type-size type: `std::mem::ManuallyDrop`: 1 bytes, alignment: 1 bytes print-type-size field `.value`: 1 bytes +print-type-size type: `std::mem::ManuallyDrop<{async fn body@$DIR/async-awaiting-fut.rs:6:17: 6:19}>`: 1 bytes, alignment: 1 bytes +print-type-size field `.value`: 1 bytes print-type-size type: `std::mem::MaybeUninit`: 1 bytes, alignment: 1 bytes print-type-size variant `MaybeUninit`: 1 bytes print-type-size field `.uninit`: 0 bytes print-type-size field `.value`: 1 bytes +print-type-size type: `std::mem::MaybeUninit<{async fn body@$DIR/async-awaiting-fut.rs:6:17: 6:19}>`: 1 bytes, alignment: 1 bytes +print-type-size variant `MaybeUninit`: 1 bytes +print-type-size field `.uninit`: 0 bytes +print-type-size field `.value`: 1 bytes print-type-size type: `std::task::Poll<()>`: 1 bytes, alignment: 1 bytes print-type-size discriminant: 1 bytes print-type-size variant `Ready`: 0 bytes diff --git a/tests/ui/async-await/in-trait/indirect-recursion-issue-112047.stderr b/tests/ui/async-await/in-trait/indirect-recursion-issue-112047.stderr index 95731b67ccf77..8e573b512ad43 100644 --- a/tests/ui/async-await/in-trait/indirect-recursion-issue-112047.stderr +++ b/tests/ui/async-await/in-trait/indirect-recursion-issue-112047.stderr @@ -1,7 +1,9 @@ -error[E0391]: cycle detected when computing layout of `{async fn body@$DIR/indirect-recursion-issue-112047.rs:33:27: 35:6}` +error[E0391]: cycle detected when computing layout of `core::mem::maybe_uninit::MaybeUninit<{async fn body@$DIR/indirect-recursion-issue-112047.rs:33:27: 35:6}>` | - = note: ...which requires computing layout of `<::Second as Second>::{opaque#0}`... - = note: ...which again requires computing layout of `{async fn body@$DIR/indirect-recursion-issue-112047.rs:33:27: 35:6}`, completing the cycle + = note: ...which requires computing layout of `core::mem::manually_drop::ManuallyDrop<{async fn body@$DIR/indirect-recursion-issue-112047.rs:33:27: 35:6}>`... + = note: ...which requires computing layout of `{async fn body@$DIR/indirect-recursion-issue-112047.rs:33:27: 35:6}`... + = note: ...which requires computing layout of `core::mem::maybe_uninit::MaybeUninit<<::Second as Second>::{opaque#0}>`... + = note: ...which again requires computing layout of `core::mem::maybe_uninit::MaybeUninit<{async fn body@$DIR/indirect-recursion-issue-112047.rs:33:27: 35:6}>`, completing the cycle = note: cycle used when computing layout of `{async block@$DIR/indirect-recursion-issue-112047.rs:6:13: 8:6}` = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information diff --git a/tests/ui/coroutine/uninhabited-field.rs b/tests/ui/coroutine/uninhabited-field.rs new file mode 100644 index 0000000000000..d9570c2fed8d4 --- /dev/null +++ b/tests/ui/coroutine/uninhabited-field.rs @@ -0,0 +1,37 @@ +// Test that uninhabited saved local doesn't make the entire variant uninhabited. +// run-pass +#![allow(unused)] +#![feature(assert_matches)] +#![feature(coroutine_trait)] +#![feature(coroutines)] +#![feature(never_type)] +use std::assert_matches::assert_matches; +use std::ops::Coroutine; +use std::ops::CoroutineState; +use std::pin::Pin; + +fn conjure() -> T { loop {} } + +fn run(x: bool, y: bool) { + let mut c = || { + if x { + let a : T; + if y { + a = conjure::(); + } + yield (); + } else { + let a : T; + if y { + a = conjure::(); + } + yield (); + } + }; + assert_matches!(Pin::new(&mut c).resume(()), CoroutineState::Yielded(())); + assert_matches!(Pin::new(&mut c).resume(()), CoroutineState::Complete(())); +} + +fn main() { + run::(false, false); +} diff --git a/tests/ui/print_type_sizes/coroutine_discr_placement.stdout b/tests/ui/print_type_sizes/coroutine_discr_placement.stdout index f34a8e9a706aa..71a7f3c63815c 100644 --- a/tests/ui/print_type_sizes/coroutine_discr_placement.stdout +++ b/tests/ui/print_type_sizes/coroutine_discr_placement.stdout @@ -9,3 +9,9 @@ print-type-size padding: 3 bytes print-type-size local `.z`: 4 bytes, alignment: 4 bytes print-type-size variant `Returned`: 0 bytes print-type-size variant `Panicked`: 0 bytes +print-type-size type: `std::mem::ManuallyDrop`: 4 bytes, alignment: 4 bytes +print-type-size field `.value`: 4 bytes +print-type-size type: `std::mem::MaybeUninit`: 4 bytes, alignment: 4 bytes +print-type-size variant `MaybeUninit`: 4 bytes +print-type-size field `.uninit`: 0 bytes +print-type-size field `.value`: 4 bytes