Skip to content

Commit 08c42cd

Browse files
Implement RFC 3631
1 parent 6c02dd4 commit 08c42cd

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
@@ -172,8 +172,14 @@ passes_doc_alias_start_end =
172172
passes_doc_attr_not_crate_level =
173173
`#![doc({$attr_name} = "...")]` isn't allowed as a crate-level attribute
174174
175-
passes_doc_cfg_hide_takes_list =
176-
`#[doc(cfg_hide(...))]` takes a list of attributes
175+
passes_doc_auto_cfg_expects_hide_or_show =
176+
`only "hide" or "show" are allowed in "#[doc(auto_cfg(...))]"`
177+
178+
passes_doc_auto_cfg_hide_show_expects_list =
179+
`#![doc(auto_cfg({$attr_name}(...)))]` only expects a list of items
180+
181+
passes_doc_auto_cfg_wrong_literal =
182+
`expected boolean for #[doc(auto_cfg = ...)]`
177183
178184
passes_doc_expect_str =
179185
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
@@ -1364,16 +1364,43 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13641364
}
13651365
}
13661366

1367-
/// Check that the `#![doc(cfg_hide(...))]` attribute only contains a list of attributes.
1368-
///
1369-
fn check_doc_cfg_hide(&self, meta: &MetaItemInner, hir_id: HirId) {
1370-
if meta.meta_item_list().is_none() {
1371-
self.tcx.emit_node_span_lint(
1372-
INVALID_DOC_ATTRIBUTES,
1373-
hir_id,
1374-
meta.span(),
1375-
errors::DocCfgHideTakesList,
1376-
);
1367+
/// Check that the `#![doc(auto_cfg(..))]` attribute has expected input.
1368+
fn check_doc_auto_cfg(&self, meta: &MetaItemInner, hir_id: HirId) {
1369+
let MetaItemInner::MetaItem(meta) = meta else {
1370+
unreachable!();
1371+
};
1372+
match &meta.kind {
1373+
MetaItemKind::Word => {}
1374+
MetaItemKind::NameValue(lit) => {
1375+
if !matches!(lit.kind, LitKind::Bool(_)) {
1376+
self.tcx.emit_node_span_lint(
1377+
INVALID_DOC_ATTRIBUTES,
1378+
hir_id,
1379+
meta.span,
1380+
errors::DocAutoCfgWrongLiteral,
1381+
);
1382+
}
1383+
}
1384+
MetaItemKind::List(list) => {
1385+
for item in list {
1386+
let Some(attr_name) = item.name() else { continue };
1387+
if attr_name != sym::hide && attr_name != sym::show {
1388+
self.tcx.emit_node_span_lint(
1389+
INVALID_DOC_ATTRIBUTES,
1390+
hir_id,
1391+
meta.span,
1392+
errors::DocAutoCfgExpectsHideOrShow,
1393+
);
1394+
} else if item.meta_item_list().is_none() {
1395+
self.tcx.emit_node_span_lint(
1396+
INVALID_DOC_ATTRIBUTES,
1397+
hir_id,
1398+
meta.span,
1399+
errors::DocAutoCfgHideShowExpectsList { attr_name: attr_name.as_str() },
1400+
);
1401+
}
1402+
}
1403+
}
13771404
}
13781405
}
13791406

@@ -1435,10 +1462,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
14351462
self.check_attr_crate_level(attr, style, meta, hir_id);
14361463
}
14371464

1438-
Some(sym::cfg_hide) => {
1439-
if self.check_attr_crate_level(attr, style, meta, hir_id) {
1440-
self.check_doc_cfg_hide(meta, hir_id);
1441-
}
1465+
Some(sym::auto_cfg) => {
1466+
self.check_doc_auto_cfg(meta, hir_id);
14421467
}
14431468

14441469
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,
@@ -1129,6 +1130,8 @@ symbols! {
11291130
hashset_iter_ty,
11301131
hexagon_target_feature,
11311132
hidden,
1133+
hidden_cfg,
1134+
hide,
11321135
hint,
11331136
homogeneous_aggregate,
11341137
host,
@@ -1942,6 +1945,7 @@ symbols! {
19421945
shl_assign,
19431946
shorter_tail_lifetimes,
19441947
should_panic,
1948+
show,
19451949
shr,
19461950
shr_assign,
19471951
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)