rust-lang / rust-lang/rust-analyzer

Autoimport doesn't account for overloaded symbols

Open
#10,599 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-assists C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

Example:

mod foo {
    pub mod bar {
        pub fn baz() {}
    }
    pub fn bar() {}
}

use crate::foo::bar;

fn main() {
    bar();
    //         v
    foo::bar::baz();
}

If we put the cursor on baz right under v in the comment and invoke Replace qualified path with use assist.

The following changes will be made:

mod foo {
    pub mod bar {
        pub fn baz() {}
    }
    pub fn bar() {}
}

- use crate::foo::bar;
+ use crate::foo::bar::{self, baz};

fn main() {
    bar();
    foo::bar::baz();
}

which results in invalid code because bar symbol is now imported via self which is only available for module namespace, so bar() call is broken.

Also, when running that assist only the use statement is updated, foo::bar::baz isn't reduced to just baz(), but that looks like an unrelated problem?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the implementation of the “Replace qualified path with use” assist and its existing tests; reproduce the provided overloaded module/function example. Ensure the assist preserves the valid bar() call while importing baz, then add a regression test and separately verify whether path shortening is covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.