rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Semicolon after a return statement breaks code with try blocks and never type

Open kryptan opened this issue 7 months ago • 0 comments

Consider this code:

#![feature(never_type, try_blocks)]

fn main() {
    let _: Result<!, ()> = try {
        ();
        return
    };
}

It compiles.

Rustfmt adds a semicolon here:

#![feature(never_type, try_blocks)]

fn main() {
    let _: Result<!, ()> = try {
        ();
        return;
    };
}

This causes a compilation error:

   Compiling playground v0.0.1 (/playground)
error[E0271]: type mismatch resolving `<Result<!, ()> as Try>::Output == ()`
 --> src/main.rs:7:5
  |
7 |     };
  |     ^ expected `()`, found `!`
  |
  = note: expected unit type `()`
                  found type `!`

For more information about this error, try `rustc --explain E0271`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Playground link

Previously reported here.

Meta

Nightly channel

Build using the Nightly version: 1.88.0-nightly

(2025-04-26 10fa3c449f6b1613b352)

kryptan avatar Apr 30 '25 11:04 kryptan