Skip to content

Commit 86259e7

Browse files
authored
Rollup merge of #117389 - oli-obk:gen_fn, r=compiler-errors
Some diagnostics improvements of `gen` blocks These are leftovers from #116447
2 parents c299595 + 224ddf8 commit 86259e7

File tree

9 files changed

+49
-10
lines changed

9 files changed

+49
-10
lines changed

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

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
}

compiler/rustc_parse/messages.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ parse_found_expr_would_be_stmt = expected expression, found `{$token}`
278278
parse_function_body_equals_expr = function body cannot be `= expression;`
279279
.suggestion = surround the expression with `{"{"}` and `{"}"}` instead of `=` and `;`
280280
281-
parse_gen_block = `gen` blocks are not yet implemented
282-
.help = only the keyword is reserved for now
281+
parse_gen_fn = `gen` functions are not yet implemented
282+
.help = for now you can use `gen {"{}"}` blocks and return `impl Iterator` instead
283283
284284
parse_generic_args_in_pat_require_turbofish_syntax = generic args in patterns require the turbofish syntax
285285

compiler/rustc_parse/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,9 @@ pub(crate) struct CatchAfterTry {
521521
}
522522

523523
#[derive(Diagnostic)]
524-
#[diag(parse_gen_block)]
524+
#[diag(parse_gen_fn)]
525525
#[help]
526-
pub(crate) struct GenBlock {
526+
pub(crate) struct GenFn {
527527
#[primary_span]
528528
pub span: Span,
529529
}

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2372,7 +2372,7 @@ impl<'a> Parser<'a> {
23722372
}
23732373

23742374
if let Gen::Yes { span, .. } = genness {
2375-
self.sess.emit_err(errors::GenBlock { span });
2375+
self.sess.emit_err(errors::GenFn { span });
23762376
}
23772377

23782378
if !self.eat_keyword_case(kw::Fn, case) {

tests/ui/coroutine/gen_block_panic.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//compile-flags: --edition 2024 -Zunstable-options
2+
// run-pass
3+
// needs-unwind
4+
#![feature(gen_blocks)]
5+
6+
fn main() {
7+
let mut iter = gen {
8+
yield 42;
9+
panic!("foo");
10+
yield 69; //~ WARN: unreachable statement
11+
};
12+
assert_eq!(iter.next(), Some(42));
13+
let mut tmp = std::panic::AssertUnwindSafe(&mut iter);
14+
match std::panic::catch_unwind(move || tmp.next()) {
15+
Ok(_) => unreachable!(),
16+
Err(err) => assert_eq!(*err.downcast::<&'static str>().unwrap(), "foo"),
17+
}
18+
19+
match std::panic::catch_unwind(move || iter.next()) {
20+
Ok(_) => unreachable!(),
21+
Err(err) => assert_eq!(
22+
*err.downcast::<&'static str>().unwrap(),
23+
"`gen fn` should just keep returning `None` after panicking",
24+
),
25+
}
26+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
warning: unreachable statement
2+
--> $DIR/gen_block_panic.rs:10: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+
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: `gen` blocks are not yet implemented
1+
error: `gen` functions are not yet implemented
22
--> $DIR/gen_fn.rs:4:1
33
|
44
LL | gen fn foo() {}
55
| ^^^
66
|
7-
= help: only the keyword is reserved for now
7+
= help: for now you can use `gen {}` blocks and return `impl Iterator` instead
88

99
error: aborting due to previous error
1010

tests/ui/coroutine/gen_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
gen fn foo() {}
55
//[none]~^ ERROR: expected one of `#`, `async`, `const`, `default`, `extern`, `fn`, `pub`, `unsafe`, or `use`, found `gen`
6-
//[e2024]~^^ ERROR: `gen` blocks are not yet implemented
6+
//[e2024]~^^ ERROR: `gen` functions are not yet implemented
77

88
fn main() {}

0 commit comments

Comments
 (0)