-
Notifications
You must be signed in to change notification settings - Fork 14k
Open
Labels
A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsC-bugCategory: This is a bug.Category: This is a bug.P-lowLow priorityLow priorityS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.Performance or correctness regression from one stable version to another.
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
trueto some runtime-only condition. - Swapping the two branches.
- Replacing
if-elsewithmatch. - 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
u8or 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.
Metadata
Metadata
Assignees
Labels
A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsC-bugCategory: This is a bug.Category: This is a bug.P-lowLow priorityLow priorityS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.Performance or correctness regression from one stable version to another.