Skip to content
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

Mistaken method shadowing by constrained impl #7856

Open
Tamschi opened this issue Mar 3, 2021 · 0 comments · Fixed by #13112
Open

Mistaken method shadowing by constrained impl #7856

Tamschi opened this issue Mar 3, 2021 · 0 comments · Fixed by #13112
Labels
A-ty type system / type inference / traits / method resolution S-actionable Someone could pick this issue up and work on it right now

Comments

@Tamschi
Copy link

Tamschi commented Mar 3, 2021

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

@flodiebold flodiebold added A-ty type system / type inference / traits / method resolution S-actionable Someone could pick this issue up and work on it right now labels Mar 3, 2021
bors added a commit that referenced this issue Feb 19, 2024
Setup infra for handling auto trait bounds disabled due to perf problems

This patch updates some of the partially-implemented functions of `ChalkContext as RustIrDatabase`, namely `adt_datum()` and `impl_provided_for()`. With those, we can now correctly work with auto trait bounds and distinguish methods based on them.

Resolves #7856 (the second code; the first one is resolved by #13074)

**IMPORTANT**: I don't think we want to merge this until #7637 is resolved. Currently this patch introduces A LOT of unknown types and type mismtaches as shown below. This is because we cannot resolve items like `hashbrown::HashMap` in `std` modules, leading to auto trait bounds on them and their dependents unprovable.

|crate (from `rustc-perf@c52ee6` except for r-a)|e3dc5a588f07d6f1d3a0f33051d4af26190abe9e|HEAD of this branch|
|---|---|---|
|rust-analyzer @ e3dc5a5 |exprs: 417528, ??ty: 907 (0%), ?ty: 114 (0%), !ty: 1|exprs: 417528, ??ty: 1704 (0%), ?ty: 403 (0%), !ty: 20|
|ripgrep|exprs: 62120, ??ty: 2 (0%), ?ty: 0 (0%), !ty: 0|exprs: 62120, ??ty: 132 (0%), ?ty: 58 (0%), !ty: 11|
|webrender/webrender|exprs: 94355, ??ty: 49 (0%), ?ty: 16 (0%), !ty: 2|exprs: 94355, ??ty: 429 (0%), ?ty: 130 (0%), !ty: 7|
|diesel|exprs: 132591, ??ty: 401 (0%), ?ty: 5129 (3%), !ty: 31|exprs: 132591, ??ty: 401 (0%), ?ty: 5129 (3%), !ty: 31|
@bors bors closed this as completed in 2223b4f Feb 19, 2024
@lnicola lnicola reopened this Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants