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
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 <[email protected]>
@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

Comment thread library/core/src/attribute_docs.rs Outdated
Comment on lines +403 to +404
/// To confirm, we compile this program into an object file, you'll see that `FOO` makes it to the
/// object file but `BAR` doesn't. Neither static variable is used by the program.

@GuillaumeGomez GuillaumeGomez Jul 6, 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
/// To confirm, we compile this program into an object file, you'll see that `FOO` makes it to the
/// object file but `BAR` doesn't. Neither static variable is used by the program.
/// To confirm, we compile this code and then we confirm that `FOO` is present in the generated files, but not `BAR`.

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
Comment on lines +426 to +427
/// The `ignore` attribute is used with the `test` attribute to stop the test harness from
/// executing a function as a test. The `ignore` attribute may only be applied to functions

@GuillaumeGomez GuillaumeGomez Jul 6, 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 `ignore` attribute is used with the `test` attribute to stop the test harness from
/// executing a function as a test. The `ignore` attribute may only be applied to functions
/// The `ignore` attribute is used alongside the `test` attribute to prevent this test to be run by default. The `ignore` attribute may only be applied to functions

View changes since the review

Comment thread library/core/src/attribute_docs.rs Outdated
Comment on lines +487 to +488
/// Multiple lints can be set to `expect` at once, each one is expected separately. For a lint group, it’s enough
/// if one lint inside the group has been emitted:

@GuillaumeGomez GuillaumeGomez Jul 6, 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
/// Multiple lints can be set to `expect` at once, each one is expected separately. For a lint group, it’s enough
/// if one lint inside the group has been emitted:
/// Multiple lints can be set to `expect` at once, each one is expected separately.

View changes since the review

Comment thread library/core/src/attribute_docs.rs
Comment thread library/core/src/attribute_docs.rs Outdated
Comment on lines +549 to +563
/// running 1 test
/// test test_without_panic - should panic ... FAILED
///
/// failures:
///
/// ---- test_without_panic stdout ----
/// A test that does not panic
/// note: test did not panic as expected at src/main.rs:3:4
///
/// failures:
/// test_without_panic
///
/// test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
///
/// error: test failed, to rerun pass `--bin panic`

@GuillaumeGomez GuillaumeGomez Jul 6, 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
/// running 1 test
/// test test_without_panic - should panic ... FAILED
///
/// failures:
///
/// ---- test_without_panic stdout ----
/// A test that does not panic
/// note: test did not panic as expected at src/main.rs:3:4
///
/// failures:
/// test_without_panic
///
/// test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
///
/// error: test failed, to rerun pass `--bin panic`
/// test test_without_panic - should panic ... FAILED

View changes since the review

@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 <[email protected]>
@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 <[email protected]>
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 <[email protected]>
Signed-off-by: Emmanuel Ugwu <[email protected]>
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 <[email protected]>
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-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.

5 participants