rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Mistaken method shadowing by constrained `impl`

Open Tamschi opened this issue 4 years ago • 0 comments

Consider the following code:

use std::marker::PhantomData;

struct A<T>(PhantomData<T>);
impl<T> A<T> {
    fn general(self) {
        self.overloaded() // <--
    }
}
impl<T: Sync> A<T> {
    fn overloaded(self) {}
}
trait Overload: Sized {
    fn overloaded(self) {}
}
impl<T> Overload for A<T> {}

At the location marked with <--, T is not constrained to Sync and as such the call resolves to the Overload implementation. However, rust-analyzer shows the information for A::overloaded(self) instead.

A similar case occurs when using by-value precedence to select a method between two traits.
use std::marker::PhantomData;

struct A<T>(PhantomData<T>);
trait Bound {
    fn either(&self) -> usize {
        todo!()
    }
}
trait Safe: Sized {
    fn either(self) -> u8 {
        todo!()
    }
}
impl<T> Bound for A<T> {}
impl<T: Bound + Send + Sync> Safe for T {}

fn issue() {
    let bound = A::<*const ()>(PhantomData).either(); // <--
    let _: usize = bound;
}

The type of bound is shown as u8 here: image

Tamschi avatar Mar 03 '21 04:03 Tamschi