Skip to content

Commit 9e4d496

Browse files
Auto merge of #138907 - GuillaumeGomez:rfc-3631, r=<try>
Implement RFC 3631: add rustdoc doc_cfg features
2 parents 40ace17 + 29c2c58 commit 9e4d496

40 files changed

+1059
-363
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ 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+
/// Allows `#[doc(cfg_hide(...))]`.
105+
(removed, doc_cfg_hide, "1.57.0", Some(43781), Some("merged into `doc_cfg`"), 138907),
104106
/// Allows using `#[doc(keyword = "...")]`.
105107
(removed, doc_keyword, "1.58.0", Some(51315),
106108
Some("merged into `#![feature(rustdoc_internals)]`"), 90420),

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,6 @@ declare_features! (
476476
(unstable, doc_auto_cfg, "1.58.0", Some(43781)),
477477
/// Allows `#[doc(cfg(...))]`.
478478
(unstable, doc_cfg, "1.21.0", Some(43781)),
479-
/// Allows `#[doc(cfg_hide(...))]`.
480-
(unstable, doc_cfg_hide, "1.57.0", Some(43781)),
481479
/// Allows `#[doc(masked)]`.
482480
(unstable, doc_masked, "1.21.0", Some(44027)),
483481
/// Allows features to allow target_feature to better interact with traits.

compiler/rustc_passes/messages.ftl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ passes_doc_attribute_not_attribute =
152152
passes_doc_cfg_hide_takes_list =
153153
`#[doc(cfg_hide(...))]` takes a list of attributes
154154
155+
passes_doc_auto_cfg_expects_hide_or_show =
156+
only `hide` or `show` are allowed in `#[doc(auto_cfg(...))]`
157+
158+
passes_doc_auto_cfg_hide_show_expects_list =
159+
`#![doc(auto_cfg({$attr_name}(...)))]` expects a list of items
160+
161+
passes_doc_auto_cfg_hide_show_unexpected_item =
162+
`#![doc(auto_cfg({$attr_name}(...)))]` only accepts identifiers or key/value items
163+
164+
passes_doc_auto_cfg_wrong_literal =
165+
expected boolean for `#[doc(auto_cfg = ...)]`
166+
155167
passes_doc_expect_str =
156168
doc {$attr_name} attribute expects a string: #[doc({$attr_name} = "a")]
157169

compiler/rustc_passes/src/check_attr.rs

Lines changed: 71 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,74 @@ 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 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) = 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 attr_name != sym::hide && attr_name != sym::show {
1189+
self.tcx.emit_node_span_lint(
1190+
INVALID_DOC_ATTRIBUTES,
1191+
hir_id,
1192+
meta.span,
1193+
errors::DocAutoCfgExpectsHideOrShow,
1194+
);
1195+
} else if let Some(list) = item.meta_item_list() {
1196+
for item in list {
1197+
if item.meta_item_list().is_some() {
1198+
self.tcx.emit_node_span_lint(
1199+
INVALID_DOC_ATTRIBUTES,
1200+
hir_id,
1201+
item.span(),
1202+
errors::DocAutoCfgHideShowUnexpectedItem {
1203+
attr_name: attr_name.as_str(),
1204+
},
1205+
);
1206+
} else if match item {
1207+
MetaItemInner::Lit(_) => true,
1208+
// We already checked above that it's not a list.
1209+
MetaItemInner::MetaItem(meta) => meta.path.segments.len() != 1,
1210+
} {
1211+
self.tcx.emit_node_span_lint(
1212+
INVALID_DOC_ATTRIBUTES,
1213+
hir_id,
1214+
item.span(),
1215+
errors::DocAutoCfgHideShowUnexpectedItem {
1216+
attr_name: attr_name.as_str(),
1217+
},
1218+
);
1219+
}
1220+
}
1221+
} else {
1222+
self.tcx.emit_node_span_lint(
1223+
INVALID_DOC_ATTRIBUTES,
1224+
hir_id,
1225+
meta.span,
1226+
errors::DocAutoCfgHideShowExpectsList { attr_name: attr_name.as_str() },
1227+
);
1228+
}
1229+
}
1230+
}
11731231
}
11741232
}
11751233

@@ -1245,10 +1303,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12451303
self.check_attr_crate_level(attr, style, meta, hir_id);
12461304
}
12471305

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-
}
1306+
Some(sym::auto_cfg) => {
1307+
self.check_doc_auto_cfg(i_meta, hir_id);
12521308
}
12531309

12541310
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<'a> {
322+
pub attr_name: &'a str,
323+
}
324+
325+
#[derive(LintDiagnostic)]
326+
#[diag(passes_doc_auto_cfg_hide_show_unexpected_item)]
327+
pub(crate) struct DocAutoCfgHideShowUnexpectedItem<'a> {
328+
pub attr_name: &'a str,
329+
}
314330

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

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 0 deletions
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,
@@ -1149,6 +1150,7 @@ symbols! {
11491150
hashset_iter_ty,
11501151
hexagon_target_feature,
11511152
hidden,
1153+
hide,
11521154
hint,
11531155
homogeneous_aggregate,
11541156
host,
@@ -1987,6 +1989,7 @@ symbols! {
19871989
shl_assign,
19881990
shorter_tail_lifetimes,
19891991
should_panic,
1992+
show,
19901993
shr,
19911994
shr_assign,
19921995
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)