Skip to content

Reject outer attributes on cfg_select branches#155734

Open
qaijuang wants to merge 6 commits intorust-lang:mainfrom
qaijuang:cfg-select-doc-comment
Open

Reject outer attributes on cfg_select branches#155734
qaijuang wants to merge 6 commits intorust-lang:mainfrom
qaijuang:cfg-select-doc-comment

Conversation

@qaijuang
Copy link
Copy Markdown
Contributor

@qaijuang qaijuang commented Apr 24, 2026

@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 Apr 24, 2026
@qaijuang qaijuang force-pushed the cfg-select-doc-comment branch from fb8cf2e to 9c49112 Compare April 24, 2026 14:12
Comment thread tests/ui/lint/unused/unused-doc-comments-for-macros.rs Outdated
Co-authored-by: Copilot <copilot@github.com>
@qaijuang qaijuang marked this pull request as ready for review April 24, 2026 14:42
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 24, 2026

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot 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 Apr 24, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 24, 2026

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 17 candidates

@qaijuang qaijuang requested a review from folkertdev April 24, 2026 16:59
Comment thread compiler/rustc_attr_parsing/src/attributes/cfg_select.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 25, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 25, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 25, 2026

📌 Commit 4bb2b07 has been approved by JonathanBrouwer

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 25, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 25, 2026
…=JonathanBrouwer

Lint doc comments in cfg_select branches

Fixes rust-lang#155701.
rust-bors Bot pushed a commit that referenced this pull request Apr 25, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #154803 (Fix ICE from cfg_attr_trace )
 - #155485 (Add an edge-case test for `--remap-path-prefix` for `rustc` & `rustdoc`)
 - #155659 (cleanup, restructure and merge `tests/ui/deriving` into `tests/ui/derives`)
 - #155696 (Add a higher-level API for parsing attributes)
 - #155734 (Lint doc comments in cfg_select branches)
 - #155769 (triagebot.toml: Ping Enselic when tests/debuginfo/basic-stepping.rs changes)
@fmease
Copy link
Copy Markdown
Member

fmease commented Apr 25, 2026

This PR allows more code to compile, so it's a language change and technically needs a T-lang FCP.

cc @rust-lang/lang

@fmease
Copy link
Copy Markdown
Member

fmease commented Apr 25, 2026

I'm sorry

@bors r-

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 25, 2026
@folkertdev folkertdev added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination S-waiting-on-t-lang Status: Awaiting decision from T-lang labels Apr 25, 2026
@traviscross
Copy link
Copy Markdown
Contributor

This PR allows more code to compile, so it's a language change and technically needs a T-lang FCP... The grammar of cfg_select is literally written down in the Reference, so any extensions are new and separate feature requests.

Thanks @fmease for catching this and navigating it.

@qaijuang qaijuang changed the title Lint doc comments in cfg_select branches Reject outer attributes on cfg_select branches Apr 25, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 25, 2026

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot 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 Apr 25, 2026
@qaijuang qaijuang requested a review from folkertdev April 25, 2026 23:46
Comment thread tests/ui/macros/cfg_select.stderr Outdated
Comment thread tests/ui/macros/cfg_select.rs
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 26, 2026
Co-authored-by: Copilot <copilot@github.com>
@rustbot rustbot 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 Apr 26, 2026
Comment thread tests/ui/macros/cfg_select.stderr
Comment thread compiler/rustc_parse/src/parser/cfg_select.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 26, 2026
Co-authored-by: Copilot <copilot@github.com>
@rustbot rustbot 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 Apr 26, 2026
// `parse_outer_attributes` recovers inner doc comments as outer ones, so collect their
// spans first and suppress the follow-up `cfg_select` branch diagnostic for them.
let mut inner_doc_comment_spans = Vec::new();
let mut snapshot = self.create_snapshot_for_diagnostic();
Copy link
Copy Markdown
Member

@fmease fmease Apr 26, 2026

Choose a reason for hiding this comment

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

IINM this is creating a parser snapshot in the happy path, right? this is an expensive operation and should only happen in error paths.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this using a snapshot in the first place? Can't you just use parse_inner_attributes and reject inner attrs afterwards? That function is smart enough not to bail out when it encounters outer attributes so you don't need to worry about that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

calling parse_inner_attributes first for /// outer followed by //! inner would return empty at the /// and would never notice the later //!

The test case:

cfg_select! {
    /// outer doc comment
    //! inner doc comment
    debug_assertions => {}
    _ => {}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

IINM this is creating a parser snapshot in the happy path, right? this is an expensive operation and should only happen in error paths.

That’s a good point. I’m thinking of classifying recovered doc comments directly from their source snippet in the error path, so we can avoid the snapshot cost

@qaijuang qaijuang requested a review from fmease April 26, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) 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.

cfg_select! + doc comment gives cryptic compiler error

6 participants