rust-lang / rust-lang/rust-analyzer

Macros inside modules with the same names are not considered as macros.

Open
#17,249 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

If I define a macro inside file foo and the macro is also named foo and reexport it next to the module, rust analyzer seems a bit confused about which foo I'm talking about.

image

In the image in module bar you can see that rust-analyzer thinks that it is importing a module (green color) but the macro is actually in scope and can be called.

For some reason this seems to subtly break some other things, for example when a macro like this is used to define a struct, that type is not picked up by rust analyzer. See the test at the end of this report for example of how this breaks "goto definition".

rust-analyzer version: Current master, 83ba42043

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

editor or extension: VSCode

test to reproduce:
Note that if you rename the generate macro so that it is not named as the containing module, the test succeeds.

    #[test]
    fn goto_struct_defined_by_macro() {
        check(
            r#"
//- /main.rs crate:main
mod generate;
mod usage;

pub use generate::generate;
use usage::Foo;

fn main() {
    let _ = Foo$0;
}

//- /generate.rs
macro_rules! generate {
    ($name:ident) => {
        pub struct $name;
    };
}

pub use generate;

//- /usage.rs
use super::generate;

generate!(Foo);
        //^^^
            "#,
        );
    }

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 with the provided goto_struct_defined_by_macro test and its virtual main.rs, generate.rs, and usage.rs files; run it to reproduce the name collision between the generate module and macro. Done means the macro remains recognized in usage.rs and goto definition from Foo resolves to the struct generated by the macro.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.