Open
Description
Originally mentioned in this comment: #96373 (comment)
Essentially, semicolons after other diverging expressions are allowed. For example:
fn works() -> Result<bool, i32> {
return Err(1);
}
Here, the compiler is clever enough to note that because return Err(1)
always diverges, adding a semicolon after doesn't matter. However, the below code which is equivalent to this does not work properly:
fn breaks() -> Result<bool, i32> {
Err(1)?;
}
Here's a playground link containing the above two examples: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=af894bab48348897a421a9402856b71f
The exact compiler error:
error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
--> src/lib.rs:5:16
|
5 | fn breaks() -> Result<bool, i32> {
| ------ ^^^^^^^^^^^^^^^^^ expected enum `Result`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
6 | Err(1)?;
| - help: remove this semicolon to return this value
|
= note: expected enum `Result<bool, i32>`
found unit type `()`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error