Skip to content

Bad interaction between never_type, try_blocks, and From/Into #125364

Open
@ia0

Description

@ia0

The following code does not compile but the 2 commented alternatives compile:

#![feature(never_type)]
#![feature(try_blocks)]

fn bar(_: Result<impl Into<!>, u32>) {
// fn bar(_: Result<impl Into<()>, u32>) {
// fn bar(_: Result<!, u32>) {
    unimplemented!()
}

pub fn foo(x: Result<!, u32>) {
    bar(try { x? });
}

The error is:

error[E0277]: the trait bound `!: From<()>` is not satisfied
  --> src/lib.rs:11:9
   |
11 |     bar(try { x? });
   |     --- ^^^^^^--^^
   |     |   |     |
   |     |   |     this tail expression is of type `Result<(), u32>`
   |     |   the trait `From<()>` is not implemented for `!`, which is required by `(): Into<!>`
   |     required by a bound introduced by this call
   |
   = note: this error might have been caused by changes to Rust's type-inference algorithm (see issue #48950 <https://github.com/rust-lang/rust/issues/48950> for more information)
   = help: did you intend to use the type `()` here instead?
   = note: required for `()` to implement `Into<!>`

For some reason, rustc believes that the try block should return () instead of !.

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (7d83a4c13 2024-05-06)
binary: rustc
commit-hash: 7d83a4c131ab9ae81a74c6fd825c827d74a2881d
commit-date: 2024-05-06
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-never_type`#![feature(never_type)]`F-try_blocks`#![feature(try_blocks)]`S-has-mcveStatus: 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.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions