Skip to content

Commit c0ee51f

Browse files
committed
Auto merge of #138907 - GuillaumeGomez:rfc-3631, r=fmease,camelid,Manishearth,lolbinarycat
Implement RFC 3631: add rustdoc doc_cfg features Implementation of rust-lang/rfcs#3631. This implementation actually resulted in a lot of simplifications: * All `cfg` computation is now done in one place: `propagate_doc_cfg.rs`. Because (trait) `impl`s are not retrieved at the same time as the other items, we cannot perform this computation in the clean process, it needs to be after. * Because there is `cfg` inheritance, we can keep track of them in one place (in `propagate_doc_cfg.rs`), meaning we don't need to copy an item's attributes to its children anymore. Only exception: impl items. For them we clone only `cfg` attributes. * `propagate_doc_cfg.rs` is also now much simpler, much less need to keep track of parents, since everything we need is handled by the new `CfgInfo` type. * I also suspect that `Cfg::simplify_with` could either be removed or at least used directly into `propagate_doc_cfg.rs` when we compute `cfg`s. Considering how big the PR already is, I'll do it in a follow-up. I didn't remove the `doc_cfg*` features in this PR because some dependencies used in `rustc` (like `stdarch`) are using it, so we need to have a nightly released with this PR before I can switch to the new feature. r? ghost
2 parents ade8487 + 6cccea8 commit c0ee51f

Some content is hidden

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

43 files changed

+1046
-371
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
183183
gate_doc!(
184184
"experimental" {
185185
cfg => doc_cfg
186-
cfg_hide => doc_cfg_hide
186+
auto_cfg => doc_cfg
187187
masked => doc_masked
188188
notable_trait => doc_notable_trait
189189
}

compiler/rustc_feature/src/removed.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ declare_features! (
101101
Some("never properly implemented; requires significant design work"), 127655),
102102
/// Allows deriving traits as per `SmartPointer` specification
103103
(removed, derive_smart_pointer, "1.84.0", Some(123430), Some("replaced by `CoercePointee`"), 131284),
104+
/// Tells rustdoc to automatically generate `#[doc(cfg(...))]`.
105+
(removed, doc_auto_cfg, "1.58.0", Some(43781), Some("merged into `doc_cfg`"), 138907),
106+
/// Allows `#[doc(cfg_hide(...))]`.
107+
(removed, doc_cfg_hide, "1.57.0", Some(43781), Some("merged into `doc_cfg`"), 138907),
104108
/// Allows using `#[doc(keyword = "...")]`.
105109
(removed, doc_keyword, "1.58.0", Some(51315),
106110
Some("merged into `#![feature(rustdoc_internals)]`"), 90420),

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,8 @@ declare_features! (
472472
(incomplete, deref_patterns, "1.79.0", Some(87121)),
473473
/// Allows deriving the From trait on single-field structs.
474474
(unstable, derive_from, "1.91.0", Some(144889)),
475-
/// Tells rustdoc to automatically generate `#[doc(cfg(...))]`.
476-
(unstable, doc_auto_cfg, "1.58.0", Some(43781)),
477475
/// Allows `#[doc(cfg(...))]`.
478476
(unstable, doc_cfg, "1.21.0", Some(43781)),
479-
/// Allows `#[doc(cfg_hide(...))]`.
480-
(unstable, doc_cfg_hide, "1.57.0", Some(43781)),
481477
/// Allows `#[doc(masked)]`.
482478
(unstable, doc_masked, "1.21.0", Some(44027)),
483479
/// Allows features to allow target_feature to better interact with traits.

compiler/rustc_passes/messages.ftl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,17 @@ passes_doc_attribute_not_attribute =
140140
nonexistent builtin attribute `{$attribute}` used in `#[doc(attribute = "...")]`
141141
.help = only existing builtin attributes are allowed in core/std
142142
143-
passes_doc_cfg_hide_takes_list =
144-
`#[doc(cfg_hide(...))]` takes a list of attributes
143+
passes_doc_auto_cfg_expects_hide_or_show =
144+
only `hide` or `show` are allowed in `#[doc(auto_cfg(...))]`
145+
146+
passes_doc_auto_cfg_hide_show_expects_list =
147+
`#![doc(auto_cfg({$attr_name}(...)))]` expects a list of items
148+
149+
passes_doc_auto_cfg_hide_show_unexpected_item =
150+
`#![doc(auto_cfg({$attr_name}(...)))]` only accepts identifiers or key/value items
151+
152+
passes_doc_auto_cfg_wrong_literal =
153+
expected boolean for `#[doc(auto_cfg = ...)]`
145154
146155
passes_doc_expect_str =
147156
doc {$attr_name} attribute expects a string: #[doc({$attr_name} = "a")]

compiler/rustc_passes/src/check_attr.rs

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::collections::hash_map::Entry;
1010
use std::slice;
1111

1212
use rustc_abi::{Align, ExternAbi, Size};
13-
use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, ast};
13+
use rustc_ast::{AttrStyle, LitKind, MetaItem, MetaItemInner, MetaItemKind, ast};
1414
use rustc_attr_parsing::{AttributeParser, Late};
1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey};
@@ -1160,16 +1160,59 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11601160
}
11611161
}
11621162

1163-
/// Check that the `#![doc(cfg_hide(...))]` attribute only contains a list of attributes.
1164-
///
1165-
fn check_doc_cfg_hide(&self, meta: &MetaItemInner, hir_id: HirId) {
1166-
if meta.meta_item_list().is_none() {
1167-
self.tcx.emit_node_span_lint(
1168-
INVALID_DOC_ATTRIBUTES,
1169-
hir_id,
1170-
meta.span(),
1171-
errors::DocCfgHideTakesList,
1172-
);
1163+
/// Check that the `#![doc(auto_cfg)]` attribute has the expected input.
1164+
fn check_doc_auto_cfg(&self, meta: &MetaItem, hir_id: HirId) {
1165+
match &meta.kind {
1166+
MetaItemKind::Word => {}
1167+
MetaItemKind::NameValue(lit) => {
1168+
if !matches!(lit.kind, LitKind::Bool(_)) {
1169+
self.tcx.emit_node_span_lint(
1170+
INVALID_DOC_ATTRIBUTES,
1171+
hir_id,
1172+
meta.span,
1173+
errors::DocAutoCfgWrongLiteral,
1174+
);
1175+
}
1176+
}
1177+
MetaItemKind::List(list) => {
1178+
for item in list {
1179+
let Some(attr_name @ (sym::hide | sym::show)) = item.name() else {
1180+
self.tcx.emit_node_span_lint(
1181+
INVALID_DOC_ATTRIBUTES,
1182+
hir_id,
1183+
meta.span,
1184+
errors::DocAutoCfgExpectsHideOrShow,
1185+
);
1186+
continue;
1187+
};
1188+
if let Some(list) = item.meta_item_list() {
1189+
for item in list {
1190+
let valid = item.meta_item().is_some_and(|meta| {
1191+
meta.path.segments.len() == 1
1192+
&& matches!(
1193+
&meta.kind,
1194+
MetaItemKind::Word | MetaItemKind::NameValue(_)
1195+
)
1196+
});
1197+
if !valid {
1198+
self.tcx.emit_node_span_lint(
1199+
INVALID_DOC_ATTRIBUTES,
1200+
hir_id,
1201+
item.span(),
1202+
errors::DocAutoCfgHideShowUnexpectedItem { attr_name },
1203+
);
1204+
}
1205+
}
1206+
} else {
1207+
self.tcx.emit_node_span_lint(
1208+
INVALID_DOC_ATTRIBUTES,
1209+
hir_id,
1210+
meta.span,
1211+
errors::DocAutoCfgHideShowExpectsList { attr_name },
1212+
);
1213+
}
1214+
}
1215+
}
11731216
}
11741217
}
11751218

@@ -1245,10 +1288,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12451288
self.check_attr_crate_level(attr, style, meta, hir_id);
12461289
}
12471290

1248-
Some(sym::cfg_hide) => {
1249-
if self.check_attr_crate_level(attr, style, meta, hir_id) {
1250-
self.check_doc_cfg_hide(meta, hir_id);
1251-
}
1291+
Some(sym::auto_cfg) => {
1292+
self.check_doc_auto_cfg(i_meta, hir_id);
12521293
}
12531294

12541295
Some(sym::inline | sym::no_inline) => {

compiler/rustc_passes/src/errors.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,24 @@ pub(crate) struct DocTestLiteral;
309309
pub(crate) struct DocTestTakesList;
310310

311311
#[derive(LintDiagnostic)]
312-
#[diag(passes_doc_cfg_hide_takes_list)]
313-
pub(crate) struct DocCfgHideTakesList;
312+
#[diag(passes_doc_auto_cfg_wrong_literal)]
313+
pub(crate) struct DocAutoCfgWrongLiteral;
314+
315+
#[derive(LintDiagnostic)]
316+
#[diag(passes_doc_auto_cfg_expects_hide_or_show)]
317+
pub(crate) struct DocAutoCfgExpectsHideOrShow;
318+
319+
#[derive(LintDiagnostic)]
320+
#[diag(passes_doc_auto_cfg_hide_show_expects_list)]
321+
pub(crate) struct DocAutoCfgHideShowExpectsList {
322+
pub attr_name: Symbol,
323+
}
324+
325+
#[derive(LintDiagnostic)]
326+
#[diag(passes_doc_auto_cfg_hide_show_unexpected_item)]
327+
pub(crate) struct DocAutoCfgHideShowUnexpectedItem {
328+
pub attr_name: Symbol,
329+
}
314330

315331
#[derive(LintDiagnostic)]
316332
#[diag(passes_doc_test_unknown_any)]

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ symbols! {
545545
attributes,
546546
audit_that,
547547
augmented_assignments,
548+
auto_cfg,
548549
auto_traits,
549550
autodiff,
550551
autodiff_forward,
@@ -628,7 +629,6 @@ symbols! {
628629
cfg_emscripten_wasm_eh,
629630
cfg_eval,
630631
cfg_fmt_debug,
631-
cfg_hide,
632632
cfg_overflow_checks,
633633
cfg_panic,
634634
cfg_relocation_model,
@@ -1149,6 +1149,7 @@ symbols! {
11491149
hashset_iter_ty,
11501150
hexagon_target_feature,
11511151
hidden,
1152+
hide,
11521153
hint,
11531154
homogeneous_aggregate,
11541155
host,
@@ -1987,6 +1988,7 @@ symbols! {
19871988
shl_assign,
19881989
shorter_tail_lifetimes,
19891990
should_panic,
1991+
show,
19901992
shr,
19911993
shr_assign,
19921994
sig_dfl,

library/alloc/src/lib.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,7 @@
6464
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
6565
test(no_crate_inject, attr(allow(unused_variables), deny(warnings)))
6666
)]
67-
#![doc(cfg_hide(
68-
not(test),
69-
no_global_oom_handling,
70-
not(no_global_oom_handling),
71-
not(no_rc),
72-
not(no_sync),
73-
target_has_atomic = "ptr"
74-
))]
67+
#![doc(auto_cfg(hide(no_global_oom_handling, no_rc, no_sync, target_has_atomic = "ptr")))]
7568
#![doc(rust_logo)]
7669
#![feature(rustdoc_internals)]
7770
#![no_std]
@@ -195,7 +188,6 @@
195188
//
196189
// Rustdoc features:
197190
#![feature(doc_cfg)]
198-
#![feature(doc_cfg_hide)]
199191
// Technically, this is a bug in rustdoc: rustdoc sees the documentation on `#[lang = slice_alloc]`
200192
// blocks is for `&[T]`, which also has documentation using this feature in `core`, and gets mad
201193
// 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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
5252
)]
5353
#![doc(rust_logo)]
54-
#![doc(cfg_hide(
54+
#![doc(auto_cfg(hide(
5555
no_fp_fmt_parse,
5656
target_pointer_width = "16",
5757
target_pointer_width = "32",
@@ -71,7 +71,7 @@
7171
target_has_atomic_load_store = "32",
7272
target_has_atomic_load_store = "64",
7373
target_has_atomic_load_store = "ptr",
74-
))]
74+
)))]
7575
#![no_core]
7676
#![rustc_coherence_is_core]
7777
#![rustc_preserve_ub_checks]
@@ -149,7 +149,6 @@
149149
#![feature(deprecated_suggestion)]
150150
#![feature(derive_const)]
151151
#![feature(doc_cfg)]
152-
#![feature(doc_cfg_hide)]
153152
#![feature(doc_notable_trait)]
154153
#![feature(extern_types)]
155154
#![feature(f16)]

library/std/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@
235235
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
236236
)]
237237
#![doc(rust_logo)]
238-
#![doc(cfg_hide(not(test), no_global_oom_handling, not(no_global_oom_handling)))]
238+
#![doc(auto_cfg(hide(no_global_oom_handling)))]
239239
// Don't link to std. We are std.
240240
#![no_std]
241241
// Tell the compiler to link to either panic_abort or panic_unwind
@@ -285,7 +285,6 @@
285285
#![feature(decl_macro)]
286286
#![feature(deprecated_suggestion)]
287287
#![feature(doc_cfg)]
288-
#![feature(doc_cfg_hide)]
289288
#![feature(doc_masked)]
290289
#![feature(doc_notable_trait)]
291290
#![feature(dropck_eyepatch)]

0 commit comments

Comments
 (0)