Skip to content

Commit 043c5f3

Browse files
Apply first round of review comments
1 parent e6b88c1 commit 043c5f3

File tree

1 file changed

+48
-50
lines changed

1 file changed

+48
-50
lines changed

text/000-rustdoc-cfgs-handling.md

+48-50
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ The end goal being to provide this information automatically so that the documen
2929

3030
This RFC proposes to add the following attributes:
3131

32+
* `#![doc(auto_cfg(enable))]`/`#[doc(auto_cfg(disable))]`
33+
34+
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+
3236
* `#[doc(cfg(...))]`
3337

3438
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.
3539

3640
The syntax of this attribute is the same as the syntax of the [`#[cfg(unix)]` attribute][cfg attribute] used for conditional compilation.
3741

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.
41-
42-
* `#[doc(cfg_hide(...))]` / `#[doc(cfg_show(...))]`
42+
* `#![doc(cfg_hide(...))]` / `#[doc(cfg_show(...))]`
4343

4444
These attributes suppress or un-suppress the `auto_cfg` behavior for a particular configuration predicate.
4545

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.
4747

4848
[cfg attribute]: https://doc.rust-lang.org/reference/conditional-compilation.html
4949
[`windows` crate]: https://docs.rs/windows/latest/windows/
@@ -56,65 +56,44 @@ All of these attributes can be added to a module or to the crate root, and they
5656

5757
## The attributes
5858

59-
### `#[doc(cfg(...))]`
59+
### `#[doc(auto_cfg(enable))]`/`#[doc(auto_cfg(disable))]`
6060

61-
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:
6264

6365
```rust
64-
// the "real" cfg condition
6566
#[cfg(feature = "futures-io")]
66-
// the `doc(cfg())` so it's displayed to the readers
67-
#[doc(cfg(feature = "futures-io"))]
6867
pub mod futures {}
6968
```
7069

71-
It will display in the documentation for this module:
72-
73-
![This is supported on feature="futures-io" only.](https://user-images.githubusercontent.com/81079/89731116-d7b7ce00-da44-11ea-87c6-022d192d6eca.png)
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.
7871

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.
8073

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(...))]`
8275

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:
8477

8578
```rust
79+
// the "real" cfg condition
8680
#[cfg(feature = "futures-io")]
81+
// the `doc(cfg())` so it's displayed to the readers
82+
#[doc(cfg(feature = "futures-io"))]
8783
pub mod futures {}
8884
```
8985

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:
9187

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+
![This is supported on feature="futures-io" only.](https://user-images.githubusercontent.com/81079/89731116-d7b7ce00-da44-11ea-87c6-022d192d6eca.png)
9389

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.
9591

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-
pub mod foo {
105-
// We re-enable the feature on `Bar` and on all its descendants.
106-
#[doc(auto_cfg)]
107-
pub struct Bar {
108-
pub f: 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.
11493

11594
### `#[doc(cfg_hide(...))]`
11695

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:
11897

11998
```rust
12099
#[cfg(any(doc, feature = "futures-io"))]
@@ -154,9 +133,20 @@ But you cannot write:
154133
#[doc(cfg_hide(not(doc)))]
155134
```
156135

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+
pub fn foo() {}
142+
```
143+
157144
### `#[doc(cfg_show(...))]`
158145

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:
160150

161151
```rust
162152
#[doc(cfg_hide(doc))]
@@ -183,6 +173,14 @@ But you cannot write:
183173
#[doc(cfg_show(not(doc)))]
184174
```
185175

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+
pub fn foo() {}
182+
```
183+
186184
## Inheritance
187185

188186
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 {
203201

204202
### Re-exports and inlining
205203

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:
207209

208210
```rust
209211
#[doc(cfg(any(windows, unix)))]
@@ -274,10 +276,6 @@ When re-exporting items with different cfgs there are two things that can happen
274276
275277
Of course, the above example is equivalent to "available on **Windows** only."
276278

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.
278-
279-
[boolean satisfiability]: https://en.wikipedia.org/wiki/Boolean_satisfiability_problem
280-
281-
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.
282280

283281
This is tracked in issue [rust-lang/rust#104991](https://github.com/rust-lang/rust/issues/104991).

0 commit comments

Comments
 (0)