about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-04-01 17:24:28 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2025-09-27 11:29:48 +0200
commit63aefe0737d0df886955f5a3aa68db865cd97f1b (patch)
tree3e3d93701bbc219210abc3c848d6fa883e287e43
parent2d82c99f1e4a4e50b47f75d767b0d1d957357c1a (diff)
downloadrust-63aefe0737d0df886955f5a3aa68db865cd97f1b.tar.gz
rust-63aefe0737d0df886955f5a3aa68db865cd97f1b.zip
Strenghten checks for `doc(auto_cfg(show/hide))` attributes
-rw-r--r--compiler/rustc_passes/messages.ftl3
-rw-r--r--compiler/rustc_passes/src/check_attr.rs15
-rw-r--r--compiler/rustc_passes/src/errors.rs6
-rw-r--r--library/alloc/src/lib.rs1
-rw-r--r--library/std/src/lib.rs8
-rw-r--r--tests/rustdoc-ui/lints/doc_cfg_hide.rs1
-rw-r--r--tests/rustdoc-ui/lints/doc_cfg_hide.stderr8
7 files changed, 32 insertions, 10 deletions
diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl
index 19014f37c66..8326ddaf194 100644
--- a/compiler/rustc_passes/messages.ftl
+++ b/compiler/rustc_passes/messages.ftl
@@ -149,6 +149,9 @@ passes_doc_auto_cfg_expects_hide_or_show =
 passes_doc_auto_cfg_hide_show_expects_list =
     `#![doc(auto_cfg({$attr_name}(...)))]` only expects a list of items
 
+passes_doc_auto_cfg_hide_show_unexpected_item =
+    `#![doc(auto_cfg({$attr_name}(...)))]` only accepts identifiers or key/values items
+
 passes_doc_auto_cfg_wrong_literal =
     `expected boolean for #[doc(auto_cfg = ...)]`
 
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index 3dc232a35ec..38b67e2f9dc 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -1187,7 +1187,20 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
                             meta.span,
                             errors::DocAutoCfgExpectsHideOrShow,
                         );
-                    } else if item.meta_item_list().is_none() {
+                    } else if let Some(list) = item.meta_item_list() {
+                        for item in list {
+                            if item.meta_item_list().is_some() {
+                                self.tcx.emit_node_span_lint(
+                                    INVALID_DOC_ATTRIBUTES,
+                                    hir_id,
+                                    item.span(),
+                                    errors::DocAutoCfgHideShowUnexpectedItem {
+                                        attr_name: attr_name.as_str(),
+                                    },
+                                );
+                            }
+                        }
+                    } else {
                         self.tcx.emit_node_span_lint(
                             INVALID_DOC_ATTRIBUTES,
                             hir_id,
diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs
index 6cc0bd6ce48..1d2428c4f9a 100644
--- a/compiler/rustc_passes/src/errors.rs
+++ b/compiler/rustc_passes/src/errors.rs
@@ -323,6 +323,12 @@ pub(crate) struct DocAutoCfgHideShowExpectsList<'a> {
 }
 
 #[derive(LintDiagnostic)]
+#[diag(passes_doc_auto_cfg_hide_show_unexpected_item)]
+pub(crate) struct DocAutoCfgHideShowUnexpectedItem<'a> {
+    pub attr_name: &'a str,
+}
+
+#[derive(LintDiagnostic)]
 #[diag(passes_doc_test_unknown_any)]
 pub(crate) struct DocTestUnknownAny {
     pub path: String,
diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs
index 243fdc2e843..dc5d243e882 100644
--- a/library/alloc/src/lib.rs
+++ b/library/alloc/src/lib.rs
@@ -78,7 +78,6 @@
 #![cfg_attr(
     not(bootstrap),
     doc(auto_cfg(hide(
-        test,
         no_global_oom_handling,
         no_rc,
         no_sync,
diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs
index 93c91b61525..ecd354f599e 100644
--- a/library/std/src/lib.rs
+++ b/library/std/src/lib.rs
@@ -243,13 +243,7 @@
         not(no_global_oom_handling)
     ))
 )]
-#![cfg_attr(
-    not(bootstrap),
-    doc(auto_cfg(hide(
-        test,
-        no_global_oom_handling,
-    )))
-)]
+#![cfg_attr(not(bootstrap), doc(auto_cfg(hide(no_global_oom_handling))))]
 // Don't link to std. We are std.
 #![no_std]
 // Tell the compiler to link to either panic_abort or panic_unwind
diff --git a/tests/rustdoc-ui/lints/doc_cfg_hide.rs b/tests/rustdoc-ui/lints/doc_cfg_hide.rs
index abf56318479..4f2625d00ce 100644
--- a/tests/rustdoc-ui/lints/doc_cfg_hide.rs
+++ b/tests/rustdoc-ui/lints/doc_cfg_hide.rs
@@ -1,2 +1,3 @@
 #![doc(auto_cfg(hide = "test"))] //~ ERROR
 #![doc(auto_cfg(hide))] //~ ERROR
+#![doc(auto_cfg(hide(not(windows))))] //~ ERROR
diff --git a/tests/rustdoc-ui/lints/doc_cfg_hide.stderr b/tests/rustdoc-ui/lints/doc_cfg_hide.stderr
index bb0e52eb666..22501d63c3f 100644
--- a/tests/rustdoc-ui/lints/doc_cfg_hide.stderr
+++ b/tests/rustdoc-ui/lints/doc_cfg_hide.stderr
@@ -12,5 +12,11 @@ error: `#![doc(auto_cfg(hide(...)))]` only expects a list of items
 LL | #![doc(auto_cfg(hide))]
    |        ^^^^^^^^^^^^^^
 
-error: aborting due to 2 previous errors
+error: `#![doc(auto_cfg(hide(...)))]` only accepts identifiers or key/values items
+  --> $DIR/doc_cfg_hide.rs:3:22
+   |
+LL | #![doc(auto_cfg(hide(not(windows))))]
+   |                      ^^^^^^^^^^^^
+
+error: aborting due to 3 previous errors