rust-lang / rust-lang/rust

Never type fallback change breaks code casting a function to `fn() -> _` when the inference variable is equated with a diverging variable

Open
#135,864 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coercions A-inference C-bug F-never_type T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

fn foo() -> Result<(), u8> {
    Ok(())
}

fn test() -> Result<(), u8> {
    let f: fn() -> _ = foo as _;

    f()?;

    Ok(())
}

I expected to see this happen:

Type inference on f should work correctly.

Instead, this happened:

When switching to 2024 edition something odd is happening and inference results in confusing compilation error:

error[E0271]: type mismatch resolving `<Result<(), u8> as Try>::Output == !`
 --> src/lib.rs:7:5
  |
7 |     f()?;
  |     ^^^^ expected `!`, found `()`
  |
  = note:   expected type `!`
          found unit type `()`

The same code works fine on 2021 edition and I don't understand why 2024 breaks.

Replacing -> _ with explicit -> Result<(), u8> helps, but it is too verbose and hurts readability.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=032415d516c4ed57e397ce6e0b64c35e

Meta

rustc --version --verbose:

1.86.0-nightly

(2025-01-21 ed43cbcb882e7c06870a)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Rust Playground reproducer and compare the 2021 and 2024 edition results using the reported rustc nightly version. Trace never-type fallback and function-pointer inference around let f: fn() -> _ = foo as _ and f()?; done when the 2024 example compiles like 2021 without an explicit return type.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.