Attribute documentation for used, expect, should_panic, cfg_attr, and path#158664
Attribute documentation for used, expect, should_panic, cfg_attr, and path#158664kantnero wants to merge 16 commits into
used, expect, should_panic, cfg_attr, and path#158664Conversation
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
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. |
This comment has been minimized.
This comment has been minimized.
…h, and ignore Signed-off-by: Emmanuel Ugwu <[email protected]>
…h, and ignore Signed-off-by: Emmanuel Ugwu <[email protected]>
Signed-off-by: Emmanuel Ugwu <[email protected]>
| /// 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. |
There was a problem hiding this comment.
| /// configuration predicate is true. If the condition is false, the attributes are ignored. |
| /// | ||
| /// ```rust | ||
| /// // This function is annotated with `#[test]` only when testing is active. | ||
| /// #[cfg_attr(test, test)] |
There was a problem hiding this comment.
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.
| /// } | ||
| /// ``` | ||
| /// | ||
| /// The predicate uses the same syntax as [`cfg`]. For complex conditions, you can combine |
There was a problem hiding this comment.
Does it link to the right cfg page?
There was a problem hiding this comment.
You still didn't answer this question. When you generate doc locally, what is the link of this item?
There was a problem hiding this comment.
/rust/build/x86_64-unknown-linux-gnu/doc/core/attribute.cfg.html
There was a problem hiding this comment.
I just checked it links to macro cfg
There was a problem hiding this comment.
So it's not the right link.
There was a problem hiding this comment.
Yes, I have removed it
| /// 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. |
There was a problem hiding this comment.
| /// 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`. |
| /// 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 |
There was a problem hiding this comment.
| /// 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 |
| /// 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: |
There was a problem hiding this comment.
| /// 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. |
| /// 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` |
There was a problem hiding this comment.
| /// 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 |
|
Reminder, once the PR becomes ready for a review, use |
Signed-off-by: Emmanuel Ugwu <[email protected]>
Signed-off-by: Emmanuel Ugwu <[email protected]>
| /// Used for conditional application of attributes. | ||
| /// | ||
| /// The `cfg_attr` attribute is used to conditionally apply one or more attributes to an item. |
There was a problem hiding this comment.
| /// 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. |
| /// Example: | ||
| /// | ||
| /// ```rust | ||
| /// // This function is annotated with `#[test]` only when on Linux platform. |
There was a problem hiding this comment.
| /// // This function is annotated with `#[test]` only when on Linux platform. | |
| /// // This function is annotated with `#[test]` only on Linux platforms. |
| /// | ||
| /// ```rust | ||
| /// #[cfg_attr(all(feature = "system", feature = "disk"), | ||
| /// doc = "This module is only accessible when both `system` and `disk` are enabled")] |
There was a problem hiding this comment.
The doc message is wrong considering that there is no cfg in here.
| /// 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. |
There was a problem hiding this comment.
| /// 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. |
| /// #[test] | ||
| /// #[ignore] | ||
| /// fn test() { | ||
| /// // ... This test is ignored by the test harness (the compiler still compiles it). |
There was a problem hiding this comment.
| /// // ... 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). |
Signed-off-by: Emmanuel Ugwu <[email protected]>
Signed-off-by: Emmanuel Ugwu <[email protected]>
| /// | ||
| /// * `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. |
There was a problem hiding this comment.
| /// * `not`: True if the predicate is false and false if the predicate is true. | |
| /// * `not`: True if the predicate is false. |
| /// #[cfg_attr(all(feature = "system", feature = "disk"), | ||
| /// doc = "For module documentation, both `system`, and `disk` need to be enabled.")] |
There was a problem hiding this comment.
| /// #[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.", | |
| /// )] |
| #[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. |
There was a problem hiding this comment.
| /// 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. |
| /// 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. |
There was a problem hiding this comment.
| /// it is kept in the files generated by `rustc` when compiling. | |
| /// should be kept in the files generated by `rustc` when compiling. |
|
|
||
| #[doc(attribute = "should_panic")] | ||
| // | ||
| /// The `should_panic` attribute is used with the `test` attribute to indicate that a test is expected |
There was a problem hiding this comment.
| /// 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 |
| /// 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. |
There was a problem hiding this comment.
| /// 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. |
| /// 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; | ||
| /// ``` |
There was a problem hiding this comment.
This whole part is actually not really useful as it doesn't provide information useful for the path attribute. Please remove it.
| // | ||
| /// The `#[path = "..."]` attribute tells the compiler where to find a module's source file, overriding the default | ||
| /// file lookup. | ||
| /// |
There was a problem hiding this comment.
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).
Signed-off-by: Emmanuel Ugwu <[email protected]>
View all comments
Attribute documentation for
used,expect,should_panic,cfg_attr, andpathusing the#[doc(attribute = "")]mechanismTested with: ./x test library/std --doc
r? @GuillaumeGomez
cc @traviscross @fmease