-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trait impl from where clause (ParamEnv
) takes precedence over freestanding trait impl
#84218
Comments
here is a minimal reproduction of the issue use std::ops::Shr;
fn works() { 1_u64 >> 2_u32; }
fn breaks<T>()
where
u64: Shr<T>
{
1_u64 >> 2_u32;
} It looks like the compiler unnecessarily restricts to the More strange scenarios (as of use std::ops::Shr;
fn works() { 1_u64 >> 2_u32; }
fn breaks<T>() where u64: Shr<T> { 1_u64 >> 2_u32; }
fn breaks_too<T>() where u64: Shr<u32> + Shr<T> { 1_u64 >> 2_u32; }
fn breaks_as_well<T>() where u64: Shr<u32> + Shr<T> { Shr::<_>::shr(1_u64, 2_u32); } // Should be infered to `Shr::<u32>` from my understanding, but doesn't seem to be the case
fn works_though<T>() where u64: Shr<T> { Shr::<u32>::shr(1_u64, 2_u32); }
fn works_too<T>() where u64: Shr<u32> + Shr<T> { Shr::<u32>::shr(1_u64, 2_u32); }
fn works_more_surprisingly<T, U>()
where
u64: Shr<T> + Shr<U> // An additional bound on another generic parameter seems to fix the resolution ??
{
1_u64 >> 2_u32;
}
fn works_more_surprisingly_in_full_form_too<T, U>()
where
u64: Shr<T> + Shr<U>
{
Shr::<_>::shr(1_u64, 2_u32);
} |
I think this is currently working as expected, since trait bounds on where clauses have precedence over freestanding trait implementations |
I feel like that does not explain the last two cases of the last snippet above though, where having two trait bounds instead of one suddenly "re-enables" the freestanding trait implementation |
Probably duplicate of #24066. |
Closing as duplicate of #24066. |
ParamEnv
) takes precedence over freestanding trait impl
Hi!
Sorry for the poor title, I have no better idea. Feel free to change it to something more appropriate.
I tried this code:
which I expect to compile fine. But instead, I get the typing error:
(playground link)
Meta
(afaict this is present in stable, beta and nightly)
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: