Skip to content

Attribute documentation for used, expect, should_panic, cfg_attr, and path#158664

Open
kantnero wants to merge 16 commits into
rust-lang:mainfrom
kantnero:attribute-docs
Open

Attribute documentation for used, expect, should_panic, cfg_attr, and path#158664
kantnero wants to merge 16 commits into
rust-lang:mainfrom
kantnero:attribute-docs

Conversation

@kantnero

@kantnero kantnero commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

View all comments

Attribute documentation for used, expect, should_panic, cfg_attr, and path using the #[doc(attribute = "")] mechanism

Tested with: ./x test library/std --doc

r? @GuillaumeGomez
cc @traviscross @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 1, 2026
@rustbot

rustbot commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
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: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 13 candidates
  • Random selection from Darksonn, JohnTitor, Mark-Simulacrum, clarfonthey, jhpratt

@rustbot

rustbot commented Jul 1, 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.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 1, 2026
kantnero added 2 commits July 1, 2026 16:59
…h, and ignore

Signed-off-by: Emmanuel Ugwu <emmanuelugwu121@gmail.com>
…h, and ignore

Signed-off-by: Emmanuel Ugwu <emmanuelugwu121@gmail.com>
Comment thread library/core/src/attribute_docs.rs Outdated
Comment thread library/core/src/attribute_docs.rs Outdated
Comment thread library/core/src/attribute_docs.rs Outdated
Comment thread library/core/src/attribute_docs.rs
Comment thread library/core/src/attribute_docs.rs Outdated
Comment thread library/core/src/attribute_docs.rs Outdated
Comment thread library/core/src/attribute_docs.rs Outdated
Comment thread library/core/src/attribute_docs.rs Outdated
Comment thread library/core/src/attribute_docs.rs Outdated
Signed-off-by: Emmanuel Ugwu <emmanuelugwu121@gmail.com>
@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 PR has merge commits, merge with caution. labels Jul 1, 2026
@kantnero kantnero requested a review from GuillaumeGomez July 1, 2026 16:43
@JohnTitor JohnTitor removed their assignment Jul 1, 2026
Comment thread library/core/src/attribute_docs.rs Outdated
/// Used for conditional application of attributes.
///
/// The `cfg_attr` attribute applies one or more attributes to an item only if a given
/// configuration predicate is true. If the condition is false, the attributes are ignored.

@GuillaumeGomez GuillaumeGomez Jul 2, 2026

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.

Suggested change
/// configuration predicate is true. If the condition is false, the attributes are ignored.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
///
/// ```rust
/// // This function is annotated with `#[test]` only when testing is active.
/// #[cfg_attr(test, test)]

@GuillaumeGomez GuillaumeGomez Jul 2, 2026

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.

It's neat, but I think it's a bit too complex (test is present twice). I suggest instead to make it a test only on linux for example. Like that we have a clear predicate, completely different than the applied attribute.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
/// }
/// ```
///
/// The predicate uses the same syntax as [`cfg`]. For complex conditions, you can combine

@GuillaumeGomez GuillaumeGomez Jul 2, 2026

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.

Does it link to the right cfg page?

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.

You still didn't answer this question. When you generate doc locally, what is the link of this item?

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.

/rust/build/x86_64-unknown-linux-gnu/doc/core/attribute.cfg.html

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.

I just checked it links to macro cfg

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.

So it's not the right link.

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.

Yes, I have removed it

@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 Jul 6, 2026
@rustbot

rustbot commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

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

Signed-off-by: Emmanuel Ugwu <emmanuelugwu121@gmail.com>
@kantnero kantnero requested a review from GuillaumeGomez July 6, 2026 16:55
@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 Jul 6, 2026
Signed-off-by: Emmanuel Ugwu <emmanuelugwu121@gmail.com>
Comment thread library/core/src/attribute_docs.rs Outdated
Comment on lines +196 to +198
/// Used for conditional application of attributes.
///
/// The `cfg_attr` attribute is used to conditionally apply one or more attributes to an item.

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

Suggested change
/// Used for conditional application of attributes.
///
/// The `cfg_attr` attribute is used to conditionally apply one or more attributes to an item.
/// The `cfg_attr` attribute is used to conditionally apply one or more attributes to an item.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
/// Example:
///
/// ```rust
/// // This function is annotated with `#[test]` only when on Linux platform.

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

