Skip to content

fix external docs for exported macros #20384

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

Merged
merged 1 commit into from
Aug 5, 2025

Conversation

vxpm
Copy link
Contributor

@vxpm vxpm commented Aug 4, 2025

macros annotated with #[macro_export] are documented at the root of the crate, yet externalDocs was returning a path which considered it to be documented at the module it's defined in. this PR fixes that.

example:

  • before: externalDocs for println! was returning https://doc.rust-lang.org/nightly/std/macros/macro.println.html
  • after: externalDocs for println! returns https://doc.rust-lang.org/nightly/std/macro.println.html

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 4, 2025
@ChayimFriedman2
Copy link
Contributor

Please add a test.

@vxpm
Copy link
Contributor Author

vxpm commented Aug 4, 2025

I'm not very familiar with the fixtures used in rust-analyzer's tests so I might be doing something wrong here, but the following test returns no links at all:

#[test]
fn external_docs_macro_export() {
    check_external_docs(
        r#"
//- /main.rs crate:foo
pub mod inner {
    #[macro_export]
    macro_rules my_macro! {
        () => {};
    }
}

fn main() {
    inner::my_m$0acro!();
}
        "#,
        Some("/home/user/project"),
        Some(expect![[r#"https://docs.rs/foo/*/foo/macro.my_macro.html"#]]),
        Some(expect![[r#"file:///home/user/project/doc/foo/macro.my_macro.html"#]]),
        Some("/sysroot"),
    );
}

Seeing as there are no other tests in the file which define macro_rules, is there something else I need to do to make the analysis work?

@ChayimFriedman2
Copy link
Contributor

You made two mistakes: misplaced the !, and forgot that #[macro_export]ed macros resolve under the crate root. The fixed code is this:

#[test]
fn external_docs_macro_export() {
    check_external_docs(
        r#"
//- /main.rs crate:foo
pub mod inner {
    #[macro_export]
    macro_rules! my_macro {
        () => {};
    }
}

fn main() {
    my_macro$0!();
}
        "#,
        Some("/home/user/project"),
        Some(expect![[r#"https://docs.rs/foo/*/foo/macro.my_macro.html"#]]),
        Some(expect![[r#"file:///home/user/project/doc/foo/macro.my_macro.html"#]]),
        Some("/sysroot"),
    );
}

And yeah, I agree that debugging incorrect code in our tests is harder that it should be (although the highlighting helps - if it isn't highlighted correctly, there's probably an error somewhere).

@vxpm
Copy link
Contributor Author

vxpm commented Aug 5, 2025

oops - macro_rules syntax always trips me up, and the wrong path is a leftover from me trying to figure out what's wrong. thanks, such silly mistakes.

@ChayimFriedman2
Copy link
Contributor

Can you squash please?

@vxpm vxpm force-pushed the fix-external-docs branch from c69f0ff to d399bec Compare August 5, 2025 21:09
@vxpm
Copy link
Contributor Author

vxpm commented Aug 5, 2025

done!

@ChayimFriedman2
Copy link
Contributor

Thanks!

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Aug 5, 2025
Merged via the queue into rust-lang:master with commit 7543653 Aug 5, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants