Skip to content

feat: support macro expansion in #[doc = ...] attributes#21928

Merged
ChayimFriedman2 merged 13 commits into
rust-lang:masterfrom
so1ve:feat/doc-macros
Apr 3, 2026
Merged

feat: support macro expansion in #[doc = ...] attributes#21928
ChayimFriedman2 merged 13 commits into
rust-lang:masterfrom
so1ve:feat/doc-macros

Conversation

@so1ve

@so1ve so1ve commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

fixes #7688, fixes #8092, fixes #11137, fixes #20494, closes #21303

Macro expansion part is hand-written; AI (specifically, GPT 5.4) was used to generate tests.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 1, 2026
@rustbot

rustbot commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 1, 2026
@so1ve so1ve force-pushed the feat/doc-macros branch from 2c59f34 to f05447d Compare April 1, 2026 10:20
@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. has-merge-commits labels Apr 1, 2026
@ChayimFriedman2

Copy link
Copy Markdown
Contributor

I cannot vouch for which is better but note that #21303 also implements that however by text-matching on include_str!() and supporting it alone. Does this PR does the same, or does it fully expand all macros?

@so1ve

so1ve commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

Yes, this PR uses rust-analyzer's internal macro expansion mechanism, enabling all macro expansion within #[doc] attributes, instead of special-casing include_str. See this test case for details.

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Then this PR is preferred.

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread crates/hir-def/src/attrs.rs Outdated
Comment thread crates/hir-def/src/attrs.rs Outdated
Comment thread crates/hir-def/src/attrs.rs
Comment thread crates/hir-def/src/attrs.rs Outdated
Comment thread crates/hir-def/src/attrs.rs Outdated
Comment thread crates/hir-def/src/attrs.rs Outdated
Comment thread crates/hir-def/src/attrs.rs Outdated
@so1ve

so1ve commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

Just applied the suggestions!

Comment thread crates/ide/src/hover/tests.rs Outdated
Comment thread crates/hir-def/src/attrs/docs.rs
Comment thread crates/hir-def/src/attrs/docs.rs Outdated
Comment thread crates/hir-def/src/attrs.rs Outdated
Comment thread crates/hir-def/src/attrs/docs.rs Outdated
Comment thread crates/hir-def/src/attrs/docs.rs
Comment thread crates/hir-def/src/attrs/docs.rs Outdated
Comment thread crates/hir-def/src/attrs/docs.rs Outdated
so1ve and others added 2 commits April 3, 2026 20:19
Co-authored-by: Chayim Refael Friedman <chayimfr@gmail.com>

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really sorry, but it looks like even outer doc comments in module resolve inside the module (https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=a98e60abf9c8b018f0d47ed687c7416e). Which is very weird IMO (unintended behavior?) but we should follow rustc 😅

View changes since this review

@so1ve

so1ve commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

Updated!

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks!

I have an idea what to do with cfg_attr, I'll do it later.

View changes since this review

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Apr 3, 2026
Merged via the queue into rust-lang:master with commit 69774d5 Apr 3, 2026
17 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 3, 2026
@asukaminato0721

Copy link
Copy Markdown
Contributor

amazing!

@lnicola

lnicola commented Apr 6, 2026

Copy link
Copy Markdown
Member

Unlike #21303, this didn't add .md to the included extensions, so many of the linked issues and even the test won't work.

Should we bite the bullet and load those into the VFS?

@Veykril

Veykril commented Apr 6, 2026

Copy link
Copy Markdown
Member

Yea I think we should

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

There aren't many markdown files usually so I believe it shouldn't cause perf problems.

@lnicola

lnicola commented Apr 6, 2026

Copy link
Copy Markdown
Member

@asukaminato0721 want to do the honors? :)

@so1ve

so1ve commented Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

Ah, I never considered that the expansion of include_str requires the use of the VFS 😅 I was misled by the test, it seems that include_str in the test can run normally

@ChayimFriedman2

ChayimFriedman2 commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Tests always includes the specified files in the VFS.

@asukaminato0721

Copy link
Copy Markdown
Contributor

@asukaminato0721 want to do the honors? :)

sure :)

@joshka

joshka commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

This does not appear to actually work (for the axum source code at least).

It does not appear to have fixed the problem:

Installed version:
Image

https://github.com/tokio-rs/axum/blob/441216428893d13544f12722b54dcaaadd47135a/axum/src/error_handling/mod.rs#L1
Module level doc:
Image

Function level doc:
https://github.com/tokio-rs/axum/blob/441216428893d13544f12722b54dcaaadd47135a/axum/src/routing/method_routing.rs#L1002
Image

I'd recommend re-opening at least one of the mentioned issues to track this (and perhaps consolidating on one of them?)

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

@joshka As explained literally just above, the expansion does work but rust-analyzer in its current version does not load md files. If you'll make the file extension .rs it'll work. This will be fixed by the next release.

@joshka

joshka commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Ah, I see now. I missed that the VFS piece meant markdown wouldn’t work yet. Thanks for clarifying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

7 participants