Skip to content

Replace check_attr_crate_level with check_target for #[doc] - #161521

Merged
rust-bors[bot] merged 7 commits into
rust-lang:mainfrom
evavh:doc-crate-level-check
Oct 26, 2026
Merged

rust-bors[bot] merged 7 commits into
rust-lang:mainfrom
evavh:doc-crate-level-check

Conversation

@evavh

@evavh evavh commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

A small change as part of the target checking refactor for #[doc] attribute parsing.

r? @JonathanBrouwer

@rustbot

rustbot commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added 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. labels Aug 22, 2026
Comment thread compiler/rustc_attr_parsing/src/attributes/doc.rs Outdated
Comment thread compiler/rustc_attr_parsing/src/attributes/doc.rs Outdated
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_attr_parsing/src/attributes/doc.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 Aug 22, 2026
@rustbot

rustbot commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

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

@evavh

evavh commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

I tried to add tests for the crate-level attribute checks, but I'm running into an interesting problem: there seems to be no way for the target check to fail. The possible ways for a crate-level attribute to be applied to a non-crate target (that I can think of) are:

  1. A #[doc(CRATE_LEVEL_ONLY)] attribute applied to an item: this outputs the error crate-level attribute should be an inner attribute: add an exclamation mark
  2. A #![doc(CRATE_LEVEL_ONLY)] attribute applied to an item (ie not at the top of the file): the error is an inner attribute is not permitted in this context

In both cases the compiler throws an error before we even reach the target checking code in doc.rs. The same applies to doc attribute arguments that shouldn't be used crate-level, like fake_variadic. It seems the distinction between inner (#!) and outer (#) attributes is enough to guard against wrong crate/non-crate targets.

The test I wrote covers all doc attribute arguments that check whether they are applied at crate level, and they all throw errors in earlier parsing steps. What do we do with the target checking code that does nothing?

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@JonathanBrouwer JonathanBrouwer 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 Aug 30, 2026
@JonathanBrouwer

JonathanBrouwer commented Aug 30, 2026 •

Copy link
Copy Markdown
Member

@evavh I took a random crate-level doc attribute, and applied it to a function:

#[doc(html_no_source)]
fn test1() { }
fn test2() {
    #![doc(html_no_source)]
}

In both cases this produces "this attribute can only be applied at the crate level"
This diagnostic is produced in target checking, so if you hit it you are doing target checking somewhere:

