Skip to content

((), !) and ((), ()) are considered incompatible types #112856

Open
@GrigorenkoPV

Description

@GrigorenkoPV

Playground

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 with match.
  • Swapping todo!() and () inside of the tuple.
  • Replacing todo!() with loop{} or std::process::exit(0).
  • Replacing todo!() with a call to a function returning std::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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-never_type`#![feature(never_type)]`P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions