Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f36357f

Browse files
Implement RFC 3631
1 parent 19f42cb commit f36357f

File tree

14 files changed

+399
-207
lines changed

14 files changed

+399
-207
lines changed

‎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
@@ -1259,16 +1259,43 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12591259
}
12601260
}
12611261

1262-
/// Check that the `#![doc(cfg_hide(...))]` attribute only contains a list of attributes.
1263-
///
1264-
fn check_doc_cfg_hide(&self, meta: &MetaItemInner, hir_id: HirId) {
1265-
if meta.meta_item_list().is_none() {
1266-
self.tcx.emit_node_span_lint(
1267-
INVALID_DOC_ATTRIBUTES,
1268-
hir_id,
1269-
meta.span(),
1270-
errors::DocCfgHideTakesList,
1271-
);
1262+
/// Check that the `#![doc(auto_cfg(..))]` attribute has expected input.
1263+
fn check_doc_auto_cfg(&self, meta: &MetaItemInner, hir_id: HirId) {
1264+
let MetaItemInner::MetaItem(meta) = meta else {
1265+
unreachable!();
1266+
};
1267+
match &meta.kind {
1268+
MetaItemKind::Word => {}
1269+
MetaItemKind::NameValue(lit) => {
1270+
if !matches!(lit.kind, LitKind::Bool(_)) {
1271+
self.tcx.emit_node_span_lint(
1272+
INVALID_DOC_ATTRIBUTES,
1273+
hir_id,
1274+
meta.span,
1275+
errors::DocAutoCfgWrongLiteral,
1276+
);
1277+
}
1278+
}
1279+
MetaItemKind::List(list) => {
1280+
for item in list {
1281+
let attr_name = item.name_or_empty();
1282+
if attr_name != sym::hide && attr_name != sym::show {
1283+
self.tcx.emit_node_span_lint(
1284+
INVALID_DOC_ATTRIBUTES,
1285+
hir_id,
1286+
meta.span,
1287+
errors::DocAutoCfgExpectsHideOrShow,
1288+
);
1289+
} else if item.meta_item_list().is_none() {
1290+
self.tcx.emit_node_span_lint(
1291+
INVALID_DOC_ATTRIBUTES,
1292+
hir_id,
1293+
meta.span,
1294+
errors::DocAutoCfgHideShowExpectsList { attr_name: attr_name.as_str() },
1295+
);
1296+
}
1297+
}
1298+
}
12721299
}
12731300
}
12741301

@@ -1329,10 +1356,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
13291356
self.check_attr_crate_level(attr, meta, hir_id);
13301357
}
13311358

1332-
sym::cfg_hide => {
1333-
if self.check_attr_crate_level(attr, meta, hir_id) {
1334-
self.check_doc_cfg_hide(meta, hir_id);
1335-
}
1359+
sym::auto_cfg => {
1360+
self.check_doc_auto_cfg(meta, hir_id);
13361361
}
13371362

13381363
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
@@ -534,6 +534,7 @@ symbols! {
534534
attributes,
535535
audit_that,
536536
augmented_assignments,
537+
auto_cfg,
537538
auto_traits,
538539
autodiff,
539540
automatically_derived,
@@ -1093,6 +1094,8 @@ symbols! {
10931094
hashset_iter_ty,
10941095
hexagon_target_feature,
10951096
hidden,
1097+
hidden_cfg,
1098+
hide,
10961099
hint,
10971100
homogeneous_aggregate,
10981101
host,
@@ -1878,6 +1881,7 @@ symbols! {
18781881
shl_assign,
18791882
shorter_tail_lifetimes,
18801883
should_panic,
1884+
show,
18811885
shr,
18821886
shr_assign,
18831887
sig_dfl,

‎library/alloc/src/lib.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,30 @@
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-
not(any(test, bootstrap)),
70-
no_global_oom_handling,
71-
not(no_global_oom_handling),
72-
not(no_rc),
73-
not(no_sync),
74-
target_has_atomic = "ptr"
75-
))]
67+
#![cfg_attr(
68+
bootstrap,
69+
doc(cfg_hide(
70+
not(test),
71+
not(any(test, bootstrap)),
72+
no_global_oom_handling,
73+
not(no_global_oom_handling),
74+
not(no_rc),
75+
not(no_sync),
76+
target_has_atomic = "ptr"
77+
))
78+
)]
79+
#![cfg_attr(
80+
not(bootstrap),
81+
doc(auto_cfg(hide(
82+
not(test),
83+
not(any(test, bootstrap)),
84+
no_global_oom_handling,
85+
not(no_global_oom_handling),
86+
not(no_rc),
87+
not(no_sync),
88+
target_has_atomic = "ptr"
89+
)))
90+
)]
7691
#![doc(rust_logo)]
7792
#![feature(rustdoc_internals)]
7893
#![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: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,24 @@
235235
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
236236
)]
237237
#![doc(rust_logo)]
238-
#![doc(cfg_hide(
239-
not(test),
240-
not(any(test, bootstrap)),
241-
no_global_oom_handling,
242-
not(no_global_oom_handling)
243-
))]
238+
#![cfg_attr(
239+
bootstrap,
240+
doc(cfg_hide(
241+
not(test),
242+
not(any(test, bootstrap)),
243+
no_global_oom_handling,
244+
not(no_global_oom_handling)
245+
))
246+
)]
247+
#![cfg_attr(
248+
not(bootstrap),
249+
doc(auto_cfg(hide(
250+
not(test),
251+
not(any(test, bootstrap)),
252+
no_global_oom_handling,
253+
not(no_global_oom_handling)
254+
)))
255+
)]
244256
// Don't link to std. We are std.
245257
#![no_std]
246258
// Tell the compiler to link to either panic_abort or panic_unwind

