Skip to content

Commit

Permalink
Make "remove semicolon" suggestion work with !
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Apr 8, 2024
1 parent 6678f63 commit 6ccb844
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_infer/src/infer/error_reporting/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let last_expr_ty = self.typeck_results.as_ref()?.expr_ty_opt(last_expr)?;
let needs_box = match (last_expr_ty.kind(), expected_ty.kind()) {
_ if last_expr_ty.references_error() => return None,
(ty::Never, _) => StatementAsExpression::CorrectType,
_ if self.same_type_modulo_infer(last_expr_ty, expected_ty) => {
StatementAsExpression::CorrectType
}
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/editions/diverging-block.e2024.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//@ revisions: e2021 e2024
//
//@[e2021] edition: 2021
//@[e2024] edition: 2024
//@[e2024] compile-flags: -Zunstable-options
//
//@[e2021] check-pass
//@[e2024] check-fail
//
//@[e2024] run-rustfix
#![crate_name = "diverging_block"] // or else rustfix dies because of dots in the crate name


fn main() {
// a diverging block, with no tail expression.
//
// edition <= 2021: the block has type `!`, which then can be coerced.
// edition >= 2024: the block has type `()`, as with any block with no tail.
let _: u32 = { //[e2024]~ error: mismatched types
return
};
}

fn _f() {
// Same as the above, but with an if
if true {
return
} else {
0_u32 //[e2024]~ error: `if` and `else` have incompatible types
};
}
10 changes: 7 additions & 3 deletions tests/ui/editions/diverging-block.e2024.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
error[E0308]: mismatched types
--> $DIR/diverging-block.rs:15:18
--> $DIR/diverging-block.rs:19:18
|
LL | let _: u32 = {
| __________________^
LL | | return;
| | - help: remove this semicolon to return this value
LL | | };
| |_____^ expected `u32`, found `()`

error[E0308]: `if` and `else` have incompatible types
--> $DIR/diverging-block.rs:25:9
--> $DIR/diverging-block.rs:29:9
|
LL | / if true {
LL | | return;
| | ------- expected because of this
| | -------
| | | |
| | | help: consider removing this semicolon
| | expected because of this
LL | | } else {
LL | | 0_u32
| | ^^^^^ expected `()`, found `u32`
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/editions/diverging-block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
//
//@[e2021] check-pass
//@[e2024] check-fail
//
//@[e2024] run-rustfix
#![crate_name = "diverging_block"] // or else rustfix dies because of dots in the crate name


fn main() {
// a diverging block, with no tail expression.
Expand Down

0 comments on commit 6ccb844

Please sign in to comment.