Skip to content

Inconsistent method lookup on never type #143349

Open
@purplesyringa

Description

@purplesyringa

I tried this code:

#![feature(never_type)]

fn produces_never_type() -> ! {
    loop {}
}

trait Trait: Sized {
    fn trait_method(self) {}
}
impl Trait for ! {}

fn f() {
    // Compiles:
    produces_never_type().trait_method();
    (produces_never_type()).trait_method();
    { produces_never_type(); }.trait_method();
    {{ produces_never_type() }; }.trait_method();

    // Doesn't compile:
    { produces_never_type() }.trait_method();
}

I'm using the never_type feature for simplicity, but this reproduces on stable with never-say-never.

I expected to see this happen: all 5 calls compile.

Instead, this happened: the last one errors out:

error[E0282]: type annotations needed
  --> src/lib.rs:20:5
   |
20 |     { produces_never_type() }.trait_method();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type

This happens both on edition 2021 and edition 2024. It doesn't happen if ! is replaced with any other type.

Whether this should work at all is... questionable, personally I found it surprising that Trait::trait_method(produces_never_type()) causes never type fallback (and if rewritten like so, all 5 snippets fail to compile on edition 2021), while .trait_method() applies before fallback. Though that looks consistent with 1.abs() and such.

Meta

rustc --version --verbose:

rustc 1.90.0-nightly (71e4c005c 2025-07-01)
binary: rustc
commit-hash: 71e4c005caa812a16fcb08d0bf1e6f1eda7c8381
commit-date: 2025-07-01
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

@rustbot label +A-method-lookup +T-compiler +T-types -needs-triage

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-method-lookupArea: method lookup (typeck, post `rustc_resolve`, not to be confused with `A-resolve`)C-bugCategory: This is a bug.F-never_type`#![feature(never_type)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.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