diff --git a/compiler/rustc_mir_transform/src/generator.rs b/compiler/rustc_mir_transform/src/generator.rs index d9a66cace52e5..0984829e05e48 100644 --- a/compiler/rustc_mir_transform/src/generator.rs +++ b/compiler/rustc_mir_transform/src/generator.rs @@ -740,24 +740,36 @@ fn sanitize_witness<'tcx>( } }; - for (local, decl) in body.local_decls.iter_enumerated() { - // Ignore locals which are internal or not saved between yields. - if !saved_locals.contains(local) || decl.internal { - continue; - } - let decl_ty = tcx.normalize_erasing_regions(param_env, decl.ty); + // First check if any of the locals are an uninhabited type. If so, we don't need to do the + // rest of this check because this code unreachable. + let any_uninhabited = body.local_decls.iter_enumerated().any(|(_, decl)| { + tcx.is_ty_uninhabited_from( + tcx.parent_module(tcx.hir().local_def_id_to_hir_id(did.expect_local())).to_def_id(), + tcx.normalize_erasing_regions(param_env, decl.ty), + param_env, + ) + }); - // Sanity check that typeck knows about the type of locals which are - // live across a suspension point - if !allowed.contains(&decl_ty) && !allowed_upvars.contains(&decl_ty) { - span_bug!( - body.span, - "Broken MIR: generator contains type {} in MIR, \ + if !any_uninhabited { + for (local, decl) in body.local_decls.iter_enumerated() { + // Ignore locals which are internal or not saved between yields. + if !saved_locals.contains(local) || decl.internal { + continue; + } + let decl_ty = tcx.normalize_erasing_regions(param_env, decl.ty); + + // Sanity check that typeck knows about the type of locals which are + // live across a suspension point + if !allowed.contains(&decl_ty) && !allowed_upvars.contains(&decl_ty) { + span_bug!( + body.span, + "Broken MIR: generator contains type {} in MIR, \ but typeck only knows about {} and {:?}", - decl_ty, - allowed, - allowed_upvars - ); + decl_ty, + allowed, + allowed_upvars + ); + } } } } diff --git a/src/test/ui/async-await/async-fn-nonsend.rs b/src/test/ui/async-await/async-fn-nonsend.rs index d7f8d7ac546c0..df8f93236419b 100644 --- a/src/test/ui/async-await/async-fn-nonsend.rs +++ b/src/test/ui/async-await/async-fn-nonsend.rs @@ -47,13 +47,6 @@ async fn non_sync_with_method_call() { } } -async fn non_sync_with_method_call_panic() { - let f: &mut std::fmt::Formatter = panic!(); - if non_sync().fmt(f).unwrap() == () { - fut().await; - } -} - async fn non_sync_with_method_call_infinite_loop() { let f: &mut std::fmt::Formatter = loop {}; if non_sync().fmt(f).unwrap() == () { @@ -69,6 +62,5 @@ pub fn pass_assert() { //~^ ERROR future cannot be sent between threads safely assert_send(non_sync_with_method_call()); //~^ ERROR future cannot be sent between threads safely - assert_send(non_sync_with_method_call_panic()); assert_send(non_sync_with_method_call_infinite_loop()); } diff --git a/src/test/ui/async-await/async-fn-nonsend.stderr b/src/test/ui/async-await/async-fn-nonsend.stderr index 40ad46b48620d..e0a47bbdb84d5 100644 --- a/src/test/ui/async-await/async-fn-nonsend.stderr +++ b/src/test/ui/async-await/async-fn-nonsend.stderr @@ -1,5 +1,5 @@ error: future cannot be sent between threads safely - --> $DIR/async-fn-nonsend.rs:68:17 + --> $DIR/async-fn-nonsend.rs:61:17 | LL | assert_send(non_send_temporary_in_match()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future returned by `non_send_temporary_in_match` is not `Send` @@ -16,13 +16,13 @@ LL | Some(_) => fut().await, LL | } | - `Some(non_send())` is later dropped here note: required by a bound in `assert_send` - --> $DIR/async-fn-nonsend.rs:64:24 + --> $DIR/async-fn-nonsend.rs:57:24 | LL | fn assert_send(_: impl Send) {} | ^^^^ required by this bound in `assert_send` error: future cannot be sent between threads safely - --> $DIR/async-fn-nonsend.rs:70:17 + --> $DIR/async-fn-nonsend.rs:63:17 | LL | assert_send(non_sync_with_method_call()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ future returned by `non_sync_with_method_call` is not `Send` @@ -40,7 +40,7 @@ LL | } LL | } | - `get_formatter()` is later dropped here note: required by a bound in `assert_send` - --> $DIR/async-fn-nonsend.rs:64:24 + --> $DIR/async-fn-nonsend.rs:57:24 | LL | fn assert_send(_: impl Send) {} | ^^^^ required by this bound in `assert_send` diff --git a/src/test/ui/async-await/drop-never.rs b/src/test/ui/async-await/drop-never.rs new file mode 100644 index 0000000000000..ac82b97df9ccd --- /dev/null +++ b/src/test/ui/async-await/drop-never.rs @@ -0,0 +1,17 @@ +// build-pass +// compile-flags: -Zdrop-tracking +// edition:2021 + +enum Never {} + +fn f() -> Never { todo!() } + +fn main() { + let _ = async { + let a = String::new(); + let b = f(); + async {}.await; + drop(a); + drop(b); + }; +} diff --git a/src/test/ui/async-await/issue-93161.rs b/src/test/ui/async-await/issue-93161.rs new file mode 100644 index 0000000000000..d2640d278270a --- /dev/null +++ b/src/test/ui/async-await/issue-93161.rs @@ -0,0 +1,26 @@ +// edition:2021 +// build-pass +// compile-flags: -Zdrop-tracking + +#![feature(never_type)] + +enum Never {} +fn never() -> Never { + panic!() +} + +async fn includes_never(crash: bool, x: u32) -> u32 { + let mut result = async { x * x }.await; + if !crash { + return result; + } + #[allow(unused)] + let bad = never(); + result *= async { x + x }.await; + drop(bad); + result +} + +fn main() { + let _ = includes_never(false, 4); +} diff --git a/src/test/ui/generator/issue-93161.rs b/src/test/ui/generator/issue-93161.rs deleted file mode 100644 index 9988acbcb73e1..0000000000000 --- a/src/test/ui/generator/issue-93161.rs +++ /dev/null @@ -1,93 +0,0 @@ -// edition:2021 -// run-pass - -#![feature(never_type)] - -use std::future::Future; - -// See if we can run a basic `async fn` -pub async fn foo(x: &u32, y: u32) -> u32 { - let y = &y; - let z = 9; - let z = &z; - let y = async { *y + *z }.await; - let a = 10; - let a = &a; - *x + y + *a -} - -async fn add(x: u32, y: u32) -> u32 { - let a = async { x + y }; - a.await -} - -async fn build_aggregate(a: u32, b: u32, c: u32, d: u32) -> u32 { - let x = (add(a, b).await, add(c, d).await); - x.0 + x.1 -} - -enum Never {} -fn never() -> Never { - panic!() -} - -async fn includes_never(crash: bool, x: u32) -> u32 { - let mut result = async { x * x }.await; - if !crash { - return result; - } - #[allow(unused)] - let bad = never(); - result *= async { x + x }.await; - drop(bad); - result -} - -async fn partial_init(x: u32) -> u32 { - #[allow(unreachable_code)] - let _x: (String, !) = (String::new(), return async { x + x }.await); -} - -async fn read_exact(_from: &mut &[u8], _to: &mut [u8]) -> Option<()> { - Some(()) -} - -async fn hello_world() { - let data = [0u8; 1]; - let mut reader = &data[..]; - - let mut marker = [0u8; 1]; - read_exact(&mut reader, &mut marker).await.unwrap(); -} - -fn run_fut(fut: impl Future) -> T { - use std::sync::Arc; - use std::task::{Context, Poll, Wake, Waker}; - - struct MyWaker; - impl Wake for MyWaker { - fn wake(self: Arc) { - unimplemented!() - } - } - - let waker = Waker::from(Arc::new(MyWaker)); - let mut context = Context::from_waker(&waker); - - let mut pinned = Box::pin(fut); - loop { - match pinned.as_mut().poll(&mut context) { - Poll::Pending => continue, - Poll::Ready(v) => return v, - } - } -} - -fn main() { - let x = 5; - assert_eq!(run_fut(foo(&x, 7)), 31); - assert_eq!(run_fut(build_aggregate(1, 2, 3, 4)), 10); - assert_eq!(run_fut(includes_never(false, 4)), 16); - assert_eq!(run_fut(partial_init(4)), 8); - run_fut(hello_world()); -}