Skip to content

Commit 4261072

Browse files
Implement RFC 3631
1 parent 855e0fe commit 4261072

File tree

16 files changed

+518
-211
lines changed

16 files changed

+518
-211
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

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

183183
gate_doc!(
184184
"experimental" {
185-
cfg => doc_cfg
186-
cfg_hide => doc_cfg_hide
187185
masked => doc_masked
188186
notable_trait => doc_notable_trait
189187
}

compiler/rustc_passes/messages.ftl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,14 @@ passes_doc_alias_start_end =
168168
passes_doc_attr_not_crate_level =
169169
`#![doc({$attr_name} = "...")]` isn't allowed as a crate-level attribute
170170
171-
passes_doc_cfg_hide_takes_list =
172-
`#[doc(cfg_hide(...))]` takes a list of attributes
171+
passes_doc_auto_cfg_expects_hide_or_show =
172+
`only "hide" or "show" are allowed in "#[doc(auto_cfg(...))]"`
173+
174+
passes_doc_auto_cfg_hide_show_expects_list =
175+
`#![doc(auto_cfg({$attr_name}(...)))]` only expects a list of items
176+
177+
passes_doc_auto_cfg_wrong_literal =
178+
`expected boolean for #[doc(auto_cfg = ...)]`
173179
174180
passes_doc_expect_str =
175181
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
@@ -1309,16 +1309,43 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13091309
}
13101310
}
13111311

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

@@ -1380,10 +1407,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13801407
self.check_attr_crate_level(attr, style, meta, hir_id);
13811408
}
13821409

1383-
Some(sym::cfg_hide) => {
1384-
if self.check_attr_crate_level(attr, style, meta, hir_id) {
1385-
self.check_doc_cfg_hide(meta, hir_id);
1386-
}
1410+
Some(sym::auto_cfg) => {
1411+
self.check_doc_auto_cfg(meta, hir_id);
13871412
}
13881413

13891414
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
@@ -362,8 +362,18 @@ pub(crate) struct DocTestLiteral;
362362
pub(crate) struct DocTestTakesList;
363363

364364
#[derive(LintDiagnostic)]
365-
#[diag(passes_doc_cfg_hide_takes_list)]
366-
pub(crate) struct DocCfgHideTakesList;
365+
#[diag(passes_doc_auto_cfg_wrong_literal)]
366+
pub(crate) struct DocAutoCfgWrongLiteral;
367+
368+
#[derive(LintDiagnostic)]
369+
#[diag(passes_doc_auto_cfg_expects_hide_or_show)]
370+
pub(crate) struct DocAutoCfgExpectsHideOrShow;
371+
372+
#[derive(LintDiagnostic)]
373+
#[diag(passes_doc_auto_cfg_hide_show_expects_list)]
374+
pub(crate) struct DocAutoCfgHideShowExpectsList<'a> {
375+
pub attr_name: &'a str,
376+
}
367377

368378
#[derive(LintDiagnostic)]
369379
#[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
@@ -539,6 +539,7 @@ symbols! {
539539
attributes,
540540
audit_that,
541541
augmented_assignments,
542+
auto_cfg,
542543
auto_traits,
543544
autodiff_forward,
544545
autodiff_reverse,
@@ -1126,6 +1127,8 @@ symbols! {
11261127
hashset_iter_ty,
11271128
hexagon_target_feature,
11281129
hidden,
1130+
hidden_cfg,
1131+
hide,
11291132
hint,
11301133
homogeneous_aggregate,
11311134
host,
@@ -1938,6 +1941,7 @@ symbols! {
19381941
shl_assign,
19391942
shorter_tail_lifetimes,
19401943
should_panic,
1944+
show,
19411945
shr,
19421946
shr_assign,
19431947
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
@@ -256,6 +256,36 @@ impl Cfg {
256256
fn omit_preposition(&self) -> bool {
257257
matches!(self, Cfg::True | Cfg::False)
258258
}
259+
260+
pub(crate) fn strip_hidden(&self, hidden: &FxHashSet<Cfg>) -> Option<Self> {
261+
match self {
262+
Self::True | Self::False => Some(self.clone()),
263+
Self::Cfg(..) => {
264+
if !hidden.contains(self) {
265+
Some(self.clone())
266+
} else {
267+
None
268+
}
269+
}
270+
Self::Not(cfg) => {
271+
if let Some(cfg) = cfg.strip_hidden(hidden) {
272+
Some(Self::Not(Box::new(cfg)))
273+
} else {
274+
None
275+
}
276+
}
277+
Self::Any(cfgs) => {
278+
let cfgs =
279+
cfgs.iter().filter_map(|cfg| cfg.strip_hidden(hidden)).collect::<Vec<_>>();
280+
if cfgs.is_empty() { None } else { Some(Self::Any(cfgs)) }
281+
}
282+
Self::All(cfgs) => {
283+
let cfgs =
284+
cfgs.iter().filter_map(|cfg| cfg.strip_hidden(hidden)).collect::<Vec<_>>();
285+
if cfgs.is_empty() { None } else { Some(Self::All(cfgs)) }
286+
}
287+
}
288+
}
259289
}
260290

261291
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;
@@ -406,6 +406,7 @@ pub(crate) fn merge_attrs(
406406
cx: &mut DocContext<'_>,
407407
old_attrs: &[hir::Attribute],
408408
new_attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
409+
cfg_info: &mut CfgInfo,
409410
) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
410411
// NOTE: If we have additional attributes (from a re-export),
411412
// always insert them first. This ensure that re-export
@@ -420,12 +421,12 @@ pub(crate) fn merge_attrs(
420421
} else {
421422
Attributes::from_hir(&both)
422423
},
423-
extract_cfg_from_attrs(both.iter(), cx.tcx, &cx.cache.hidden_cfg),
424+
extract_cfg_from_attrs(both.iter(), cx.tcx, cfg_info),
424425
)
425426
} else {
426427
(
427428
Attributes::from_hir(old_attrs),
428-
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg),
429+
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, cfg_info),
429430
)
430431
}
431432
}
@@ -601,7 +602,7 @@ pub(crate) fn build_impl(
601602
});
602603
}
603604

604-
let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs);
605+
let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs, &mut CfgInfo::default());
605606
trace!("merged_attrs={merged_attrs:?}");
606607

607608
trace!(

0 commit comments

Comments
 (0)