Skip to content

Commit bc926f7

Browse files
committed
Add a custom panic message for resuming gen blocks after they panicked
1 parent 745c600 commit bc926f7

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

Diff for: compiler/rustc_middle/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ middle_assert_coroutine_resume_after_return = coroutine resumed after completion
1212
middle_assert_divide_by_zero =
1313
attempt to divide `{$val}` by zero
1414
15+
middle_assert_gen_resume_after_panic = `gen` fn or block cannot be further iterated on after it panicked
16+
1517
middle_assert_misaligned_ptr_deref =
1618
misaligned pointer dereference: address must be a multiple of {$required} but is {$found}
1719

Diff for: compiler/rustc_middle/src/mir/terminator.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ impl<O> AssertKind<O> {
250250
middle_assert_coroutine_resume_after_return
251251
}
252252
ResumedAfterPanic(CoroutineKind::Async(_)) => middle_assert_async_resume_after_panic,
253-
// FIXME(gen_blocks): custom error message for `gen` blocks
254-
ResumedAfterPanic(CoroutineKind::Gen(_)) => middle_assert_async_resume_after_panic,
253+
ResumedAfterPanic(CoroutineKind::Gen(_)) => middle_assert_gen_resume_after_panic,
255254
ResumedAfterPanic(CoroutineKind::Coroutine) => {
256255
middle_assert_coroutine_resume_after_panic
257256
}

Diff for: tests/ui/coroutine/gen_block_panic.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//compile-flags: --edition 2024 -Zunstable-options
2+
// run-pass
3+
#![feature(gen_blocks)]
4+
5+
fn main() {
6+
let mut iter = gen {
7+
yield 42;
8+
panic!("foo");
9+
yield 69; //~ WARN: unreachable statement
10+
};
11+
assert_eq!(iter.next(), Some(42));
12+
let mut tmp = std::panic::AssertUnwindSafe(&mut iter);
13+
match std::panic::catch_unwind(move || tmp.next()) {
14+
Ok(_) => unreachable!(),
15+
Err(err) => assert_eq!(*err.downcast::<&'static str>().unwrap(), "foo"),
16+
}
17+
18+
match std::panic::catch_unwind(move || iter.next()) {
19+
Ok(_) => unreachable!(),
20+
Err(err) => assert_eq!(
21+
*err.downcast::<&'static str>().unwrap(),
22+
"`gen fn` should just keep returning `None` after panicking",
23+
),
24+
}
25+
}

Diff for: tests/ui/coroutine/gen_block_panic.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
warning: unreachable statement
2+
--> $DIR/gen_block_panic.rs:9:9
3+
|
4+
LL | panic!("foo");
5+
| ------------- any code following this expression is unreachable
6+
LL | yield 69;
7+
| ^^^^^^^^^ unreachable statement
8+
|
9+
= note: `#[warn(unreachable_code)]` on by default
10+
11+
warning: 1 warning emitted
12+

0 commit comments

Comments
 (0)