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

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

Open
mladedav opened this issue May 17, 2024 · 1 comment
Open
Labels
A-nameres name, path and module resolution C-bug Category: bug

Comments

@mladedav
Copy link
Contributor

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);
        //^^^
            "#,
        );
    }
@mladedav mladedav added the C-bug Category: bug label May 17, 2024
@Veykril Veykril added the A-nameres name, path and module resolution label May 17, 2024
@mladedav
Copy link
Contributor Author

This was a bit surprising to me, but I've just noticed I can also call foo::foo!() in the example inside bar.

So the use super::foo; seems to bring both the module and macro to the scope. So the coloring may not be an issue since rust analyzer has to choose one or the other, just the resolution seems wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-nameres name, path and module resolution C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

2 participants