Open arximboldi opened 3 months ago
Minimized:
pub struct Dummy;
pub struct Thing;
pub trait SomeTrait<T> {}
impl SomeTrait<Thing> for Dummy {}
pub fn foo<T>(_x: T)
where
Dummy: SomeTrait<T>,
{
}
pub fn what<T>()
where
// Comment below to fix
Dummy: SomeTrait<T>,
// Uncomment below to fix
// Dummy: SomeTrait<Thing>,
{
foo(Thing);
// Comment above and uncomment below to fix
// foo::<Thing>(Thing);
}
It seems that the irrelevant Dummy: SomeTrait<T>
is causing rust to incorrectly infer that the only possible foo
call is foo::<T>()
, missing the alternative possibility of foo::<Thing>()
.
Explicitly annotating the type of the call makes the code compile. (In the original code, this would be self.find_intersects::<AABB, C>(&rect, ctx).into_iter().next()
)
I think this happens because when proving the bounds of the function, for the Dummy: SomeTrait<?0>
bound, it will solve it using the candidate from the where clause Dummy: SomeTrait<T>
, which is then selected as where clause candidates take priority over the impl, causing the generic args to be unified, constraining ?0
(the generic arg inference variable) to T
.
It seems that the irrelevant
Dummy: SomeTrait<T>
is causing rust to incorrectly infer that the only possiblefoo
call isfoo::<T>()
, missing the alternative possibility offoo::<Thing>()
.
Note that in the original code that trait is not irrelevant. The usage was omitted to make the submission simpler. In any case thanks for the minimization!
I think @Nilstrieb is pointing in the right direction as to why it happens.
Code
Current output
Desired output
Rationale and extra context
Why is this even an error? There is an implementation that satisfies the bound already visible, shouldn't the line that fixes the error be redundant? (See comment:
UNCOMMENT THIS TO FIX
)Even in that case, am I wrong to assume that the deduction that
U == T
does not make sense? Where is that coming from? Shouldn't the error tell you that you can't pass anAABB
becauseIntersects<AABB, C>
is not satisfied and thereforeU
can't beAABB
, and suggest to make the correct change that fixes it straight away, instead of the very misleading suggestion which, in fact, does not fix the issue?Other cases
No response
Rust Version
Anything else?
Run
rustc bug.rs
to try.