about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-03-20 07:10:31 +0100
committerGitHub <noreply@github.com>2023-03-20 07:10:31 +0100
commit5d3f4607089722dcf7fa46d072df77f4aed9a3ae (patch)
treec35b4de24cc6b1cc49f250d501495d1c2e7b5a91
parentfdb1eefc73c84aa227d3a8b5d70d798581a947d8 (diff)
parentc05bebcd67b08acd011dc8e0ef025f5f88bf2659 (diff)
downloadrust-5d3f4607089722dcf7fa46d072df77f4aed9a3ae.tar.gz
rust-5d3f4607089722dcf7fa46d072df77f4aed9a3ae.zip
Rollup merge of #109301 - Ezrashaw:fix-ctf-ice, r=Nilstrieb
fix: fix ICE in `custom-test-frameworks` feature

Fixes #107454

Simple fix to emit error instead of ICEing. At some point, all the code in `tests.rs` should be refactored, there is a bit of duplication (this PR's code is repeated five times over lol).

r? `@Nilstrieb` (active on the linked issue?)
-rw-r--r--compiler/rustc_builtin_macros/src/test.rs26
-rw-r--r--tests/ui/test-attrs/custom-test-frameworks/issue-107454.rs10
-rw-r--r--tests/ui/test-attrs/custom-test-frameworks/issue-107454.stderr15
3 files changed, 49 insertions, 2 deletions
diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs
index 007d14b0e74..151afd2d458 100644
--- a/compiler/rustc_builtin_macros/src/test.rs
+++ b/compiler/rustc_builtin_macros/src/test.rs
@@ -33,7 +33,23 @@ pub fn expand_test_case(
     }
 
     let sp = ecx.with_def_site_ctxt(attr_sp);
-    let mut item = anno_item.expect_item();
+    let (mut item, is_stmt) = match anno_item {
+        Annotatable::Item(item) => (item, false),
+        Annotatable::Stmt(stmt) if let ast::StmtKind::Item(_) = stmt.kind => if let ast::StmtKind::Item(i) = stmt.into_inner().kind {
+            (i, true)
+        } else {
+            unreachable!()
+        },
+        _ => {
+            ecx.struct_span_err(
+                anno_item.span(),
+                "`#[test_case]` attribute is only allowed on items",
+            )
+            .emit();
+
+            return vec![];
+        }
+    };
     item = item.map(|mut item| {
         let test_path_symbol = Symbol::intern(&item_path(
             // skip the name of the root module
@@ -50,7 +66,13 @@ pub fn expand_test_case(
         item
     });
 
-    return vec![Annotatable::Item(item)];
+    let ret = if is_stmt {
+        Annotatable::Stmt(P(ecx.stmt_item(item.span, item)))
+    } else {
+        Annotatable::Item(item)
+    };
+
+    vec![ret]
 }
 
 pub fn expand_test(
diff --git a/tests/ui/test-attrs/custom-test-frameworks/issue-107454.rs b/tests/ui/test-attrs/custom-test-frameworks/issue-107454.rs
new file mode 100644
index 00000000000..2bb133e8bfd
--- /dev/null
+++ b/tests/ui/test-attrs/custom-test-frameworks/issue-107454.rs
@@ -0,0 +1,10 @@
+// compile-flags: --test
+
+#![feature(custom_test_frameworks)]
+#![deny(unnameable_test_items)]
+
+fn foo() {
+    #[test_case]
+    //~^ ERROR cannot test inner items [unnameable_test_items]
+    fn test2() {}
+}
diff --git a/tests/ui/test-attrs/custom-test-frameworks/issue-107454.stderr b/tests/ui/test-attrs/custom-test-frameworks/issue-107454.stderr
new file mode 100644
index 00000000000..bd604afb79f
--- /dev/null
+++ b/tests/ui/test-attrs/custom-test-frameworks/issue-107454.stderr
@@ -0,0 +1,15 @@
+error: cannot test inner items
+  --> $DIR/issue-107454.rs:7:5
+   |
+LL |     #[test_case]
+   |     ^^^^^^^^^^^^
+   |
+note: the lint level is defined here
+  --> $DIR/issue-107454.rs:4:9
+   |
+LL | #![deny(unnameable_test_items)]
+   |         ^^^^^^^^^^^^^^^^^^^^^
+   = note: this error originates in the attribute macro `test_case` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to previous error
+