pub(crate) fn check_crate_level(cx: &mut AcceptContext<'_, 'sess>, warn: bool) {

The diagnostic "an inner attribute is not permitted in this context" is produced when you apply inner attributes to an item that doesn't support inner attributes, such as a struct, and is indeed produced in the parser

struct Test {
    #![doc(html_no_source)]
}

@rustbot author

@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 Aug 30, 2026
@evavh
evavh force-pushed the doc-crate-level-check branch from 6c2968e to 7a056e3 Compare August 31, 2026 14:31
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@mejrs mejrs added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. needs-crater This change needs a crater run to check for possible breakage in the ecosystem. labels Aug 31, 2026
@mejrs

mejrs commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

These are breaking changes, so they will need a crater run. Alternatively, you could refactor this in a way that doesn't change behavior.

@evavh
evavh marked this pull request as draft September 1, 2026 11:56
@evavh

evavh commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Turns out I was very confused by the "new" diagnostic emitted by check_crate_level called by AttributeParser::check_target. The target checking is still happening correctly with this refactor, though I do think the diagnostic has become worse.

I tried changing the diagnostic in check_crate_level, but that broke all kinds of unrelated tests.

As for breaking changes, using a crate-level doc attribute on the wrong target now emits an ERROR instead of a WARN.

@rustbot ready

@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 Sep 1, 2026
@rustbot

This comment has been minimized.

@evavh
evavh force-pushed the doc-crate-level-check branch from 8433ebe to 873c9c0 Compare October 26, 2026 11:19
@rustbot

rustbot commented Oct 26, 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.

@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors r+ rollup
Thanks a lot!

@rust-bors

rust-bors Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

📋 This PR cannot be approved because it currently has the following labels: needs-fcp, needs-crater.

@JonathanBrouwer JonathanBrouwer removed needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. needs-crater This change needs a crater run to check for possible breakage in the ecosystem. labels Oct 26, 2026
@JonathanBrouwer

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 873c9c0 has been tentatively approved by JonathanBrouwer

It will be put into the queue for this repository once PR CI succeeds.

@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 Oct 26, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 26, 2026
…athanBrouwer

Replace check_attr_crate_level with check_target for #[doc]

A small change as part of the target checking refactor for #[doc] attribute parsing.

r? @JonathanBrouwer
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 26, 2026
…athanBrouwer

Replace check_attr_crate_level with check_target for #[doc]

A small change as part of the target checking refactor for #[doc] attribute parsing.

r? @JonathanBrouwer
rust-bors Bot pushed a commit that referenced this pull request Oct 26, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #162808 (Enable `--generate-link-to-definition` rustdoc option on std docs)
 - #163219 (assumptions_on_binders: Test mixed ambiguous ORs)
 - #164985 (Only recover if we may for `recover_missing_dot`)
 - #161521 (Replace check_attr_crate_level with check_target for #[doc])
 - #162530 (Move #[naked] attribute check to attribute parsing stage)
 - #164103 (trait solver: Resolve region vars before eager placeholder handling)
 - #164692 (move `ppcf128` lang item to `core::num::imp`)
 - #164702 (Point at `impl` through which associated function is provided)
 - #164776 (Account for trait object with implicit `'static` lifetime in field type being returned)
 - #164791 (Self param after const param)
 - #164856 (Switch from `insta` to `expect-test` in bootstrap)
 - #164888 (Add regression tests for #156380)
rust-bors Bot pushed a commit that referenced this pull request Oct 26, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #163219 (assumptions_on_binders: Test mixed ambiguous ORs)
 - #164985 (Only recover if we may for `recover_missing_dot`)
 - #161521 (Replace check_attr_crate_level with check_target for #[doc])
 - #162530 (Move #[naked] attribute check to attribute parsing stage)
 - #164103 (trait solver: Resolve region vars before eager placeholder handling)
 - #164692 (move `ppcf128` lang item to `core::num::imp`)
 - #164702 (Point at `impl` through which associated function is provided)
 - #164741 (Use explicit target cpu in tests parsing generated asm)
 - #164767 (Add regression tests for transmutability issues)
 - #164776 (Account for trait object with implicit `'static` lifetime in field type being returned)
 - #164791 (Self param after const param)
 - #164856 (Switch from `insta` to `expect-test` in bootstrap)
 - #164888 (Add regression tests for #156380)
 - #164967 (Add codegen test for power of two implying nonzero with debuginfo)
@rust-bors
rust-bors Bot merged commit 7154f9c into rust-lang:main Oct 26, 2026
14 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Oct 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 26, 2026
Rollup merge of #161521 - evavh:doc-crate-level-check, r=JonathanBrouwer

Replace check_attr_crate_level with check_target for #[doc]

A small change as part of the target checking refactor for #[doc] attribute parsing.

r? @JonathanBrouwer
github-actions Bot pushed a commit to rust-lang/stdarch that referenced this pull request Oct 26, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - rust-lang/rust#163219 (assumptions_on_binders: Test mixed ambiguous ORs)
 - rust-lang/rust#164985 (Only recover if we may for `recover_missing_dot`)
 - rust-lang/rust#161521 (Replace check_attr_crate_level with check_target for #[doc])
 - rust-lang/rust#162530 (Move #[naked] attribute check to attribute parsing stage)
 - rust-lang/rust#164103 (trait solver: Resolve region vars before eager placeholder handling)
 - rust-lang/rust#164692 (move `ppcf128` lang item to `core::num::imp`)
 - rust-lang/rust#164702 (Point at `impl` through which associated function is provided)
 - rust-lang/rust#164741 (Use explicit target cpu in tests parsing generated asm)
 - rust-lang/rust#164767 (Add regression tests for transmutability issues)
 - rust-lang/rust#164776 (Account for trait object with implicit `'static` lifetime in field type being returned)
 - rust-lang/rust#164791 (Self param after const param)
 - rust-lang/rust#164856 (Switch from `insta` to `expect-test` in bootstrap)
 - rust-lang/rust#164888 (Add regression tests for #156380)
 - rust-lang/rust#164967 (Add codegen test for power of two implying nonzero with debuginfo)
#[note(
"read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> for more information"
)]
pub(crate) struct AttrCrateLevelOnly;

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.

This is incorrect, this needs a primary span, otherwise if used as an error it won't have a span to point at and will be displayed like this:

error: this attribute can only be applied at the crate level
   |
   = note: read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> for more information

(Fixed in #164582)

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.

Right, thanks!

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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

5 participants