Skip to content

Commit 1346a11

Browse files
Stabilize doc_cfg feature
1 parent 52fda2e commit 1346a11

File tree

61 files changed

+314
-485
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+314
-485
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
181181

182182
gate_doc!(
183183
"experimental" {
184-
cfg => doc_cfg
185-
auto_cfg => doc_cfg
186184
masked => doc_masked
187185
notable_trait => doc_notable_trait
188186
}

compiler/rustc_error_codes/src/error_codes/E0536.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ The `not` cfg-predicate was malformed.
55
Erroneous code example (using `cargo doc`):
66

77
```ignore, E0536 (only triggers on cargo doc)
8-
#![feature(doc_cfg)]
98
#[doc(cfg(not()))]
109
pub fn main() {
1110
@@ -15,7 +14,6 @@ pub fn main() {
1514
The `not` predicate expects one cfg-pattern. Example:
1615

1716
```
18-
#![feature(doc_cfg)]
1917
#[doc(cfg(not(target_os = "linux")))] // ok!
2018
pub fn main() {
2119

compiler/rustc_feature/src/accepted.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ declare_features! (
187187
(accepted, do_not_recommend, "1.85.0", Some(51992)),
188188
/// Allows `#[doc(alias = "...")]`.
189189
(accepted, doc_alias, "1.48.0", Some(50146)),
190+
/// Allows `#[doc(cfg(...))]`.
191+
(accepted, doc_cfg, "1.21.0", Some(43781)),
190192
/// Allows `..` in tuple (struct) patterns.
191193
(accepted, dotdot_in_tuple_patterns, "1.14.0", Some(33627)),
192194
/// Allows `..=` in patterns (RFC 1192).

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,6 @@ declare_features! (
487487
(unstable, derive_from, "1.91.0", Some(144889)),
488488
/// Allows giving non-const impls custom diagnostic messages if attempted to be used as const
489489
(unstable, diagnostic_on_const, "CURRENT_RUSTC_VERSION", Some(143874)),
490-
/// Allows `#[doc(cfg(...))]`.
491-
(unstable, doc_cfg, "1.21.0", Some(43781)),
492490
/// Allows `#[doc(masked)]`.
493491
(unstable, doc_masked, "1.21.0", Some(44027)),
494492
/// Allows features to allow target_feature to better interact with traits.

library/alloc/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,6 @@
192192
#![rustc_preserve_ub_checks]
193193
// tidy-alphabetical-end
194194
//
195-
// Rustdoc features:
196-
#![feature(doc_cfg)]
197195
// Technically, this is a bug in rustdoc: rustdoc sees the documentation on `#[lang = slice_alloc]`
198196
// blocks is for `&[T]`, which also has documentation using this feature in `core`, and gets mad
199197
// that the feature-gate isn't enabled. Ideally, it wouldn't check for the feature gate for docs

library/core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@
148148
#![feature(deprecated_suggestion)]
149149
#![feature(derive_const)]
150150
#![feature(diagnostic_on_const)]
151-
#![feature(doc_cfg)]
152151
#![feature(doc_notable_trait)]
153152
#![feature(extern_types)]
154153
#![feature(f16)]

library/std/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@
278278
#![feature(core_float_math)]
279279
#![feature(decl_macro)]
280280
#![feature(deprecated_suggestion)]
281-
#![feature(doc_cfg)]
282281
#![feature(doc_masked)]
283282
#![feature(doc_notable_trait)]
284283
#![feature(dropck_eyepatch)]

library/std_detect/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! * `s390x`: [`is_s390x_feature_detected`]
1616
1717
#![unstable(feature = "stdarch_internal", issue = "none")]
18-
#![feature(staged_api, cfg_select, doc_cfg, allow_internal_unstable)]
18+
#![feature(staged_api, cfg_select, allow_internal_unstable)]
1919
#![deny(rust_2018_idioms)]
2020
#![allow(clippy::shadow_reuse)]
2121
#![cfg_attr(test, allow(unused_imports))]

library/stdarch/crates/core_arch/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
fmt_helpers_for_derive,
1818
rustc_attrs,
1919
staged_api,
20-
doc_cfg,
2120
riscv_target_feature,
2221
arm_target_feature,
2322
mips_target_feature,

src/doc/rustdoc/src/unstable-features.md

Lines changed: 0 additions & 268 deletions
Original file line numberDiff line numberDiff line change
@@ -750,271 +750,3 @@ pass `--doctest-build-arg ARG` for each argument `ARG`.
750750
## `--generate-macro-expansion`: Generate macros expansion toggles in source code
751751

752752
This flag enables the generation of toggles to expand macros in the HTML source code pages.
753-
754-
## `#[doc(cfg)]` and `#[doc(auto_cfg)]`
755-
756-
This feature aims at providing rustdoc users the possibility to add visual markers to the rendered documentation to know under which conditions an item is available (currently possible through the following unstable feature: `doc_cfg`).
757-
758-
It does not aim to allow having a same item with different `cfg`s to appear more than once in the generated documentation.
759-
760-
It does not aim to document items which are *inactive* under the current configuration (i.e., “`cfg`ed out”).
761-
762-
This features adds the following attributes:
763-
764-
* `#[doc(auto_cfg)]`/`#[doc(auto_cfg = true)]`/`#[doc(auto_cfg = false)]`
765-
* `#[doc(cfg(...))]`
766-
* `#![doc(auto_cfg(hide(...)))]` / `#[doc(auto_cfg(show(...)))]`
767-
768-
All of these attributes can be added to a module or to the crate root, and they will be inherited by the child items unless another attribute overrides it. This is why "opposite" attributes like `auto_cfg(hide(...))` and `auto_cfg(show(...))` are provided: they allow a child item to override its parent.
769-
770-
### `#[doc(cfg(...))]`
771-
772-
This attribute provides a standardized format to override `#[cfg()]` attributes to document conditionally available items. Example:
773-
774-
```rust,ignore (nightly)
775-
// the "real" cfg condition
776-
#[cfg(feature = "futures-io")]
777-
// the `doc(cfg())` so it's displayed to the readers
778-
#[doc(cfg(feature = "futures-io"))]
779-
pub mod futures {}
780-
```
781-
782-
It will display in the documentation for this module:
783-
784-
```text
785-
This is supported on feature="futures-io" only.
786-
```
787-
788-
You can use it to display information in generated documentation, whether or not there is a `#[cfg()]` attribute:
789-
790-
```rust,ignore (nightly)
791-
#[doc(cfg(feature = "futures-io"))]
792-
pub mod futures {}
793-
```
794-
795-
It will be displayed exactly the same as the previous code.
796-
797-
This attribute has the same syntax as conditional compilation, but it only causes documentation to be added. This means `#[doc(cfg(not(windows)))]` will not cause your docs to be hidden on non-windows targets, even though `#[cfg(not(windows))]` does do that.
798-
799-
If `doc(auto_cfg)` is enabled on the item, `doc(cfg)` will override it anyway so in the two previous examples, even if the `doc(auto_cfg)` feature was enabled, it would still display the same thing.
800-
801-
This attribute works on modules and on items.
802-
803-
### `#[doc(auto_cfg(hide(...)))]`
804-
805-
This attribute is used to prevent some `cfg` to be generated in the visual markers. It only applies to `#[doc(auto_cfg = true)]`, not to `#[doc(cfg(...))]`. So in the previous example:
806-
807-
```rust,ignore (nightly)
808-
#[cfg(any(unix, feature = "futures-io"))]
809-
pub mod futures {}
810-
```
811-
812-
It currently displays both `unix` and `feature = "futures-io"` into the documentation, which is not great. To prevent the `unix` cfg to ever be displayed, you can use this attribute at the crate root level:
813-
814-
```rust,ignore (nightly)
815-
#![doc(auto_cfg(hide(unix)))]
816-
```
817-
818-
Or directly on a given item/module as it covers any of the item's descendants:
819-
820-
```rust,ignore (nightly)
821-
#[doc(auto_cfg(hide(unix)))]
822-
#[cfg(any(unix, feature = "futures-io"))]
823-
pub mod futures {
824-
// `futures` and all its descendants won't display "unix" in their cfgs.
825-
}
826-
```
827-
828-
Then, the `unix` cfg will never be displayed into the documentation.
829-
830-
Rustdoc currently hides `doc` and `doctest` attributes by default and reserves the right to change the list of "hidden by default" attributes.
831-
832-
The attribute accepts only a list of identifiers or key/value items. So you can write:
833-
834-
```rust,ignore (nightly)
835-
#[doc(auto_cfg(hide(unix, doctest, feature = "something")))]
836-
#[doc(auto_cfg(hide()))]
837-
```
838-
839-
But you cannot write:
840-
841-
```rust,ignore (nightly)
842-
#[doc(auto_cfg(hide(not(unix))))]
843-
```
844-
845-
So if we use `doc(auto_cfg(hide(unix)))`, it means it will hide all mentions of `unix`:
846-
847-
```rust,ignore (nightly)
848-
#[cfg(unix)] // nothing displayed
849-
#[cfg(any(unix))] // nothing displayed
850-
#[cfg(any(unix, windows))] // only `windows` displayed
851-
```
852-
853-
However, it only impacts the `unix` cfg, not the feature:
854-
855-
```rust,ignore (nightly)
856-
#[cfg(feature = "unix")] // `feature = "unix"` is displayed
857-
```
858-
859-
If `cfg_auto(show(...))` and `cfg_auto(hide(...))` are used to show/hide a same `cfg` on a same item, it'll emit an error. Example:
860-
861-
```rust,ignore (nightly)
862-
#[doc(auto_cfg(hide(unix)))]
863-
#[doc(auto_cfg(show(unix)))] // Error!
864-
pub fn foo() {}
865-
```
866-
867-
Using this attribute will re-enable `auto_cfg` if it was disabled at this location:
868-
869-
```rust,ignore (nightly)
870-
#[doc(auto_cfg = false)] // Disabling `auto_cfg`
871-
pub fn foo() {}
872-
```
873-
874-
And using `doc(auto_cfg)` will re-enable it:
875-
876-
```rust,ignore (nightly)
877-
#[doc(auto_cfg = false)] // Disabling `auto_cfg`
878-
pub mod module {
879-
#[doc(auto_cfg(hide(unix)))] // `auto_cfg` is re-enabled.
880-
pub fn foo() {}
881-
}
882-
```
883-
884-
However, using `doc(auto_cfg = ...)` and `doc(auto_cfg(...))` on the same item will emit an error:
885-
886-
```rust,ignore (nightly)
887-
#[doc(auto_cfg = false)]
888-
#[doc(auto_cfg(hide(unix)))] // error
889-
pub fn foo() {}
890-
```
891-
892-
The reason behind this is that `doc(auto_cfg = ...)` enables or disables the feature, whereas `doc(auto_cfg(...))` enables it unconditionally, making the first attribute to appear useless as it will be overidden by the next `doc(auto_cfg)` attribute.
893-
894-
### `#[doc(auto_cfg(show(...)))]`
895-
896-
This attribute does the opposite of `#[doc(auto_cfg(hide(...)))]`: if you used `#[doc(auto_cfg(hide(...)))]` and want to revert its effect on an item and its descendants, you can use `#[doc(auto_cfg(show(...)))]`.
897-
It only applies to `#[doc(auto_cfg = true)]`, not to `#[doc(cfg(...))]`.
898-
899-
For example:
900-
901-
```rust,ignore (nightly)
902-
#[doc(auto_cfg(hide(unix)))]
903-
#[cfg(any(unix, feature = "futures-io"))]
904-
pub mod futures {
905-
// `futures` and all its descendants won't display "unix" in their cfgs.
906-
#[doc(auto_cfg(show(unix)))]
907-
pub mod child {
908-
// `child` and all its descendants will display "unix" in their cfgs.
909-
}
910-
}
911-
```
912-
913-
The attribute accepts only a list of identifiers or key/value items. So you can write:
914-
915-
```rust,ignore (nightly)
916-
#[doc(auto_cfg(show(unix, doctest, feature = "something")))]
917-
#[doc(auto_cfg(show()))]
918-
```
919-
920-
But you cannot write:
921-
922-
```rust,ignore (nightly)
923-
#[doc(auto_cfg(show(not(unix))))]
924-
```
925-
926-
If `auto_cfg(show(...))` and `auto_cfg(hide(...))` are used to show/hide a same `cfg` on a same item, it'll emit an error. Example:
927-
928-
```rust,ignore (nightly)
929-
#[doc(auto_cfg(show(unix)))]
930-
#[doc(auto_cfg(hide(unix)))] // Error!
931-
pub fn foo() {}
932-
```
933-
934-
Using this attribute will re-enable `auto_cfg` if it was disabled at this location:
935-
936-
```rust,ignore (nightly)
937-
#[doc(auto_cfg = false)] // Disabling `auto_cfg`
938-
#[doc(auto_cfg(show(unix)))] // `auto_cfg` is re-enabled.
939-
pub fn foo() {}
940-
```
941-
942-
### `#[doc(auto_cfg)`/`#[doc(auto_cfg = true)]`/`#[doc(auto_cfg = false)]`
943-
944-
By default, `#[doc(auto_cfg)]` is enabled at the crate-level. When it's enabled, Rustdoc will automatically display `cfg(...)` compatibility information as-if the same `#[doc(cfg(...))]` had been specified.
945-
946-
This attribute impacts the item on which it is used and its descendants.
947-
948-
So if we take back the previous example:
949-
950-
```rust
951-
#[cfg(feature = "futures-io")]
952-
pub mod futures {}
953-
```
954-
955-
There's no need to "duplicate" the `cfg` into a `doc(cfg())` to make Rustdoc display it.
956-
957-
In some situations, the detailed conditional compilation rules used to implement the feature might not serve as good documentation (for example, the list of supported platforms might be very long, and it might be better to document them in one place). To turn it off, add the `#[doc(auto_cfg = false)]` attribute on the item.
958-
959-
If no argument is specified (ie `#[doc(auto_cfg)]`), it's the same as writing `#[doc(auto_cfg = true)]`.
960-
961-
## Inheritance
962-
963-
Rustdoc merges `cfg` attributes from parent modules to its children. For example, in this case, the module `non_unix` will describe the entire compatibility matrix for the module, and not just its directly attached information:
964-
965-
```rust,ignore (nightly)
966-
#[doc(cfg(any(windows, unix)))]
967-
pub mod desktop {
968-
#[doc(cfg(not(unix)))]
969-
pub mod non_unix {
970-
// ...
971-
}
972-
}
973-
```
974-
975-
This code will display:
976-
977-
```text
978-
Available on (Windows or Unix) and non-Unix only.
979-
```
980-
981-
### Re-exports and inlining
982-
983-
`cfg` attributes of a re-export are never merged with the re-exported item(s) attributes except if the re-export has the `#[doc(inline)]` attribute. In this case, the `cfg` of the re-exported item will be merged with the re-export's.
984-
985-
When talking about "attributes merge", we mean that if the re-export has `#[cfg(unix)]` and the re-exported item has `#[cfg(feature = "foo")]`, you will only see `cfg(unix)` on the re-export and only `cfg(feature = "foo")` on the re-exported item, unless the re-export has `#[doc(inline)]`, then you will only see the re-exported item with both `cfg(unix)` and `cfg(feature = "foo")`.
986-
987-
Example:
988-
989-
```rust,ignore (nightly)
990-
#[doc(cfg(any(windows, unix)))]
991-
pub mod desktop {
992-
#[doc(cfg(not(unix)))]
993-
pub mod non_unix {
994-
// code
995-
}
996-
}
997-
998-
#[doc(cfg(target_os = "freebsd"))]
999-
pub use desktop::non_unix as non_unix_desktop;
1000-
#[doc(cfg(target_os = "macos"))]
1001-
#[doc(inline)]
1002-
pub use desktop::non_unix as inlined_non_unix_desktop;
1003-
```
1004-
1005-
In this example, `non_unix_desktop` will only display `cfg(target_os = "freeebsd")` and not display any `cfg` from `desktop::non_unix`.
1006-
1007-
On the contrary, `inlined_non_unix_desktop` will have cfgs from both the re-export and the re-exported item.
1008-
1009-
So that also means that if a crate re-exports a foreign item, unless it has `#[doc(inline)]`, the `cfg` and `doc(cfg)` attributes will not be visible:
1010-
1011-
```rust,ignore (nightly)
1012-
// dep:
1013-
#[cfg(feature = "a")]
1014-
pub struct S;
1015-
1016-
// crate using dep:
1017-
1018-
// There will be no mention of `feature = "a"` in the documentation.
1019-
pub use dep::S as Y;
1020-
```

0 commit comments

Comments
 (0)