You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrong infered type when an additional constrain added #64681
For posterity, here's the error where it inferred S instead of bool:
error[E0308]: mismatched types
--> src/lib.rs:9:5
|
4 | fn f<S, R>(rng: &mut R) -> bool
| ---- expected `bool` because of return type
...
9 | rng.gen()
| ^^^^^^^^^ expected bool, found type parameter
|
= note: expected type `bool`
found type `S`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
If f's constraint is changed to Standard: Distribution<bool>, it infers bool just fine.
I also tried Distribution<S> + Distribution<bool> and vice versa, but either way infers S.
Results in (on 1.42.0-nightly (2019-12-29 da3629b05f8f1b425a73)):
error[E0308]: mismatched types
--> src/lib.rs:16:27
|
11 | pub fn add_1_foo<T>(y: T) -> Foo
| - this type parameter
...
16 | let foo_1 = Foo::from(1);
| ^ expected type parameter `T`, found integer
|
= note: expected type parameter `T`
found type `{integer}`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
It seems like since the Foo: From<T> constraint is there, it expects all uses of Foo::from to be on a T. But T has to be equal to i32, and it still doesn't work. Also, I can't figure out any way to make add_1_foo compile just by adding type annotations. Changing the Foo::from(1) line to let foo_1 : Foo = 1.into(); works, but that just avoids calling from.
Activity
cuviper commentedon Sep 23, 2019
For posterity, here's the error where it inferred
Sinstead ofbool:If
f's constraint is changed toStandard: Distribution<bool>, it infersbooljust fine.I also tried
Distribution<S> + Distribution<bool>and vice versa, but either way infersS.orangeturtle739 commentedon Jan 5, 2020
Edit: minimized the example even more:
Here's another example: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=bcd9a0d9507ac1e42ec7b0ce9f7a5f42
Results in (on
1.42.0-nightly (2019-12-29 da3629b05f8f1b425a73)):It seems like since the
Foo: From<T>constraint is there, it expects all uses ofFoo::fromto be on aT. ButThas to be equal toi32, and it still doesn't work. Also, I can't figure out any way to makeadd_1_foocompile just by adding type annotations. Changing theFoo::from(1)line tolet foo_1 : Foo = 1.into();works, but that just avoids calling from.installgentoo commentedon Jan 17, 2020
Just encountered this exact bug.
lcnr commentedon Aug 1, 2025
Closing as duplicate of #24066
ParamEnv) take precedence over freestanding trait impls #24066