about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-06-05 23:47:57 +0200
committerGitHub <noreply@github.com>2023-06-05 23:47:57 +0200
commit129a57a9f6f7824ce59e70c3dcc6584447d38898 (patch)
tree6032be75606b7537aea4a3a0c6af844ae69dc045
parent408bbd040613f6776e0a7d05d582c81f4ddc189a (diff)
parent70bbcceaeccc529670f9c9d60cd4fb038ee49fe4 (diff)
downloadrust-129a57a9f6f7824ce59e70c3dcc6584447d38898.tar.gz
rust-129a57a9f6f7824ce59e70c3dcc6584447d38898.zip
Rollup merge of #112081 - obeis:doc-test-literal, r=compiler-errors
Avoid ICE on `#![doc(test(...)]` with literal parameter

Close #109066

r? `@compiler-errors`
-rw-r--r--compiler/rustc_passes/messages.ftl2
-rw-r--r--compiler/rustc_passes/src/check_attr.rs19
-rw-r--r--compiler/rustc_passes/src/errors.rs4
-rw-r--r--tests/ui/attributes/doc-test-literal.rs7
-rw-r--r--tests/ui/attributes/doc-test-literal.stderr17
5 files changed, 43 insertions, 6 deletions
diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl
index 7f9222dac6c..e76f1614b93 100644
--- a/compiler/rustc_passes/messages.ftl
+++ b/compiler/rustc_passes/messages.ftl
@@ -211,6 +211,8 @@ passes_doc_keyword_not_mod =
 passes_doc_keyword_only_impl =
     `#[doc(keyword = "...")]` should be used on impl blocks
 
+passes_doc_test_literal = `#![doc(test(...)]` does not take a literal
+
 passes_doc_test_takes_list =
     `#[doc(test(...)]` takes a list of attributes
 
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index c3189d1fefe..c35c7da2664 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -944,21 +944,28 @@ impl CheckAttrVisitor<'_> {
         let mut is_valid = true;
         if let Some(metas) = meta.meta_item_list() {
             for i_meta in metas {
-                match i_meta.name_or_empty() {
-                    sym::attr | sym::no_crate_inject => {}
-                    _ => {
+                match (i_meta.name_or_empty(), i_meta.meta_item()) {
+                    (sym::attr | sym::no_crate_inject, _) => {}
+                    (_, Some(m)) => {
                         self.tcx.emit_spanned_lint(
                             INVALID_DOC_ATTRIBUTES,
                             hir_id,
                             i_meta.span(),
                             errors::DocTestUnknown {
-                                path: rustc_ast_pretty::pprust::path_to_string(
-                                    &i_meta.meta_item().unwrap().path,
-                                ),
+                                path: rustc_ast_pretty::pprust::path_to_string(&m.path),
                             },
                         );
                         is_valid = false;
                     }
+                    (_, None) => {
+                        self.tcx.emit_spanned_lint(
+                            INVALID_DOC_ATTRIBUTES,
+                            hir_id,
+                            i_meta.span(),
+                            errors::DocTestLiteral,
+                        );
+                        is_valid = false;
+                    }
                 }
             }
         } else {
diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs
index 99fc69d1bec..ae624dbc9c9 100644
--- a/compiler/rustc_passes/src/errors.rs
+++ b/compiler/rustc_passes/src/errors.rs
@@ -282,6 +282,10 @@ pub struct DocTestUnknown {
 }
 
 #[derive(LintDiagnostic)]
+#[diag(passes_doc_test_literal)]
+pub struct DocTestLiteral;
+
+#[derive(LintDiagnostic)]
 #[diag(passes_doc_test_takes_list)]
 pub struct DocTestTakesList;
 
diff --git a/tests/ui/attributes/doc-test-literal.rs b/tests/ui/attributes/doc-test-literal.rs
new file mode 100644
index 00000000000..a06a1afcb3f
--- /dev/null
+++ b/tests/ui/attributes/doc-test-literal.rs
@@ -0,0 +1,7 @@
+#![deny(warnings)]
+
+#![doc(test(""))]
+//~^ ERROR `#![doc(test(...)]` does not take a literal
+//~^^ WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
+
+fn main() {}
diff --git a/tests/ui/attributes/doc-test-literal.stderr b/tests/ui/attributes/doc-test-literal.stderr
new file mode 100644
index 00000000000..ebee09994ba
--- /dev/null
+++ b/tests/ui/attributes/doc-test-literal.stderr
@@ -0,0 +1,17 @@
+error: `#![doc(test(...)]` does not take a literal
+  --> $DIR/doc-test-literal.rs:3:13
+   |
+LL | #![doc(test(""))]
+   |             ^^
+   |
+   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
+   = note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
+note: the lint level is defined here
+  --> $DIR/doc-test-literal.rs:1:9
+   |
+LL | #![deny(warnings)]
+   |         ^^^^^^^^
+   = note: `#[deny(invalid_doc_attributes)]` implied by `#[deny(warnings)]`
+
+error: aborting due to previous error
+