Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ia0 opened this issue May 21, 2024 · 2 comments
Open

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

ia0 opened this issue May 21, 2024 · 2 comments
Labels
C-bug Category: This is a bug. F-never_type `#![feature(never_type)]` F-try_blocks `#![feature(try_blocks)]` S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@ia0
Copy link
Contributor

ia0 commented May 21, 2024

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
@ia0 ia0 added the C-bug Category: This is a bug. label May 21, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 21, 2024
@jieyouxu jieyouxu added T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. F-never_type `#![feature(never_type)]` F-try_blocks `#![feature(try_blocks)]` S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 22, 2024
@WaffleLapkin
Copy link
Member

WaffleLapkin commented May 26, 2024

This is an issue with the "never type fallback".

try { x? } allows rust to coerce the ! that is un- and re-wrapped, so the type of this is basically Result<?0t, u32> (where ?0t is a variable, signifying that compiler doesn't yet know the type). Then you require that ?0t implements impl Into<!>. Any number of types can implement Into<!>, so the type here is not really inferable. Rustc currently uses () as the default type in cases like this, so you get an error.

In the 2024 edition (and possibly in all editions in a later rustc version) we plan to change that which would fix your example. (although I would suggest just not using Into<!>, if possible)

@ia0
Copy link
Contributor Author

ia0 commented May 26, 2024

Thanks for the explanation! I'll just wait for the 2024 edition then. (Regarding not using Into<!>, that's not simple because this happens in generic code where I actually have Into<T> and the problem occurs only when T is instantiated with !.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-never_type `#![feature(never_type)]` F-try_blocks `#![feature(try_blocks)]` S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants