Skip to content

Commit 2773990

Browse files
Strenghten checks for doc(auto_cfg(show/hide)) attributes
1 parent 0cdcd40 commit 2773990

File tree

7 files changed

+36
-16
lines changed

7 files changed

+36
-16
lines changed

compiler/rustc_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ passes_doc_auto_cfg_expects_hide_or_show =
188188
passes_doc_auto_cfg_hide_show_expects_list =
189189
`#![doc(auto_cfg({$attr_name}(...)))]` only expects a list of items
190190
191+
passes_doc_auto_cfg_hide_show_unexpected_item =
192+
`#![doc(auto_cfg({$attr_name}(...)))]` only accepts identifiers or key/values items
193+
191194
passes_doc_auto_cfg_wrong_literal =
192195
`expected boolean for #[doc(auto_cfg = ...)]`
193196

compiler/rustc_passes/src/check_attr.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,20 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12861286
meta.span,
12871287
errors::DocAutoCfgExpectsHideOrShow,
12881288
);
1289-
} else if item.meta_item_list().is_none() {
1289+
} else if let Some(list) = item.meta_item_list() {
1290+
for item in list {
1291+
if item.meta_item_list().is_some() {
1292+
self.tcx.emit_node_span_lint(
1293+
INVALID_DOC_ATTRIBUTES,
1294+
hir_id,
1295+
item.span(),
1296+
errors::DocAutoCfgHideShowUnexpectedItem {
1297+
attr_name: attr_name.as_str(),
1298+
},
1299+
);
1300+
}
1301+
}
1302+
} else {
12901303
self.tcx.emit_node_span_lint(
12911304
INVALID_DOC_ATTRIBUTES,
12921305
hir_id,

compiler/rustc_passes/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ pub(crate) struct DocAutoCfgHideShowExpectsList<'a> {
346346
pub attr_name: &'a str,
347347
}
348348

349+
#[derive(LintDiagnostic)]
350+
#[diag(passes_doc_auto_cfg_hide_show_unexpected_item)]
351+
pub(crate) struct DocAutoCfgHideShowUnexpectedItem<'a> {
352+
pub attr_name: &'a str,
353+
}
354+
349355
#[derive(LintDiagnostic)]
350356
#[diag(passes_doc_test_unknown_any)]
351357
pub(crate) struct DocTestUnknownAny {

library/alloc/src/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@
7979
#![cfg_attr(
8080
not(bootstrap),
8181
doc(auto_cfg(hide(
82-
not(test),
83-
not(any(test, bootstrap)),
82+
bootstrap,
8483
no_global_oom_handling,
85-
not(no_global_oom_handling),
86-
not(no_rc),
87-
not(no_sync),
84+
no_global_oom_handling,
85+
no_rc,
86+
no_sync,
8887
target_has_atomic = "ptr"
8988
)))
9089
)]

library/std/src/lib.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,7 @@
244244
not(no_global_oom_handling)
245245
))
246246
)]
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-
)]
247+
#![cfg_attr(not(bootstrap), doc(auto_cfg(hide(bootstrap, no_global_oom_handling,))))]
256248
// Don't link to std. We are std.
257249
#![no_std]
258250
// Tell the compiler to link to either panic_abort or panic_unwind
+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#![doc(auto_cfg(hide = "test"))] //~ ERROR
22
#![doc(auto_cfg(hide))] //~ ERROR
3+
#![doc(auto_cfg(hide(not(windows))))] //~ ERROR

tests/rustdoc-ui/lints/doc_cfg_hide.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@ error: `#![doc(auto_cfg(hide(...)))]` only expects a list of items
1212
LL | #![doc(auto_cfg(hide))]
1313
| ^^^^^^^^^^^^^^
1414

15-
error: aborting due to 2 previous errors
15+
error: `#![doc(auto_cfg(hide(...)))]` only accepts identifiers or key/values items
16+
--> $DIR/doc_cfg_hide.rs:3:22
17+
|
18+
LL | #![doc(auto_cfg(hide(not(windows))))]
19+
| ^^^^^^^^^^^^
20+
21+
error: aborting due to 3 previous errors
1622

0 commit comments

Comments
 (0)