Skip to content

Combination of try blocks, never type, and rustfmt is broken #140361

Closed as not planned
@kryptan

Description

@kryptan

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

I'm not sure if rustfmt shouldn't add a semicolon here or if rustc should accept a semicolon.

Playground link

Meta

Nightly channel

Build using the Nightly version: 1.88.0-nightly

(2025-04-26 10fa3c449f6b1613b352)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-rustfmtArea: RustfmtC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions