You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When this is turned on, `#[cfg]` attributes are shown in documentation just like `#[doc(cfg)]` attributes are. By default, `auto_cfg` will be enabled.
35
+
32
36
*`#[doc(cfg(...))]`
33
37
34
38
This attribute is used to document the operating systems, feature flags, and build profiles where an item is available. For example, `#[doc(cfg(unix))` will add a tag that says "this is supported on **unix** only" to the item.
35
39
36
40
The syntax of this attribute is the same as the syntax of the [`#[cfg(unix)]` attribute][cfg attribute] used for conditional compilation.
37
41
38
-
*`#[doc(auto_cfg)]`/`#[doc(no_auto_cfg)]`
39
-
40
-
When this is turned on, `#[cfg]` attributes are shown in documentation just like `#[doc(cfg)]` attributes are.
These attributes suppress or un-suppress the `auto_cfg` behavior for a particular configuration predicate.
45
45
46
-
For example, `#[doc(cfg_hide(windows))]`shall be used in newer versions of the [`windows` crate] to prevent the "this is supported on **windows** only" tag from being shown on every single item.
46
+
For example, `#[doc(cfg_hide(windows))]`could be used in newer versions of the [`windows` crate] to prevent the "this is supported on **windows** only" tag from being shown on every single item.
This attribute provides a standardized format to document conditionally available items. Example:
61
+
This is a crate-level attribute. 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.
62
+
63
+
So if we take back the previous example:
62
64
63
65
```rust
64
-
// the "real" cfg condition
65
66
#[cfg(feature ="futures-io")]
66
-
// the `doc(cfg())` so it's displayed to the readers
67
-
#[doc(cfg(feature ="futures-io"))]
68
67
pubmodfutures {}
69
68
```
70
69
71
-
It will display in the documentation for this module:
72
-
73
-

74
-
75
-
This attribute has the same syntax as conditional compilation, but it only causes documentation to be added. This means `#[doc(cfg(false))` will not cause your docs to be hidden, even though `#[cfg(false)]` does do that.
76
-
77
-
This attribute works on modules and on items but cannot be used at the crate root level.
70
+
There's no need to "duplicate" the `cfg` into a `doc(cfg())` to make Rustdoc display it.
78
71
79
-
### `#[doc(auto_cfg)]`/`#[doc(no_auto_cfg)]`
72
+
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(disable))]` attribute at the crate-level.
80
73
81
-
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.
74
+
### `#[doc(cfg(...))]`
82
75
83
-
So if we take back the previous example:
76
+
This attribute provides a standardized format to override `#[cfg()]` attributes to document conditionally available items. Example:
84
77
85
78
```rust
79
+
// the "real" cfg condition
86
80
#[cfg(feature ="futures-io")]
81
+
// the `doc(cfg())` so it's displayed to the readers
82
+
#[doc(cfg(feature ="futures-io"))]
87
83
pubmodfutures {}
88
84
```
89
85
90
-
There's no need to "duplicate" the `cfg` into a `doc(cfg())` to make Rustdoc display it.
86
+
It will display in the documentation for this module:
91
87
92
-
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(no_auto_cfg)]` attribute.
88
+

93
89
94
-
Both `#[doc(auto_cfg)]` and `#[doc(no_auto_cfg)]` attributes impact all there descendants. You can then enable/disable them by using the opposite attribute on a given item. They can be used as follows:
90
+
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.
95
91
96
-
```rust
97
-
// As an inner attribute, all this module's descendants will have this feature
98
-
// enabled.
99
-
#![doc(auto_cfg)]
100
-
101
-
// As an outer attribute. So in this case, `foo` and all its
102
-
// descendants won't have the `auto_cfg` feature enabled.
103
-
#[doc(no_auto_cfg)]
104
-
pubmodfoo {
105
-
// We re-enable the feature on `Bar` and on all its descendants.
106
-
#[doc(auto_cfg)]
107
-
pubstructBar {
108
-
pubf:u32,
109
-
}
110
-
}
111
-
```
112
-
113
-
As mentioned, both attributes can be used on modules, items and crate root level.
92
+
This attribute works on modules and on items but cannot be used at the crate root level.
114
93
115
94
### `#[doc(cfg_hide(...))]`
116
95
117
-
This attribute is used to prevent some `cfg` to be generated in the visual markers. So in the previous example:
96
+
This attribute is used to prevent some `cfg` to be generated in the visual markers. It only applies to `#[doc(auto_cfg(enable))]`, not to `#[doc(cfg(...))]`. So in the previous example:
118
97
119
98
```rust
120
99
#[cfg(any(doc, feature ="futures-io"))]
@@ -154,9 +133,20 @@ But you cannot write:
154
133
#[doc(cfg_hide(not(doc)))]
155
134
```
156
135
136
+
If `cfg_show` and `cfg_hide` are used to show/hide a same `cfg` on a same item, it'll emit an error. Example:
137
+
138
+
```rust
139
+
#[doc(cfg_hide(unix))]
140
+
#[doc(cfg_show(unix))] // Error!
141
+
pubfnfoo() {}
142
+
```
143
+
157
144
### `#[doc(cfg_show(...))]`
158
145
159
-
This attribute does the opposite of `#[doc(cfg_hide(...))]`: if you used `#[doc(cfg_hide(...))]` and want to revert its effect on an item and its descendants, you can use `#[doc(cfg_show(...))]`:
146
+
This attribute does the opposite of `#[doc(cfg_hide(...))]`: if you used `#[doc(cfg_hide(...))]` and want to revert its effect on an item and its descendants, you can use `#[doc(cfg_show(...))]`.
147
+
It only applies to `#[doc(auto_cfg(enable))]`, not to `#[doc(cfg(...))]`.
148
+
149
+
For example:
160
150
161
151
```rust
162
152
#[doc(cfg_hide(doc))]
@@ -183,6 +173,14 @@ But you cannot write:
183
173
#[doc(cfg_show(not(doc)))]
184
174
```
185
175
176
+
If `cfg_show` and `cfg_hide` are used to show/hide a same `cfg` on a same item, it'll emit an error. Example:
177
+
178
+
```rust
179
+
#[doc(cfg_hide(unix))]
180
+
#[doc(cfg_show(unix))] // Error!
181
+
pubfnfoo() {}
182
+
```
183
+
186
184
## Inheritance
187
185
188
186
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:
@@ -203,7 +201,11 @@ pub mod desktop {
203
201
204
202
### Re-exports and inlining
205
203
206
-
`cfg` attributes of a re-export are never merged the re-exported item(s). If `#[doc(inline)]` attribute is used on a re-export, the `cfg` of the re-exported item will be merged with the re-export's.
204
+
`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.
205
+
206
+
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")`.
207
+
208
+
Example:
207
209
208
210
```rust
209
211
#[doc(cfg(any(windows, unix)))]
@@ -274,10 +276,6 @@ When re-exporting items with different cfgs there are two things that can happen
274
276
275
277
Of course, the above example is equivalent to "available on **Windows** only."
276
278
277
-
Making this actually work all the time is equivalent to a [boolean satisfiability] check, coliquially called a "SAT problem," and can take exponential time.
We probably don't want to make promises one way or the other about whether rustdoc does this, but for compatibility's sake, Rustdoc does promise that `#[doc(cfg(false))` will not hide the documentation. This means simplification can be added, and it won't cause docs to mysteriously vanish.
279
+
We probably don't want to make promises one way or the other about whether rustdoc does this, but for compatibility's sake, Rustdoc does promise that `#[doc(cfg(false))]` will not hide the documentation. This means simplification can be added, and it won't cause docs to mysteriously vanish.
282
280
283
281
This is tracked in issue [rust-lang/rust#104991](https://github.com/rust-lang/rust/issues/104991).
0 commit comments