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

Auto completion should use fully qualified syntax in case of ambiguity #18895

Open
PRO-2684 opened this issue Jan 9, 2025 · 6 comments
Open
Assignees
Labels
A-completion autocompletion C-bug Category: bug

Comments

@PRO-2684
Copy link

PRO-2684 commented Jan 9, 2025

rust-analyzer version: 0.3.2257-standalone

rustc version: 1.83.0 (90b35a623 2024-11-26)

editor or extension: VSCode, with [email protected]

relevant settings: N/A

repository link (if public, optional): N/A

code snippet to reproduce:

fn main() {
    trait Animal {
        fn baby_name() -> String;
    }
    struct Dog;
    impl Dog {
        fn baby_name() -> String {
            "Spot".into()
        }
    }
    impl Animal for Dog {
        fn baby_name() -> String {
            "puppy".into()
        }
    }
    // Try typing: `println!("{}", Dog::);`, and here's the top-2 suggestions RA gave me:
    // 1. `baby_name()`
    // 2. `baby_name()` (as `Animal`)
    // After choosing the second one, this is what RA auto-completed:
    println!("{}", Dog::baby_name()); // Prints "Spot"
    // While I expected:
    println!("{}", <Dog as Animal>::baby_name()); // Prints "puppy"
}
@PRO-2684 PRO-2684 added the C-bug Category: bug label Jan 9, 2025
@lnicola lnicola added the A-completion autocompletion label Jan 9, 2025
@ChayimFriedman2
Copy link
Contributor

The code completed is correct, why should we prefer UFCS?

@lnicola
Copy link
Member

lnicola commented Jan 9, 2025

I think the point is that we should use UFCS if the user picks a trait method that's shadowed by an inherent one?

@ChayimFriedman2
Copy link
Contributor

Ah yes sorry, I thought there wasn't ambiguity.

@PRO-2684
Copy link
Author

PRO-2684 commented Jan 10, 2025

This issue can also be observed from methods with &self. For example:

fn main() {
    trait Pilot {
        fn fly(&self);
    }
    trait Wizard {
        fn fly(&self);
    }
    struct Human;
    impl Pilot for Human {
        fn fly(&self) {
            println!("This is your captain speaking.");
        }
    }
    impl Wizard for Human {
        fn fly(&self) {
            println!("Up!");
        }
    }
    let person = Human;
    // Try typing: `person.f`, and RA will suggest: `fly()` (as `Wizard`)
    // Note that it doesn't provide suggestion for (as `Pilot`)
    // When I accept the auto-completion, it gave me:
    person.fly(); // `rustc` would complain: multiple applicable items in scope
    // The expected behavior should be suggesting all applicable items:
    // 1. `fly()` (as `Wizard`)
    // 2. `fly()` (as `Pilot`)
    // And use the following syntax to avoid ambiguity:
    Pilot::fly(&person);
    Wizard::fly(&person);
}

@Veykril
Copy link
Member

Veykril commented Jan 10, 2025

Note that we have some construction logic for this in the qualify assist (so there should be possibility of code sharing for the completions)

@davidsemakula
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion autocompletion C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

5 participants