Adding non-divergent core::ops overload breaks type inference and default integer type preference #98357
Labels
A-inference
Area: Type inference
C-bug
Category: This is a bug.
needs-triage
This issue may need triage. Remove it if it has been sufficiently triaged.
This code compiles fine:
Adding an
impl Mul<Foo> for i8
(or any other integer type) breaks the type inference happening on the first line ofmain()
(playground link):The stable (1.61.0) compiler gives a better message about what's really happening here as compared to the beta or nightly (1.62.0, 1.63.0) versions. Stable prints this:
The outputs of all the
Mul
impl blocks are non-diverging and produceFoo
for all inputs. Theoretically, specifyinglet prod1: Foo = ...
instead oflet prod1 = ...
would do nothing to ameliorate the situation (since either i8 or i32 impl could satisfy the code as written, even with the additional type constraint). However, simply supplying theprod1: Foo
type specifier fixes the inference and lets rust's default integer preference succeed: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=bf76c0fa2edf27dc656a7bc07ecaab12Note that this doesn't happen for the commutative equivalent (
Foo * numeric
rather thannumeric * Foo
) with the correctMul
impls in place.rust seems to know that the output is
Foo
in all cases, because the following emits an error with the correct type:So there's no reason why
: Foo
needs to be specified explicitly.Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: