Skip to content

FCW on expressions in doc attributes on macro calls - #160904

Open
mejrs wants to merge 1 commit into
rust-lang:mainfrom
mejrs:doc_feature_gating
Open

FCW on expressions in doc attributes on macro calls#160904
mejrs wants to merge 1 commit into
rust-lang:mainfrom
mejrs:doc_feature_gating

Conversation

@mejrs

@mejrs mejrs commented Aug 11, 2026

Copy link
Copy Markdown
Member

Recently I discovered that, since Rust 1.94, doc attributes on macro invocations can have arbitrary expressions in them:

// accidentally stabilized in 1.94
#[doc = concat!("", "")]
#[doc = {
    let a = 1;
    let b = 1;
    let sum = a + b;
    assert_eq!(sum, 2);
}]
println!();

As part of the attribute parsing rework this was accidentally allowed. Note that doc attributes (or any doc comment) on macro invocations do nothing, because documentation for macro invocations is not rendered - this emits a lint saying macros must produce doc comments as part of their expansion.

With this PR, it now emits a FCW, like #57571. As this is so niche it's probable this could go straight to an error but there's quite a crater queue so I'd rather do this now and try turning it into an error later.

error: invalid expression in `doc` attribute on macro invocation
  --> $DIR/attr-on-mac-call.rs:115:13
   |
LL |     #[doc = concat!("", "")]
   |     --------^^^^^^^^^^^^^^^^^^^^^^^- help: remove the attribute
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
   = note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

error: invalid expression in `doc` attribute on macro invocation
  --> $DIR/attr-on-mac-call.rs:118:13
   |
LL |        #[doc = {
   |  ______-       ^
   | | _____________|
LL | ||         let a = 1;
LL | ||         let b = 1;
LL | ||         let sum = a + b;
LL | ||         assert_eq!(sum, 2);
LL | ||     }]
   | ||_____^- help: remove the attribute
   |  |_____|
   |
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>

error: aborting due to 7 previous errors; 30 warnings emitted

Note that #[doc = mac!()] is included in this. While this is allowed everywhere else as normally attribute parsing only sees it after its expansion, it is not expanded here, but we do need to check attributes here since we can't check them later as they're lost by then.

#[doc = mac!()] // this would be expanded second, but any remaining attributes
                // on macro invocations are dropped before they are expanded.
println!(); // this is expanded first

I don't think it is worth trying to make particular case work - this would involve checking that the expression would expand to a string literal:

  • These expansions have a defined order (see also @petrochenkov's comment at #t-compiler > attribute parsing rework @ 💬)
  • so it would be quite a hack to check whether the expression would expand to a string literal
  • changing macro expansion order might be possible but is a big can of worms and undesirable.
  • it would serve no use case, as the attribute is dropped regardless

This change would also make it consistent with all other key-value attributes. For an example, the following are allowed

#[deprecated = "foo"] // just `unused_attributes` warning
println!();
    
#[deprecated = concat!()]
struct Foo;

but this is not:

#[deprecated = concat!()]
println!();
error: attribute value must be a literal
 --> src/main.rs:2:20
  |
2 | #[deprecated = concat!()]

This PR implements some non-visible changes as well

  • move feature gating of doc attributes to attribute parsing
  • remove OmitDoc (only used for doc expressions on macro calls..?)

r? @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 11, 2026
@mejrs
mejrs force-pushed the doc_feature_gating branch from 63a2f0f to f705d49 Compare August 11, 2026 12:07
@rust-log-analyzer

This comment has been minimized.

@mejrs
mejrs force-pushed the doc_feature_gating branch from f705d49 to 11ac310 Compare August 12, 2026 09:40
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@mejrs
mejrs force-pushed the doc_feature_gating branch from 478a22c to 65407a3 Compare August 13, 2026 12:12
@rust-log-analyzer

This comment has been minimized.

@mejrs
mejrs force-pushed the doc_feature_gating branch from 65407a3 to 9b15145 Compare August 14, 2026 21:21
@rust-log-analyzer

This comment has been minimized.

@mejrs
mejrs force-pushed the doc_feature_gating branch from 9b15145 to dd5debd Compare August 15, 2026 17:07
@rust-log-analyzer

This comment has been minimized.

@mejrs
mejrs force-pushed the doc_feature_gating branch from dd5debd to 1953f77 Compare August 16, 2026 14:37
@rust-log-analyzer

This comment has been minimized.

@mejrs
mejrs force-pushed the doc_feature_gating branch from 1953f77 to 3888e00 Compare August 16, 2026 15:34
@mejrs
mejrs marked this pull request as ready for review August 16, 2026 15:42
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 16, 2026
@rustbot

rustbot commented Aug 16, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 16, 2026
@mejrs
mejrs force-pushed the doc_feature_gating branch from 3888e00 to 8c0900d Compare August 19, 2026 22:26
@rustbot

rustbot commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main 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.

@mejrs
mejrs force-pushed the doc_feature_gating branch from 8c0900d to 9fdd707 Compare August 19, 2026 23:11
@mejrs mejrs changed the title rework handling of doc attributes on macro calls FCW on expressions in doc attributes on macro calls Aug 19, 2026
@mejrs mejrs added the I-lang-nominated Nominated for discussion during a lang team meeting. label Aug 19, 2026
@mejrs

mejrs commented Aug 19, 2026

Copy link
Copy Markdown
Member Author

@JonathanBrouwer this is ready for review now

@rust-log-analyzer

This comment has been minimized.

@mejrs
mejrs force-pushed the doc_feature_gating branch from 9fdd707 to 254ea39 Compare August 20, 2026 00:01
@rust-log-analyzer

This comment has been minimized.

@mejrs
mejrs force-pushed the doc_feature_gating branch from 254ea39 to 623360c Compare August 20, 2026 11:58
@rust-bors

rust-bors Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #161043) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants