Very strange error when calling methods with multi-parameter bounds
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
use std::collections::HashSet;
pub struct AABB {}
pub trait Intersects<T, C> {
fn intersects(&self, x: &T, ctx: &C) -> bool;
}
impl<C> Intersects<AABB, C> for AABB {
fn intersects(&self, _x: &AABB, _ctx: &C) -> bool {
true
}
}
pub struct QuadTree<T> {
pub x: Option<T>,
}
impl<T> QuadTree<T> {
pub fn find_intersects<U, C>(&self, _search: &U, _ctx: &C) -> HashSet<T>
where
AABB: Intersects<U, C>,
U: Intersects<T, C>,
{
// ...
HashSet::new()
}
pub fn find_closest<C>(&self, _to: (f64, f64), ctx: &C) -> Option<T>
where
AABB: Intersects<T, C>
// UNCOMMENT THIS TO FIX
// + Intersects<AABB, C>
,
{
let rect = AABB {};
self.find_intersects(&rect, ctx).into_iter().next()
}
}
fn main() {
println!("Hello world");
}
Current output
error[E0277]: the trait bound `T: Intersects<T, C>` is not satisfied
--> bug.rs:29:30
|
29 | self.find_intersects(&rect, ctx).into_iter().next()
| --------------- ^^^^^ the trait `Intersects<T, C>` is not implemented for `T`
| |
| required by a bound introduced by this call
|
note: required by a bound in `QuadTree::<T>::find_intersects`
--> bug.rs:17:12
|
14 | pub fn find_intersects<U, C>(&self, _search: &U, _ctx: &C) -> HashSet<T>
| --------------- required by a bound in this associated function
...
17 | U: Intersects<T, C>,
| ^^^^^^^^^^^^^^^^ required by this bound in `QuadTree::<T>::find_intersects`
help: consider restricting type parameter `T`
|
13 | impl<T: Intersects<T, C>> QuadTree<T> {
| ++++++++++++++++++
error[E0308]: mismatched types
--> bug.rs:29:30
|
13 | impl<T> QuadTree<T> {
| - expected this type parameter
...
29 | self.find_intersects(&rect, ctx).into_iter().next()
| --------------- ^^^^^ expected `&T`, found `&AABB`
| |
| arguments to this method are incorrect
|
= note: expected reference `&T`
found reference `&AABB`
note: method defined here
--> bug.rs:14:12
|
14 | pub fn find_intersects<U, C>(&self, _search: &U, _ctx: &C) -> HashSet<T>
| ^^^^^^^^^^^^^^^ -----------
error: aborting due to 2 previous errors
Desired output
Not sure exactly. See rationale.
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 == Tdoes not make sense? Where is that coming from? Shouldn't the error tell you that you can't pass anAABBbecauseIntersects<AABB, C>is not satisfied and thereforeUcan'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
rustc 1.77.2 (25ef9e3d8 2024-04-09) (built from a source tarball)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-unknown-linux-gnu
release: 1.77.2
LLVM version: 17.0.6
Anything else?
Run rustc bug.rs to try.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run rustc bug.rs with the reported Rust version and compare the two diagnostics with the trait bounds in the reproducer. Trace how rustc chooses the suggested restriction and improve the diagnostic so it identifies the actual failing bound or gives an accurate fix; verify the resulting output against this example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 32/100