Description
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