Skip to content

Result type inference breaks in 2024 edition, while working correctly in 2021 edition #135864

Open
@nazar-pc

Description

@nazar-pc

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=7f1158511f3560d231db70cb839340c4

Meta

rustc --version --verbose:

1.86.0-nightly

(2025-01-21 ed43cbcb882e7c06870a)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-inferenceArea: Type inferenceC-bugCategory: This is a bug.F-never_type`#![feature(never_type)]`T-typesRelevant to the types 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