Skip to content

Commit e32328b

Browse files
Rollup merge of #89596 - GuillaumeGomez:implicit-doc-cfg, r=jyn514
Make cfg imply doc(cfg) This is a reopening of #79341, rebased and modified a bit (we made a lot of refactoring in rustdoc's types so they needed to be reflected in this PR as well): * `hidden_cfg` is now in the `Cache` instead of `DocContext` because `cfg` information isn't stored anymore on `clean::Attributes` type but instead computed on-demand, so we need this information in later parts of rustdoc. * I removed the `bool_to_options` feature (which makes the code a bit simpler to read for `SingleExt` trait implementation. * I updated the version for the feature. There is only one thing I couldn't figure out: [this comment](#79341 (comment)) > I think I'll likely scrap the whole `SingleExt` extension trait as the diagnostics for 0 and >1 items should be different. How/why should they differ? EDIT: this part has been solved, the current code was fine, just needed a little simplification. cc `@Nemo157` r? `@jyn514` Original PR description: This is only active when the `doc_cfg` feature is active. The implicit cfg can be overridden via `#[doc(cfg(...))]`, so e.g. to hide a `#[cfg]` you can use something like: ```rust #[cfg(unix)] #[doc(cfg(all()))] pub struct Unix; ``` By adding `#![doc(cfg_hide(foobar))]` to the crate attributes the cfg `#[cfg(foobar)]` (and _only_ that _exact_ cfg) will not be implicitly treated as a `doc(cfg)` to render a message in the documentation.
2 parents de0b4f9 + 09c7688 commit e32328b

File tree

23 files changed

+247
-10
lines changed

23 files changed

+247
-10
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
279279

280280
gate_doc!(
281281
cfg => doc_cfg
282+
cfg_hide => doc_cfg_hide
282283
masked => doc_masked
283284
notable_trait => doc_notable_trait
284285
keyword => doc_keyword

compiler/rustc_feature/src/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,9 @@ declare_features! (
675675
/// Allows `#[track_caller]` on closures and generators.
676676
(active, closure_track_caller, "1.57.0", Some(87417), None),
677677

678+
/// Allows `#[doc(cfg_hide(...))]`.
679+
(active, doc_cfg_hide, "1.57.0", Some(43781), None),
680+
678681
// -------------------------------------------------------------------------
679682
// feature-group-end: actual feature gates
680683
// -------------------------------------------------------------------------

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ impl CheckAttrVisitor<'tcx> {
938938
// plugins: removed, but rustdoc warns about it itself
939939
sym::alias
940940
| sym::cfg
941+
| sym::cfg_hide
941942
| sym::hidden
942943
| sym::html_favicon_url
943944
| sym::html_logo_url

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ symbols! {
399399
cfg_attr_multi,
400400
cfg_doctest,
401401
cfg_eval,
402+
cfg_hide,
402403
cfg_panic,
403404
cfg_sanitize,
404405
cfg_target_abi,
@@ -547,6 +548,7 @@ symbols! {
547548
doc,
548549
doc_alias,
549550
doc_cfg,
551+
doc_cfg_hide,
550552
doc_keyword,
551553
doc_masked,
552554
doc_notable_trait,

library/alloc/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
6868
test(no_crate_inject, attr(allow(unused_variables), deny(warnings)))
6969
)]
70+
#![cfg_attr(
71+
not(bootstrap),
72+
doc(cfg_hide(not(test), not(any(test, bootstrap)), target_has_atomic = "ptr"))
73+
)]
7074
#![no_std]
7175
#![needs_allocator]
7276
#![warn(deprecated_in_future)]
@@ -146,6 +150,8 @@
146150
#![feature(associated_type_bounds)]
147151
#![feature(slice_group_by)]
148152
#![feature(decl_macro)]
153+
#![feature(doc_cfg)]
154+
#![cfg_attr(not(bootstrap), feature(doc_cfg_hide))]
149155
// Allow testing this library
150156

151157
#[cfg(test)]

library/core/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@
6060
test(no_crate_inject, attr(deny(warnings))),
6161
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
6262
)]
63+
#![cfg_attr(
64+
not(bootstrap),
65+
doc(cfg_hide(
66+
not(test),
67+
target_pointer_width = "16",
68+
target_pointer_width = "32",
69+
target_pointer_width = "64",
70+
target_has_atomic = "8",
71+
target_has_atomic = "16",
72+
target_has_atomic = "32",
73+
target_has_atomic = "64",
74+
target_has_atomic = "ptr",
75+
target_has_atomic_equal_alignment = "8",
76+
target_has_atomic_equal_alignment = "16",
77+
target_has_atomic_equal_alignment = "32",
78+
target_has_atomic_equal_alignment = "64",
79+
target_has_atomic_equal_alignment = "ptr",
80+
target_has_atomic_load_store = "8",
81+
target_has_atomic_load_store = "16",
82+
target_has_atomic_load_store = "32",
83+
target_has_atomic_load_store = "64",
84+
target_has_atomic_load_store = "ptr",
85+
))
86+
)]
6387
#![no_core]
6488
//
6589
// Lints:
@@ -133,6 +157,7 @@
133157
#![feature(doc_notable_trait)]
134158
#![feature(doc_primitive)]
135159
#![feature(exhaustive_patterns)]
160+
#![cfg_attr(not(bootstrap), feature(doc_cfg_hide))]
136161
#![feature(extern_types)]
137162
#![feature(fundamental)]
138163
#![feature(if_let_guard)]

library/std/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
test(no_crate_inject, attr(deny(warnings))),
196196
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
197197
)]
198+
#![cfg_attr(not(bootstrap), doc(cfg_hide(not(test), not(any(test, bootstrap)))))]
198199
// Don't link to std. We are std.
199200
#![no_std]
200201
#![warn(deprecated_in_future)]
@@ -263,6 +264,7 @@
263264
#![feature(custom_test_frameworks)]
264265
#![feature(decl_macro)]
265266
#![feature(doc_cfg)]
267+
#![cfg_attr(not(bootstrap), feature(doc_cfg_hide))]
266268
#![feature(doc_keyword)]
267269
#![feature(doc_masked)]
268270
#![feature(doc_notable_trait)]

library/std/src/os/raw/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ macro_rules! type_alias {
4646
}
4747

4848
type_alias! { "char.md", c_char = u8, NonZero_c_char = NonZeroU8;
49+
#[doc(cfg(all()))]
4950
#[cfg(any(
5051
all(
5152
target_os = "linux",
@@ -88,6 +89,7 @@ type_alias! { "char.md", c_char = u8, NonZero_c_char = NonZeroU8;
8889
all(target_os = "fuchsia", target_arch = "aarch64")
8990
))]}
9091
type_alias! { "char.md", c_char = i8, NonZero_c_char = NonZeroI8;
92+
#[doc(cfg(all()))]
9193
#[cfg(not(any(
9294
all(
9395
target_os = "linux",
@@ -136,12 +138,16 @@ type_alias! { "ushort.md", c_ushort = u16, NonZero_c_ushort = NonZeroU16; }
136138
type_alias! { "int.md", c_int = i32, NonZero_c_int = NonZeroI32; }
137139
type_alias! { "uint.md", c_uint = u32, NonZero_c_uint = NonZeroU32; }
138140
type_alias! { "long.md", c_long = i32, NonZero_c_long = NonZeroI32;
141+
#[doc(cfg(all()))]
139142
#[cfg(any(target_pointer_width = "32", windows))] }
140143
type_alias! { "ulong.md", c_ulong = u32, NonZero_c_ulong = NonZeroU32;
144+
#[doc(cfg(all()))]
141145
#[cfg(any(target_pointer_width = "32", windows))] }
142146
type_alias! { "long.md", c_long = i64, NonZero_c_long = NonZeroI64;
147+
#[doc(cfg(all()))]
143148
#[cfg(all(target_pointer_width = "64", not(windows)))] }
144149
type_alias! { "ulong.md", c_ulong = u64, NonZero_c_ulong = NonZeroU64;
150+
#[doc(cfg(all()))]
145151
#[cfg(all(target_pointer_width = "64", not(windows)))] }
146152
type_alias! { "longlong.md", c_longlong = i64, NonZero_c_longlong = NonZeroI64; }
147153
type_alias! { "ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZeroU64; }

library/std/src/os/windows/raw.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use crate::os::raw::c_void;
77
#[stable(feature = "raw_ext", since = "1.1.0")]
88
pub type HANDLE = *mut c_void;
99
#[cfg(target_pointer_width = "32")]
10+
#[doc(cfg(all()))]
1011
#[stable(feature = "raw_ext", since = "1.1.0")]
1112
pub type SOCKET = u32;
1213
#[cfg(target_pointer_width = "64")]
14+
#[doc(cfg(all()))]
1315
#[stable(feature = "raw_ext", since = "1.1.0")]
1416
pub type SOCKET = u64;

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,10 @@ fn merge_attrs(
318318
} else {
319319
Attributes::from_ast(&both, None)
320320
},
321-
both.cfg(cx.sess()),
321+
both.cfg(cx.tcx, &cx.cache.hidden_cfg),
322322
)
323323
} else {
324-
(old_attrs.clean(cx), old_attrs.cfg(cx.sess()))
324+
(old_attrs.clean(cx), old_attrs.cfg(cx.tcx, &cx.cache.hidden_cfg))
325325
}
326326
}
327327

0 commit comments

Comments
 (0)