Skip to content

[Drop Tracking] Allow uninhabited types in generator interiors #94752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions compiler/rustc_mir_transform/src/generator.rs
Original file line number Diff line number Diff line change
@@ -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)| {
Comment on lines +743 to +745
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this validates a complete generator, we don't really know which parts of it are unreachable if any.

If we want to optimize out code following call with uninhabited type as unreachable, we should do that by transforming the MIR and ensuring those locals are not saved in the first place, while keeping the validation in place to ensure that typeck and MIR are in sync.

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
);
}
}
}
}
8 changes: 0 additions & 8 deletions src/test/ui/async-await/async-fn-nonsend.rs
Original file line number Diff line number Diff line change
@@ -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());
}
8 changes: 4 additions & 4 deletions src/test/ui/async-await/async-fn-nonsend.stderr
Original file line number Diff line number Diff line change
@@ -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`
17 changes: 17 additions & 0 deletions src/test/ui/async-await/drop-never.rs
Original file line number Diff line number Diff line change
@@ -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);
};
}
26 changes: 26 additions & 0 deletions src/test/ui/async-await/issue-93161.rs
Original file line number Diff line number Diff line change
@@ -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);
}
93 changes: 0 additions & 93 deletions src/test/ui/generator/issue-93161.rs

This file was deleted.