Suggested change
/// // This function is annotated with `#[test]` only when on Linux platform.
/// // This function is annotated with `#[test]` only on Linux platforms.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
///
/// ```rust
/// #[cfg_attr(all(feature = "system", feature = "disk"),
/// doc = "This module is only accessible when both `system` and `disk` are enabled")]

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

The doc message is wrong considering that there is no cfg in here.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
Comment on lines +388 to +389
/// The `#[used]` attribute can be used on static variables to prevent the Rust compiler
/// from optimizing them away even if they are not referenced anywhere else in the code.

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

Suggested change
/// The `#[used]` attribute can be used on static variables to prevent the Rust compiler
/// from optimizing them away even if they are not referenced anywhere else in the code.
/// The `#[used]` attribute can be used on static variables to prevent the Rust compiler
/// from optimizing them away even if they are not used anywhere in the current crate.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
/// #[test]
/// #[ignore]
/// fn test() {
/// // ... This test is ignored by the test harness (the compiler still compiles it).

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

Suggested change
/// // ... This test is ignored by the test harness (the compiler still compiles it).
/// // ... This test is ignored by the test harness (the compiler still compiles it).

View changes since the review

kantnero added 2 commits July 7, 2026 14:21
Signed-off-by: Emmanuel Ugwu <emmanuelugwu121@gmail.com>
Signed-off-by: Emmanuel Ugwu <emmanuelugwu121@gmail.com>
Comment thread library/core/src/attribute_docs.rs Outdated
///
/// * `all`: True if all given predicates are true.
/// * `any`: True if at least one of the given predicates is true.
/// * `not`: True if the predicate is false and false if the predicate is true.

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

Suggested change
/// * `not`: True if the predicate is false and false if the predicate is true.
/// * `not`: True if the predicate is false.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
Comment on lines +226 to +227
/// #[cfg_attr(all(feature = "system", feature = "disk"),
/// doc = "For module documentation, both `system`, and `disk` need to be enabled.")]

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

Suggested change
/// #[cfg_attr(all(feature = "system", feature = "disk"),
/// doc = "For module documentation, both `system`, and `disk` need to be enabled.")]
/// #[cfg_attr(
/// all(feature = "system", feature = "disk"),
/// doc = "For module documentation, both `system`, and `disk` need to be enabled.",
/// )]

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
#[doc(attribute = "used")]
//
/// The `#[used]` attribute can be used on static variables to prevent the Rust compiler
/// from optimizing them away even if they are not used anywhere in the current crate.

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

Suggested change
/// from optimizing them away even if they are not used anywhere in the current crate.
/// from optimizing them away even if they are not used in the current crate.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
/// from optimizing them away even if they are not used anywhere in the current crate.
///
/// It tells the compiler that an item is still in use or needed elsewhere and, because of this,
/// it is kept in the files generated by `rustc` when compiling.

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

Suggested change
/// it is kept in the files generated by `rustc` when compiling.
/// should be kept in the files generated by `rustc` when compiling.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated

#[doc(attribute = "should_panic")]
//
/// The `should_panic` attribute is used with the `test` attribute to indicate that a test is expected

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

Suggested change
/// The `should_panic` attribute is used with the `test` attribute to indicate that a test is expected
/// The `should_panic` attribute is used alongside the `test` attribute to indicate that a test is expected

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
/// The `should_panic` attribute is used with the `test` attribute to indicate that a test is expected
/// to panic and will fail if it doesn't.
///
/// The `should_panic` attribute may only be applied to functions annotated with the test attribute.

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

Suggested change
/// The `should_panic` attribute may only be applied to functions annotated with the test attribute.
/// The `should_panic` attribute may only be applied to functions annotated with the `test` attribute.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
Comment on lines +572 to +584
/// You can combine `#[path]` with `#[cfg]` Or even better: use `#[cfg_attr]` to load platform-specific files:
///
/// ```rust,compile_fail
/// // On Linux, the platform/linux.rs source file is loaded.
/// #[cfg(target_os = "linux")]
/// #[path = "platform/linux.rs"]
/// mod platform;
///
/// // On Windows, the platform/windows.rs source file is loaded.
/// #[cfg(target_os = "windows")]
/// #[path = "platform/windows.rs"]
/// mod platform;
/// ```

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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 whole part is actually not really useful as it doesn't provide information useful for the path attribute. Please remove it.

