Semicolon after a return statement breaks code with try blocks and never type
Open
Nobody has claimed this yet.
C-bug
- Dominant language
- Rust
- Stars
- 7k
- Forks
- 1.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 24
Description
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
Previously reported here.
Meta
Nightly channel
Build using the Nightly version: 1.88.0-nightly
(2025-04-26 10fa3c449f6b1613b352)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the two snippets from the issue with the linked Rust Playground and compare rustfmt's output with rustc's result. The payload names no repository file or test entry point, so locate the formatting path and existing try-block tests in rustfmt. Done means formatting this case no longer changes its compilation behavior, with a regression test covering the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100