Skip to content

Commit 50db903

Browse files
Implement RFC 3631
1 parent 40daf23 commit 50db903

File tree

16 files changed

+519
-217
lines changed

16 files changed

+519
-217
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
172172

173173
gate_doc!(
174174
"experimental" {
175-
cfg => doc_cfg
176-
cfg_hide => doc_cfg_hide
177175
masked => doc_masked
178176
notable_trait => doc_notable_trait
179177
}

compiler/rustc_passes/messages.ftl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,14 @@ passes_doc_alias_start_end =
182182
passes_doc_attr_not_crate_level =
183183
`#![doc({$attr_name} = "...")]` isn't allowed as a crate-level attribute
184184
185-
passes_doc_cfg_hide_takes_list =
186-
`#[doc(cfg_hide(...))]` takes a list of attributes
185+
passes_doc_auto_cfg_expects_hide_or_show =
186+
`only "hide" or "show" are allowed in "#[doc(auto_cfg(...))]"`
187+
188+
passes_doc_auto_cfg_hide_show_expects_list =
189+
`#![doc(auto_cfg({$attr_name}(...)))]` only expects a list of items
190+
191+
passes_doc_auto_cfg_wrong_literal =
192+
`expected boolean for #[doc(auto_cfg = ...)]`
187193
188194
passes_doc_expect_str =
189195
doc {$attr_name} attribute expects a string: #[doc({$attr_name} = "a")]

compiler/rustc_passes/src/check_attr.rs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,16 +1307,43 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13071307
}
13081308
}
13091309

1310-
/// Check that the `#![doc(cfg_hide(...))]` attribute only contains a list of attributes.
1311-
///
1312-
fn check_doc_cfg_hide(&self, meta: &MetaItemInner, hir_id: HirId) {
1313-
if meta.meta_item_list().is_none() {
1314-
self.tcx.emit_node_span_lint(
1315-
INVALID_DOC_ATTRIBUTES,
1316-
hir_id,
1317-
meta.span(),
1318-
errors::DocCfgHideTakesList,
1319-
);
1310+
/// Check that the `#![doc(auto_cfg(..))]` attribute has expected input.
1311+
fn check_doc_auto_cfg(&self, meta: &MetaItemInner, hir_id: HirId) {
1312+
let MetaItemInner::MetaItem(meta) = meta else {
1313+
unreachable!();
1314+
};
1315+
match &meta.kind {
1316+
MetaItemKind::Word => {}
1317+
MetaItemKind::NameValue(lit) => {
1318+
if !matches!(lit.kind, LitKind::Bool(_)) {
1319+
self.tcx.emit_node_span_lint(
1320+
INVALID_DOC_ATTRIBUTES,
1321+
hir_id,
1322+
meta.span,
1323+
errors::DocAutoCfgWrongLiteral,
1324+
);
1325+
}
1326+
}
1327+
MetaItemKind::List(list) => {
1328+
for item in list {
1329+
let Some(attr_name) = item.name() else { continue };
1330+
if attr_name != sym::hide && attr_name != sym::show {
1331+
self.tcx.emit_node_span_lint(
1332+
INVALID_DOC_ATTRIBUTES,
1333+
hir_id,
1334+
meta.span,
1335+
errors::DocAutoCfgExpectsHideOrShow,
1336+
);
1337+
} else if item.meta_item_list().is_none() {
1338+
self.tcx.emit_node_span_lint(
1339+
INVALID_DOC_ATTRIBUTES,
1340+
hir_id,
1341+
meta.span,
1342+
errors::DocAutoCfgHideShowExpectsList { attr_name: attr_name.as_str() },
1343+
);
1344+
}
1345+
}
1346+
}
13201347
}
13211348
}
13221349

@@ -1377,10 +1404,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13771404
self.check_attr_crate_level(attr, meta, hir_id);
13781405
}
13791406

1380-
Some(sym::cfg_hide) => {
1381-
if self.check_attr_crate_level(attr, meta, hir_id) {
1382-
self.check_doc_cfg_hide(meta, hir_id);
1383-
}
1407+
Some(sym::auto_cfg) => {
1408+
self.check_doc_auto_cfg(meta, hir_id);
13841409
}
13851410

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

compiler/rustc_passes/src/errors.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,18 @@ pub(crate) struct DocTestLiteral;
333333
pub(crate) struct DocTestTakesList;
334334

335335
#[derive(LintDiagnostic)]
336-
#[diag(passes_doc_cfg_hide_takes_list)]
337-
pub(crate) struct DocCfgHideTakesList;
336+
#[diag(passes_doc_auto_cfg_wrong_literal)]
337+
pub(crate) struct DocAutoCfgWrongLiteral;
338+
339+
#[derive(LintDiagnostic)]
340+
#[diag(passes_doc_auto_cfg_expects_hide_or_show)]
341+
pub(crate) struct DocAutoCfgExpectsHideOrShow;
342+
343+
#[derive(LintDiagnostic)]
344+
#[diag(passes_doc_auto_cfg_hide_show_expects_list)]
345+
pub(crate) struct DocAutoCfgHideShowExpectsList<'a> {
346+
pub attr_name: &'a str,
347+
}
338348

339349
#[derive(LintDiagnostic)]
340350
#[diag(passes_doc_test_unknown_any)]

compiler/rustc_span/src/symbol.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ symbols! {
540540
attributes,
541541
audit_that,
542542
augmented_assignments,
543+
auto_cfg,
543544
auto_traits,
544545
autodiff_forward,
545546
autodiff_reverse,
@@ -1124,6 +1125,8 @@ symbols! {
11241125
hashset_iter_ty,
11251126
hexagon_target_feature,
11261127
hidden,
1128+
hidden_cfg,
1129+
hide,
11271130
hint,
11281131
homogeneous_aggregate,
11291132
host,
@@ -1928,6 +1931,7 @@ symbols! {
19281931
shl_assign,
19291932
shorter_tail_lifetimes,
19301933
should_panic,
1934+
show,
19311935
shr,
19321936
shr_assign,
19331937
sig_dfl,

library/alloc/src/lib.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,27 @@
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+
#![cfg_attr(
68+
bootstrap,
69+
doc(cfg_hide(
70+
not(test),
71+
no_global_oom_handling,
72+
not(no_global_oom_handling),
73+
not(no_rc),
74+
not(no_sync),
75+
target_has_atomic = "ptr"
76+
))
77+
)]
78+
#![cfg_attr(
79+
not(bootstrap),
80+
doc(auto_cfg(hide(
81+
test,
82+
no_global_oom_handling,
83+
no_rc,
84+
no_sync,
85+
target_has_atomic = "ptr"
86+
)))
87+
)]
7588
#![doc(rust_logo)]
7689
#![feature(rustdoc_internals)]
7790
#![no_std]

library/core/src/lib.rs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,54 @@
5151
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
5252
)]
5353
#![doc(rust_logo)]
54-
#![doc(cfg_hide(
55-
no_fp_fmt_parse,
56-
target_pointer_width = "16",
57-
target_pointer_width = "32",
58-
target_pointer_width = "64",
59-
target_has_atomic = "8",
60-
target_has_atomic = "16",
61-
target_has_atomic = "32",
62-
target_has_atomic = "64",
63-
target_has_atomic = "ptr",
64-
target_has_atomic_equal_alignment = "8",
65-
target_has_atomic_equal_alignment = "16",
66-
target_has_atomic_equal_alignment = "32",
67-
target_has_atomic_equal_alignment = "64",
68-
target_has_atomic_equal_alignment = "ptr",
69-
target_has_atomic_load_store = "8",
70-
target_has_atomic_load_store = "16",
71-
target_has_atomic_load_store = "32",
72-
target_has_atomic_load_store = "64",
73-
target_has_atomic_load_store = "ptr",
74-
))]
54+
#![cfg_attr(
55+
bootstrap,
56+
doc(cfg_hide(
57+
no_fp_fmt_parse,
58+
target_pointer_width = "16",
59+
target_pointer_width = "32",
60+
target_pointer_width = "64",
61+
target_has_atomic = "8",
62+
target_has_atomic = "16",
63+
target_has_atomic = "32",
64+
target_has_atomic = "64",
65+
target_has_atomic = "ptr",
66+
target_has_atomic_equal_alignment = "8",
67+
target_has_atomic_equal_alignment = "16",
68+
target_has_atomic_equal_alignment = "32",
69+
target_has_atomic_equal_alignment = "64",
70+
target_has_atomic_equal_alignment = "ptr",
71+
target_has_atomic_load_store = "8",
72+
target_has_atomic_load_store = "16",
73+
target_has_atomic_load_store = "32",
74+
target_has_atomic_load_store = "64",
75+
target_has_atomic_load_store = "ptr",
76+
))
77+
)]
78+
#![cfg_attr(
79+
not(bootstrap),
80+
doc(auto_cfg(hide(
81+
no_fp_fmt_parse,
82+
target_pointer_width = "16",
83+
target_pointer_width = "32",
84+
target_pointer_width = "64",
85+
target_has_atomic = "8",
86+
target_has_atomic = "16",
87+
target_has_atomic = "32",
88+
target_has_atomic = "64",
89+
target_has_atomic = "ptr",
90+
target_has_atomic_equal_alignment = "8",
91+
target_has_atomic_equal_alignment = "16",
92+
target_has_atomic_equal_alignment = "32",
93+
target_has_atomic_equal_alignment = "64",
94+
target_has_atomic_equal_alignment = "ptr",
95+
target_has_atomic_load_store = "8",
96+
target_has_atomic_load_store = "16",
97+
target_has_atomic_load_store = "32",
98+
target_has_atomic_load_store = "64",
99+
target_has_atomic_load_store = "ptr",
100+
)))
101+
)]
75102
#![no_core]
76103
#![rustc_coherence_is_core]
77104
#![rustc_preserve_ub_checks]

library/std/src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,21 @@
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+
#![cfg_attr(
239+
bootstrap,
240+
doc(cfg_hide(
241+
not(test),
242+
no_global_oom_handling,
243+
not(no_global_oom_handling)
244+
))
245+
)]
246+
#![cfg_attr(
247+
not(bootstrap),
248+
doc(auto_cfg(hide(
249+
test,
250+
no_global_oom_handling,
251+
)))
252+
)]
239253
// Don't link to std. We are std.
240254
#![no_std]
241255
// Tell the compiler to link to either panic_abort or panic_unwind

src/librustdoc/clean/cfg.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,36 @@ impl Cfg {
253253
fn omit_preposition(&self) -> bool {
254254
matches!(self, Cfg::True | Cfg::False)
255255
}
256+
257+
pub(crate) fn strip_hidden(&self, hidden: &FxHashSet<Cfg>) -> Option<Self> {
258+
match self {
259+
Self::True | Self::False => Some(self.clone()),
260+
Self::Cfg(..) => {
261+
if !hidden.contains(self) {
262+
Some(self.clone())
263+
} else {
264+
None
265+
}
266+
}
267+
Self::Not(cfg) => {
268+
if let Some(cfg) = cfg.strip_hidden(hidden) {
269+
Some(Self::Not(Box::new(cfg)))
270+
} else {
271+
None
272+
}
273+
}
274+
Self::Any(cfgs) => {
275+
let cfgs =
276+
cfgs.iter().filter_map(|cfg| cfg.strip_hidden(hidden)).collect::<Vec<_>>();
277+
if cfgs.is_empty() { None } else { Some(Self::Any(cfgs)) }
278+
}
279+
Self::All(cfgs) => {
280+
let cfgs =
281+
cfgs.iter().filter_map(|cfg| cfg.strip_hidden(hidden)).collect::<Vec<_>>();
282+
if cfgs.is_empty() { None } else { Some(Self::All(cfgs)) }
283+
}
284+
}
285+
}
256286
}
257287

258288
impl ops::Not for Cfg {

src/librustdoc/clean/inline.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use tracing::{debug, trace};
1919

2020
use super::{Item, extract_cfg_from_attrs};
2121
use crate::clean::{
22-
self, Attributes, ImplKind, ItemId, Type, clean_bound_vars, clean_generics, clean_impl_item,
23-
clean_middle_assoc_item, clean_middle_field, clean_middle_ty, clean_poly_fn_sig,
24-
clean_trait_ref_with_constraints, clean_ty, clean_ty_alias_inner_type, clean_ty_generics,
25-
clean_variant_def, utils,
22+
self, Attributes, CfgInfo, ImplKind, ItemId, Type, clean_bound_vars, clean_generics,
23+
clean_impl_item, clean_middle_assoc_item, clean_middle_field, clean_middle_ty,
24+
clean_poly_fn_sig, clean_trait_ref_with_constraints, clean_ty, clean_ty_alias_inner_type,
25+
clean_ty_generics, clean_variant_def, utils,
2626
};
2727
use crate::core::DocContext;
2828
use crate::formats::item_type::ItemType;
@@ -395,6 +395,7 @@ pub(crate) fn merge_attrs(
395395
cx: &mut DocContext<'_>,
396396
old_attrs: &[hir::Attribute],
397397
new_attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
398+
cfg_info: &mut CfgInfo,
398399
) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
399400
// NOTE: If we have additional attributes (from a re-export),
400401
// always insert them first. This ensure that re-export
@@ -409,12 +410,12 @@ pub(crate) fn merge_attrs(
409410
} else {
410411
Attributes::from_hir(&both)
411412
},
412-
extract_cfg_from_attrs(both.iter(), cx.tcx, &cx.cache.hidden_cfg),
413+
extract_cfg_from_attrs(both.iter(), cx.tcx, cfg_info),
413414
)
414415
} else {
415416
(
416417
Attributes::from_hir(old_attrs),
417-
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg),
418+
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, cfg_info),
418419
)
419420
}
420421
}
@@ -593,7 +594,7 @@ pub(crate) fn build_impl(
593594
});
594595
}
595596

596-
let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs);
597+
let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs, &mut CfgInfo::default());
597598
trace!("merged_attrs={merged_attrs:?}");
598599

599600
trace!(

0 commit comments

Comments
 (0)