Open
Description
I tried this code:
fn main() {
let a = if true {
((), todo!())
} else {
((), ())
};
}
I expected to see this happen:
Code compiles, runs, and panics.
a
has type ((), ())
.
Instead, this happened:
Compiling example v0.1.0 (/tmp/exmaple)
warning: unreachable expression
--> src/main.rs:3:9
|
3 | ((), todo!())
| ^^^^^-------^
| | |
| | any code following this expression is unreachable
| unreachable expression
|
= note: `#[warn(unreachable_code)]` on by default
error[E0308]: `if` and `else` have incompatible types
--> src/main.rs:5:9
|
2 | let a = if true {
| _____________-
3 | | ((), todo!())
| | ------------- expected because of this
4 | | } else {
5 | | ((), ())
| | ^^^^^^^^ expected `((), !)`, found `((), ())`
6 | | };
| |_____- `if` and `else` have incompatible types
|
= note: expected tuple `((), !)`
found tuple `((), ())`
For more information about this error, try `rustc --explain E0308`.
warning: `example` (bin "example") generated 1 warning
error: could not compile `example` (bin "example") due to previous error; 1 warning emitted
The following changes have no effect:
- Removing
let a =
. - Changing
true
to some runtime-only condition. - Swapping the two branches.
- Replacing
if
-else
withmatch
. - Swapping
todo!()
and()
inside of the tuple. - Replacing
todo!()
withloop{}
orstd::process::exit(0)
. - Replacing
todo!()
with a call to a function returningstd::convert::Infallible
(though this makes the warning about the unreachable code go away). - Using
u8
or any other "normal" type instead of()
.
The following changes make the error go away.
let x = todo!();
((), x)
fn foo() -> () { todo!() }
((), foo())
let foo = || todo!();
((), foo())
((), (||todo!())())
Meta
rustc --version --verbose
:
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: x86_64-unknown-linux-gnu
release: 1.70.0
LLVM version: 16.0.2
Same on nightly and beta.
Must be related to #35121.