‎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;
@@ -384,6 +384,7 @@ pub(crate) fn merge_attrs(
384384
cx: &mut DocContext<'_>,
385385
old_attrs: &[hir::Attribute],
386386
new_attrs: Option<(&[hir::Attribute], Option<LocalDefId>)>,
387+
cfg_info: &mut CfgInfo,
387388
) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
388389
// NOTE: If we have additional attributes (from a re-export),
389390
// always insert them first. This ensure that re-export
@@ -398,12 +399,12 @@ pub(crate) fn merge_attrs(
398399
} else {
399400
Attributes::from_hir(&both)
400401
},
401-
extract_cfg_from_attrs(both.iter(), cx.tcx, &cx.cache.hidden_cfg),
402+
extract_cfg_from_attrs(both.iter(), cx.tcx, cfg_info),
402403
)
403404
} else {
404405
(
405406
Attributes::from_hir(old_attrs),
406-
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, &cx.cache.hidden_cfg),
407+
extract_cfg_from_attrs(old_attrs.iter(), cx.tcx, cfg_info),
407408
)
408409
}
409410
}
@@ -582,7 +583,7 @@ pub(crate) fn build_impl(
582583
});
583584
}
584585

585-
let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs);
586+
let (merged_attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs,&mutCfgInfo::default());
586587
trace!("merged_attrs={merged_attrs:?}");
587588

588589
trace!(

‎src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,10 @@ fn generate_item_with_correct_attrs(
198198
// We only keep the item's attributes.
199199
target_attrs.iter().map(|attr| (Cow::Borrowed(attr), None)).collect()
200200
};
201-
let cfg = extract_cfg_from_attrs(
202-
attrs.iter().map(move |(attr, _)| match attr {
203-
Cow::Borrowed(attr) => *attr,
204-
Cow::Owned(attr) => attr,
205-
}),
206-
cx.tcx,
207-
&cx.cache.hidden_cfg,
208-
);
209201
let attrs = Attributes::from_hir_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
210202

211203
let name = renamed.or(Some(name));
212-
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, attrs, cfg);
204+
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, attrs, None);
213205
item.inner.inline_stmt_id = import_id;
214206
item
215207
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /