Skip to content

Commit 32d4a68

Browse files
Implement RFC 3631
1 parent 86e05cd commit 32d4a68

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
@@ -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
@@ -164,8 +164,14 @@ passes_doc_alias_start_end =
164164
passes_doc_attr_not_crate_level =
165165
`#![doc({$attr_name} = "...")]` isn't allowed as a crate-level attribute
166166
167-
passes_doc_cfg_hide_takes_list =
168-
`#[doc(cfg_hide(...))]` takes a list of attributes
167+
passes_doc_auto_cfg_expects_hide_or_show =
168+
`only "hide" or "show" are allowed in "#[doc(auto_cfg(...))]"`
169+
170+
passes_doc_auto_cfg_hide_show_expects_list =
171+
`#![doc(auto_cfg({$attr_name}(...)))]` only expects a list of items
172+
173+
passes_doc_auto_cfg_wrong_literal =
174+
`expected boolean for #[doc(auto_cfg = ...)]`
169175
170176
passes_doc_expect_str =
171177
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
@@ -1271,16 +1271,43 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12711271
}
12721272
}
12731273

1274-
/// Check that the `#![doc(cfg_hide(...))]` attribute only contains a list of attributes.
1275-
///
1276-
fn check_doc_cfg_hide(&self, meta: &MetaItemInner, hir_id: HirId) {
1277-
if meta.meta_item_list().is_none() {
1278-
self.tcx.emit_node_span_lint(
1279-
INVALID_DOC_ATTRIBUTES,
1280-
hir_id,
1281-
meta.span(),
1282-
errors::DocCfgHideTakesList,
1283-
);
1274+
/// Check that the `#![doc(auto_cfg(..))]` attribute has expected input.
1275+
fn check_doc_auto_cfg(&self, meta: &MetaItemInner, hir_id: HirId) {
1276+
let MetaItemInner::MetaItem(meta) = meta else {
1277+
unreachable!();
1278+
};
1279+
match &meta.kind {
1280+
MetaItemKind::Word => {}
1281+
MetaItemKind::NameValue(lit) => {
1282+
if !matches!(lit.kind, LitKind::Bool(_)) {
1283+
self.tcx.emit_node_span_lint(
1284+
INVALID_DOC_ATTRIBUTES,
1285+
hir_id,
1286+
meta.span,
1287+
errors::DocAutoCfgWrongLiteral,
1288+
);
1289+
}
1290+
}
1291+
MetaItemKind::List(list) => {
1292+
for item in list {
1293+
let Some(attr_name) = item.name() else { continue };
1294+
if attr_name != sym::hide && attr_name != sym::show {
1295+
self.tcx.emit_node_span_lint(
1296+
INVALID_DOC_ATTRIBUTES,
1297+
hir_id,
1298+
meta.span,
1299+
errors::DocAutoCfgExpectsHideOrShow,
1300+
);
1301+
} else if item.meta_item_list().is_none() {
1302+
self.tcx.emit_node_span_lint(
1303+
INVALID_DOC_ATTRIBUTES,
1304+
hir_id,
1305+
meta.span,
1306+
errors::DocAutoCfgHideShowExpectsList { attr_name: attr_name.as_str() },
1307+
);
1308+
}
1309+
}
1310+
}
12841311
}
12851312
}
12861313

@@ -1342,10 +1369,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13421369
self.check_attr_crate_level(attr, style, meta, hir_id);
13431370
}
13441371

1345-
Some(sym::cfg_hide) => {
1346-
if self.check_attr_crate_level(attr, style, meta, hir_id) {
1347-
self.check_doc_cfg_hide(meta, hir_id);
1348-
}
1372+
Some(sym::auto_cfg) => {
1373+
self.check_doc_auto_cfg(meta, hir_id);
13491374
}
13501375

13511376
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,
@@ -1940,6 +1943,7 @@ symbols! {
19401943
shl_assign,
19411944
shorter_tail_lifetimes,
19421945
should_panic,
1946+
show,
19431947
shr,
19441948
shr_assign,
19451949
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;
@@ -400,6 +400,7 @@ pub(crate) fn merge_attrs(
400400
cx: &mut DocContext<'_>,
401401
old_attrs: &[hir::Attribute],
402402
new_attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
403+
cfg_info: &mut CfgInfo,
403404
) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
404405
// NOTE: If we have additional attributes (from a re-export),
405406
// always insert them first. This ensure that re-export
@@ -414,12 +415,12 @@ pub(crate) fn merge_attrs(
414415
} else {
415416
Attributes::from_hir(&both)
416417
},
417-
extract_cfg_from_attrs(both.iter(), cx.tcx, &cx.cache.hidden_cfg),
418+
extract_cfg_from_attrs(both.iter(), cx.tcx, cfg_info),
418419
)
419420
} else {
420421
(
421422
Attributes::from_hir(old_attrs),
422-
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg),
423+
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, cfg_info),
423424
)
424425
}
425426
}
@@ -595,7 +596,7 @@ pub(crate) fn build_impl(
595596
});
596597
}
597598

598-
let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs);
599+
let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs, &mut CfgInfo::default());
599600
trace!("merged_attrs={merged_attrs:?}");
600601

601602
trace!(

0 commit comments

Comments
 (0)