Skip to content

Commit c39e26c

Browse files
committed
Make "remove semicolon" suggestion work with !
1 parent 6678f63 commit c39e26c

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

compiler/rustc_infer/src/infer/error_reporting/suggest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
714714
let last_expr_ty = self.typeck_results.as_ref()?.expr_ty_opt(last_expr)?;
715715
let needs_box = match (last_expr_ty.kind(), expected_ty.kind()) {
716716
_ if last_expr_ty.references_error() => return None,
717+
(ty::Never, _) => StatementAsExpression::CorrectType,
717718
_ if self.same_type_modulo_infer(last_expr_ty, expected_ty) => {
718719
StatementAsExpression::CorrectType
719720
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ revisions: e2021 e2024
2+
//
3+
//@[e2021] edition: 2021
4+
//@[e2024] edition: 2024
5+
//@[e2024] compile-flags: -Zunstable-options
6+
//
7+
//@[e2021] check-pass
8+
//@[e2024] check-fail
9+
//
10+
//@[e2024] run-rustfix
11+
12+
13+
fn main() {
14+
// a diverging block, with no tail expression.
15+
//
16+
// edition <= 2021: the block has type `!`, which then can be coerced.
17+
// edition >= 2024: the block has type `()`, as with any block with no tail.
18+
let _: u32 = { //[e2024]~ error: mismatched types
19+
return
20+
};
21+
}
22+
23+
fn _f() {
24+
// Same as the above, but with an if
25+
if true {
26+
return
27+
} else {
28+
0_u32 //[e2024]~ error: `if` and `else` have incompatible types
29+
};
30+
}

tests/ui/editions/diverging-block.e2024.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
error[E0308]: mismatched types
2-
--> $DIR/diverging-block.rs:15:18
2+
--> $DIR/diverging-block.rs:18:18
33
|
44
LL | let _: u32 = {
55
| __________________^
66
LL | | return;
7+
| | - help: remove this semicolon to return this value
78
LL | | };
89
| |_____^ expected `u32`, found `()`
910

1011
error[E0308]: `if` and `else` have incompatible types
11-
--> $DIR/diverging-block.rs:25:9
12+
--> $DIR/diverging-block.rs:28:9
1213
|
1314
LL | / if true {
1415
LL | | return;
15-
| | ------- expected because of this
16+
| | -------
17+
| | | |
18+
| | | help: consider removing this semicolon
19+
| | expected because of this
1620
LL | | } else {
1721
LL | | 0_u32
1822
| | ^^^^^ expected `()`, found `u32`

tests/ui/editions/diverging-block.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
//
77
//@[e2021] check-pass
88
//@[e2024] check-fail
9+
//
10+
//@[e2024] run-rustfix
11+
912

1013
fn main() {
1114
// a diverging block, with no tail expression.

0 commit comments

Comments
 (0)