View changes since the review

//
/// The `#[path = "..."]` attribute tells the compiler where to find a module's source file, overriding the default
/// file lookup.
///

@GuillaumeGomez GuillaumeGomez Jul 7, 2026

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.

You need to add an explanation to how the path works (does it need to be absolute? If not, from where is it relative, etc).

View changes since the review

Signed-off-by: Emmanuel Ugwu <emmanuelugwu121@gmail.com>
@kantnero kantnero requested a review from GuillaumeGomez July 8, 2026 02:19
@traviscross traviscross added the T-lang-docs Relevant to the lang-docs team. label Jul 8, 2026
@kantnero

kantnero commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rust-bors

rust-bors Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

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

#[doc(attribute = "expect")]
//
/// The `#[expect]` attribute declares that a particular lint is expected to be emitted.
/// It is an equivalent of the `allow` attribute, except that it will fail compilation if the `expect`ed lint

@traviscross traviscross Jul 10, 2026

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.

It just warns by default; it doesn't fail compilation.

View changes since the review

Comment on lines +465 to +469
/// ```rust,compile_fail
/// #[expect(unused_variables)]
/// let name = "rust-lang";
/// println!("{name}");
/// ````

@traviscross traviscross Jul 10, 2026

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.

The rendering of compile_fail to the reader will be misleading since this only fails due to the harness defaults. Maybe it's worth adding an explicit #[deny(unfulfilled_lint_expectations)] so this will make sense to the reader.

View changes since the review

Comment on lines +502 to +505
/// // The lint group is fulfilled as the variables are unused.
/// #[expect(unused)]
/// let x = 10;
/// let y = 12;

@traviscross traviscross Jul 10, 2026

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.

The expect does not apply to let y = 12 here.

View changes since the review

/// println!("{name}");
/// ````
///
/// In the example above the variable is used, so the `expect` is unfufilled this warning is emitted:

@traviscross traviscross Jul 10, 2026

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.

Suggested change
/// In the example above the variable is used, so the `expect` is unfufilled this warning is emitted:
/// In the example above the variable is used, so the `expect` is unfulfilled this warning is emitted:

View changes since the review

/// #[expect(unused_variables)]
/// let name = "rust-lang";
/// println!("{name}");
/// ````

@traviscross traviscross Jul 10, 2026

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.

Suggested change
/// ````
/// ```

View changes since the review

/// It is an equivalent of the `allow` attribute, except that it will fail compilation if the `expect`ed lint
/// wasn't emitted.
///
/// `expect` can be overridden by `warn`, `allow`, `deny`, and `forbid`.

@traviscross traviscross Jul 10, 2026

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.

The behavior of this should probably be discussed; i.e., it doesn't cancel the expect.

View changes since the review

/// }
/// ```
///
/// For complex conditions, you can combine `all(...)`, `any(...)`, and, `not(...)`.

@traviscross traviscross Jul 10, 2026

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.

Suggested change
/// For complex conditions, you can combine `all(...)`, `any(...)`, and, `not(...)`.
/// For complex conditions, you can combine `all(...)`, `any(...)`, and `not(...)`.

View changes since the review

/// ```rust
/// #[cfg_attr(
/// all(feature = "system", feature = "disk"),
/// doc = "For module documentation, both `system`, and `disk` need to be enabled.",

@traviscross traviscross Jul 10, 2026

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.

Suggested change
/// doc = "For module documentation, both `system`, and `disk` need to be enabled.",
/// doc = "For module documentation, both `system` and `disk` need to be enabled.",

View changes since the review

///
/// For more information, see the Reference on [the `cfg_attr` attribute].
///
/// [the `cfg` attribute]: ../reference/conditional-compilation.html#the-cfg-attribute

@traviscross traviscross Jul 10, 2026

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.


#[doc(attribute = "ignore")]
//
/// The `ignore` attribute is used alongside the `test` attribute to prevent this test to be run by default.

@traviscross traviscross Jul 10, 2026

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.

Suggested change
/// The `ignore` attribute is used alongside the `test` attribute to prevent this test to be run by default.
/// The `ignore` attribute is used alongside the `test` attribute to prevent this test from being run by default.

View changes since the review

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

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-lang-docs Relevant to the lang-docs team. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants