about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2021-06-17 23:41:02 +0200
committerGitHub <noreply@github.com>2021-06-17 23:41:02 +0200
commit0274401f93de16c04850fb99b535151e5abd0537 (patch)
treece41c9496f3625cb053cad6a073f511c00419041
parent3d7437fa214d56df7878db9bd9479c75798df8bf (diff)
parente9e844f44cf21bfa9ff8931b9256da57a2dec79f (diff)
downloadrust-0274401f93de16c04850fb99b535151e5abd0537.tar.gz
rust-0274401f93de16c04850fb99b535151e5abd0537.zip
Rollup merge of #86401 - FabianWolff:issue-83512, r=LeSeulArtichaut
Fix ICE when using `#[doc(keyword = "...")]` on non-items

This pull request fixes #83512. The code for checking attributes calls `expect_item()` when it shouldn't, thus causing an ICE. I have implemented a proper check for the node kind, so that an error is reported instead of the ICE.
-rw-r--r--compiler/rustc_passes/src/check_attr.rs7
-rw-r--r--src/test/ui/rustdoc/doc_keyword.rs8
-rw-r--r--src/test/ui/rustdoc/doc_keyword.stderr8
3 files changed, 20 insertions, 3 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index eca84c791fb..e85392cf0bd 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -525,8 +525,11 @@ impl CheckAttrVisitor<'tcx> {
             self.doc_attr_str_error(meta, "keyword");
             return false;
         }
-        match self.tcx.hir().expect_item(hir_id).kind {
-            ItemKind::Mod(ref module) => {
+        match self.tcx.hir().find(hir_id).and_then(|node| match node {
+            hir::Node::Item(item) => Some(&item.kind),
+            _ => None,
+        }) {
+            Some(ItemKind::Mod(ref module)) => {
                 if !module.item_ids.is_empty() {
                     self.tcx
                         .sess
diff --git a/src/test/ui/rustdoc/doc_keyword.rs b/src/test/ui/rustdoc/doc_keyword.rs
index 4c72e7e9684..4518f77ef93 100644
--- a/src/test/ui/rustdoc/doc_keyword.rs
+++ b/src/test/ui/rustdoc/doc_keyword.rs
@@ -10,3 +10,11 @@ mod foo {
 
 #[doc(keyword = "hall")] //~ ERROR
 fn foo() {}
+
+
+// Regression test for the ICE described in #83512.
+trait Foo {
+    #[doc(keyword = "match")]
+    //~^ ERROR: `#[doc(keyword = "...")]` can only be used on modules
+    fn quux() {}
+}
diff --git a/src/test/ui/rustdoc/doc_keyword.stderr b/src/test/ui/rustdoc/doc_keyword.stderr
index 0679bb8c5a7..6ba7034d541 100644
--- a/src/test/ui/rustdoc/doc_keyword.stderr
+++ b/src/test/ui/rustdoc/doc_keyword.stderr
@@ -10,11 +10,17 @@ error: `#[doc(keyword = "...")]` can only be used on modules
 LL | #[doc(keyword = "hall")]
    |       ^^^^^^^^^^^^^^^^
 
+error: `#[doc(keyword = "...")]` can only be used on modules
+  --> $DIR/doc_keyword.rs:17:11
+   |
+LL |     #[doc(keyword = "match")]
+   |           ^^^^^^^^^^^^^^^^^
+
 error: `#![doc(keyword = "...")]` isn't allowed as a crate-level attribute
   --> $DIR/doc_keyword.rs:4:8
    |
 LL | #![doc(keyword = "hello")]
    |        ^^^^^^^^^^^^^^^^^
 
-error: aborting due to 3 previous errors
+error: aborting due to 4 previous errors