rust-lang / rust-lang/rust-analyzer

import item quick-fix breaks `use` of macro that happens to share names with a module in the same scope

Open
#17,317 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

rust-analyzer version: rust-analyzer version: 0.3.1958-standalone

rustc version: rustc 1.78.0 (9b00956e5 2024-04-29)

editor or extension: VSCode v0.3.1958

relevant settings:

{
    "rust-analyzer.imports.prefix": "crate",
    "rust-analyzer.imports.group.enable": false,
    "rust-analyzer.cargo.targetDir": true,
}

repository link (if public, optional): (eg. rust-analyzer)

running the "import crate:🅱️:Baz2" quick-fix where indicated breaks the import of the some_mod macro, because rust-analyzer replaces the use with the following, which is incorrect because you can't import a macro using use path::to::macro::self:

    use crate::{
        a::some_mod::{self, Bar, Foo},
        b::{Baz, Baz2},
    };

code snippet to reproduce:

pub mod a {
    pub mod some_mod {
        #[derive(Hash)]
        pub struct Foo;
        #[derive(Hash)]
        pub struct Bar;
    }
    pub use Hash as some_mod; // actually I have a attribute macro, but this should be sufficient for testing
}
pub mod b {
    #[derive(Hash)]
    pub struct Baz;
    #[derive(Hash)]
    pub struct Baz2;
}
pub mod c {
    use crate::{
        a::{
            some_mod,
            some_mod::{Foo, Bar},
        },
        b::Baz,
    };

    type T = Baz2; // use the "import crate::b::Baz2" quick-fix

    #[derive(some_mod)]
    pub struct S(Foo, Bar, Baz, T);
}

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 by reproducing the import crate::b::Baz2 quick-fix with the provided Rust snippet and inspect the import quick-fix handling. Verify that adding the import does not rewrite the macro import into some_mod::{self, ...} and that the resulting code still resolves some_mod, Foo, Bar, Baz, and Baz2.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
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.