Skip to content

Error on non-literal expressions in doc attributes on macro calls - #160904

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

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

View all comments

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!()]

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

This comment has been minimized.

@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

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 29, 2026
…onathanbrouwer

rework handling of doc attributes on macro calls

 rust-lang#160904 but with the fcw changes removed

r? @JonathanBrouwer
rust-bors Bot pushed a commit that referenced this pull request Aug 30, 2026
Rollup merge of #161514 - mejrs:move_doc_feature_gating, r=jonathanbrouwer

rework handling of doc attributes on macro calls

 #160904 but with the fcw changes removed

r? @JonathanBrouwer
@rust-bors

This comment has been minimized.

@mejrs
mejrs force-pushed the doc_feature_gating branch from 708eacf to 20a64c9 Compare August 30, 2026 11:27
@rustbot

This comment has been minimized.

@mejrs
mejrs force-pushed the doc_feature_gating branch from 20a64c9 to e42b6b2 Compare August 30, 2026 11:35
@mejrs mejrs added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 6, 2026
@JonathanBrouwer JonathanBrouwer added S-waiting-on-t-lang Status: Awaiting decision from T-lang and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 11, 2026
@traviscross traviscross added the T-lang Relevant to the language team label Sep 16, 2026
@tmandry

tmandry commented Sep 16, 2026

Copy link
Copy Markdown
Member

We discussed this in the lang meeting today. Those present were happy to approve this, and also to go straight to a hard error, crater permitting.

@rfcbot fcp merge lang

@rust-rfcbot

rust-rfcbot commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

@tmandry has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 16, 2026
@traviscross

Copy link
Copy Markdown
Contributor

Thanks @mejrs, @JonathanBrouwer.

@rfcbot reviewed

@mejrs

mejrs commented Sep 16, 2026

Copy link
Copy Markdown
Member Author

I'll go ahead and crater this asap, I'd rather not have the complexity of emitting the lint anyway

@mejrs
mejrs force-pushed the doc_feature_gating branch from e42b6b2 to 1c5fe3f Compare September 16, 2026 22:07
@rustbot

rustbot commented Sep 16, 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 changed the title FCW on expressions in doc attributes on macro calls Error on non-literal expressions in doc attributes on macro calls Sep 16, 2026
@mejrs

mejrs commented Sep 17, 2026

Copy link
Copy Markdown
Member Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 17, 2026
Error on non-literal expressions in doc attributes on macro calls
@rust-bors

rust-bors Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: f4dd856 (f4dd856795debe636f2ba39a900f162f5abeb7e8)
Base parent: c999cef (c999cef531ea9059e189e82fe0e82c5daf249bc9)

@mejrs

mejrs commented Sep 17, 2026

Copy link
Copy Markdown
Member Author

@craterbot check

@craterbot

Copy link
Copy Markdown
Collaborator

👌 Experiment pr-160904 created and queued.
🤖 Automatically detected try build f4dd856
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-t-lang Status: Awaiting decision from T-lang labels Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-crater Status: Waiting on a crater run to be completed. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants