Skip to content

Mistaken method shadowing by constrained impl #7856

@Tamschi

Description

@Tamschi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tytype system / type inference / traits / method resolutionS-actionableSomeone could pick this issue up and work on it right now

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions