Skip to content

Commit c05bebc

Browse files
committed
fix: fix ICE in custom-test-frameworks feature
1 parent 85123d2 commit c05bebc

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

compiler/rustc_builtin_macros/src/test.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,23 @@ pub fn expand_test_case(
3333
}
3434

3535
let sp = ecx.with_def_site_ctxt(attr_sp);
36-
let mut item = anno_item.expect_item();
36+
let (mut item, is_stmt) = match anno_item {
37+
Annotatable::Item(item) => (item, false),
38+
Annotatable::Stmt(stmt) if let ast::StmtKind::Item(_) = stmt.kind => if let ast::StmtKind::Item(i) = stmt.into_inner().kind {
39+
(i, true)
40+
} else {
41+
unreachable!()
42+
},
43+
_ => {
44+
ecx.struct_span_err(
45+
anno_item.span(),
46+
"`#[test_case]` attribute is only allowed on items",
47+
)
48+
.emit();
49+
50+
return vec![];
51+
}
52+
};
3753
item = item.map(|mut item| {
3854
let test_path_symbol = Symbol::intern(&item_path(
3955
// skip the name of the root module
@@ -50,7 +66,13 @@ pub fn expand_test_case(
5066
item
5167
});
5268

53-
return vec![Annotatable::Item(item)];
69+
let ret = if is_stmt {
70+
Annotatable::Stmt(P(ecx.stmt_item(item.span, item)))
71+
} else {
72+
Annotatable::Item(item)
73+
};
74+
75+
vec![ret]
5476
}
5577

5678
pub fn expand_test(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// compile-flags: --test
2+
3+
#![feature(custom_test_frameworks)]
4+
#![deny(unnameable_test_items)]
5+
6+
fn foo() {
7+
#[test_case]
8+
//~^ ERROR cannot test inner items [unnameable_test_items]
9+
fn test2() {}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: cannot test inner items
2+
--> $DIR/issue-107454.rs:7:5
3+
|
4+
LL | #[test_case]
5+
| ^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-107454.rs:4:9
9+
|
10+
LL | #![deny(unnameable_test_items)]
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
= note: this error originates in the attribute macro `test_case` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)