Rollup merge of #147841 - jdonszelmann:test-macro-ice, r=wafflelapkin

Fix ICE when applying test macro to crate root

This PR does a couple of things. First of all, I found [an ICE](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=a733a7f3d223e1a9712e44b571f3e5cf) that happens when applying `#![core::prelude::v1::test]` to the crate root. This is caused by the test macro not expanding to an item when `--test` isn't applied. For the crate root, that means it deletes the crate....

The fix now first does target checking, and only if the target is valid discards the item when `--test` isn't applied. The discarding is, I think, important for perf.

The problem with this PR is that it means that `#[test]` applied to structs previously would give no errors unless `--test` is applied! That sounds like a bug to me, but maybe we should crater run it just in case, since technically that's a breaking change. Errors in such items wouldn't be reported previously.

 Also fixed a smol diagnostics bug with `#[bench]`'s error messages refering to `#[test]` accidentally.

r? noratrieb (since I already explained you a bunch, feel free to re-assign)

Fixes https://github.com/rust-lang/rust/issues/114920
This commit is contained in:
Matthias Krüger 2025-12-04 08:46:17 +01:00 committed by GitHub
commit 5019bdaefe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 500 additions and 321 deletions

View file

@ -34,10 +34,6 @@ pub(crate) fn expand_test_case(
check_builtin_macro_attribute(ecx, meta_item, sym::test_case);
warn_on_duplicate_attribute(ecx, &anno_item, sym::test_case);
if !ecx.ecfg.should_test {
return vec![];
}
let sp = ecx.with_def_site_ctxt(attr_sp);
let (mut item, is_stmt) = match anno_item {
Annotatable::Item(item) => (item, false),
@ -54,6 +50,10 @@ pub(crate) fn expand_test_case(
}
};
if !ecx.ecfg.should_test {
return vec![];
}
// `#[test_case]` is valid on functions, consts, and statics. Only modify
// the item in those cases.
match &mut item.kind {
@ -113,22 +113,17 @@ pub(crate) fn expand_test_or_bench(
item: Annotatable,
is_bench: bool,
) -> Vec<Annotatable> {
// If we're not in test configuration, remove the annotated item
if !cx.ecfg.should_test {
return vec![];
}
let (item, is_stmt) = match item {
Annotatable::Item(i) => (i, false),
Annotatable::Stmt(box ast::Stmt { kind: ast::StmtKind::Item(i), .. }) => (i, true),
other => {
not_testable_error(cx, attr_sp, None);
not_testable_error(cx, is_bench, attr_sp, None);
return vec![other];
}
};
let ast::ItemKind::Fn(fn_) = &item.kind else {
not_testable_error(cx, attr_sp, Some(&item));
not_testable_error(cx, is_bench, attr_sp, Some(&item));
return if is_stmt {
vec![Annotatable::Stmt(Box::new(cx.stmt_item(item.span, item)))]
} else {
@ -136,6 +131,11 @@ pub(crate) fn expand_test_or_bench(
};
};
// If we're not in test configuration, remove the annotated item
if !cx.ecfg.should_test {
return vec![];
}
if let Some(attr) = attr::find_by_name(&item.attrs, sym::naked) {
cx.dcx().emit_err(errors::NakedFunctionTestingAttribute {
testing_span: attr_sp,
@ -405,9 +405,10 @@ pub(crate) fn expand_test_or_bench(
}
}
fn not_testable_error(cx: &ExtCtxt<'_>, attr_sp: Span, item: Option<&ast::Item>) {
fn not_testable_error(cx: &ExtCtxt<'_>, is_bench: bool, attr_sp: Span, item: Option<&ast::Item>) {
let dcx = cx.dcx();
let msg = "the `#[test]` attribute may only be used on a non-associated function";
let name = if is_bench { "bench" } else { "test" };
let msg = format!("the `#[{name}]` attribute may only be used on a free function");
let level = match item.map(|i| &i.kind) {
// These were a warning before #92959 and need to continue being that to avoid breaking
// stable user code (#94508).
@ -426,12 +427,16 @@ fn not_testable_error(cx: &ExtCtxt<'_>, attr_sp: Span, item: Option<&ast::Item>)
),
);
}
err.with_span_label(attr_sp, "the `#[test]` macro causes a function to be run as a test and has no effect on non-functions")
.with_span_suggestion(attr_sp,
err.span_label(attr_sp, format!("the `#[{name}]` macro causes a function to be run as a test and has no effect on non-functions"));
if !is_bench {
err.with_span_suggestion(attr_sp,
"replace with conditional compilation to make the item only exist when tests are being run",
"#[cfg(test)]",
Applicability::MaybeIncorrect)
.emit();
Applicability::MaybeIncorrect).emit();
} else {
err.emit();
}
}
fn get_location_info(cx: &ExtCtxt<'_>, fn_: &ast::Fn) -> (Symbol, usize, usize, usize, usize) {

View file

@ -1,2 +0,0 @@
//@ known-bug: #114920
#![core::prelude::v1::test]

View file

@ -0,0 +1,48 @@
#![feature(test)]
// test is a built-in macro, not a built-in attribute, but it kind of acts like both.
// check its target checking anyway here
#[test]
//~^ ERROR the `#[test]` attribute may only be used on a free function
mod test {
mod inner { #![test] }
//~^ ERROR inner macro attributes are unstable
//~| ERROR the `#[test]` attribute may only be used on a free function
#[test]
//~^ ERROR the `#[test]` attribute may only be used on a free function
struct S;
#[test]
//~^ ERROR the `#[test]` attribute may only be used on a free function
type T = S;
#[test]
//~^ ERROR the `#[test]` attribute may only be used on a free function
impl S { }
}
// At time of unit test authorship, if compiling without `--test` then
// non-crate-level #[bench] attributes seem to be ignored.
#[bench]
//~^ ERROR the `#[bench]` attribute may only be used on a free function
mod bench {
mod inner { #![bench] }
//~^ ERROR inner macro attributes are unstable
//~| ERROR the `#[bench]` attribute may only be used on a free function
#[bench]
//~^ ERROR the `#[bench]` attribute may only be used on a free function
struct S;
#[bench]
//~^ ERROR the `#[bench]` attribute may only be used on a free function
type T = S;
#[bench]
//~^ ERROR the `#[bench]` attribute may only be used on a free function
impl S { }
}
fn main() {}

View file

@ -0,0 +1,151 @@
error: the `#[test]` attribute may only be used on a free function
--> $DIR/gating-of-test-attrs.rs:5:1
|
LL | #[test]
| ^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
LL |
LL | / mod test {
LL | | mod inner { #![test] }
... |
LL | | impl S { }
LL | | }
| |_- expected a non-associated function, found a module
|
help: replace with conditional compilation to make the item only exist when tests are being run
|
LL - #[test]
LL + #[cfg(test)]
|
error[E0658]: inner macro attributes are unstable
--> $DIR/gating-of-test-attrs.rs:8:20
|
LL | mod inner { #![test] }
| ^^^^
|
= note: see issue #54726 <https://github.com/rust-lang/rust/issues/54726> for more information
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: the `#[test]` attribute may only be used on a free function
--> $DIR/gating-of-test-attrs.rs:8:17
|
LL | mod inner { #![test] }
| ------------^^^^^^^^--
| | |
| | the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
| expected a non-associated function, found a module
|
help: replace with conditional compilation to make the item only exist when tests are being run
|
LL - mod inner { #![test] }
LL + mod inner { #[cfg(test)] }
|
error: the `#[test]` attribute may only be used on a free function
--> $DIR/gating-of-test-attrs.rs:12:5
|
LL | #[test]
| ^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
LL |
LL | struct S;
| --------- expected a non-associated function, found a struct
|
help: replace with conditional compilation to make the item only exist when tests are being run
|
LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a free function
--> $DIR/gating-of-test-attrs.rs:16:5
|
LL | #[test]
| ^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
LL |
LL | type T = S;
| ----------- expected a non-associated function, found a type alias
|
help: replace with conditional compilation to make the item only exist when tests are being run
|
LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a free function
--> $DIR/gating-of-test-attrs.rs:20:5
|
LL | #[test]
| ^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
LL |
LL | impl S { }
| ---------- expected a non-associated function, found an implementation
|
help: replace with conditional compilation to make the item only exist when tests are being run
|
LL - #[test]
LL + #[cfg(test)]
|
error: the `#[bench]` attribute may only be used on a free function
--> $DIR/gating-of-test-attrs.rs:28:1
|
LL | #[bench]
| ^^^^^^^^ the `#[bench]` macro causes a function to be run as a test and has no effect on non-functions
LL |
LL | / mod bench {
LL | | mod inner { #![bench] }
... |
LL | | impl S { }
LL | | }
| |_- expected a non-associated function, found a module
error[E0658]: inner macro attributes are unstable
--> $DIR/gating-of-test-attrs.rs:31:20
|
LL | mod inner { #![bench] }
| ^^^^^
|
= note: see issue #54726 <https://github.com/rust-lang/rust/issues/54726> for more information
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: the `#[bench]` attribute may only be used on a free function
--> $DIR/gating-of-test-attrs.rs:31:17
|
LL | mod inner { #![bench] }
| ------------^^^^^^^^^--
| | |
| | the `#[bench]` macro causes a function to be run as a test and has no effect on non-functions
| expected a non-associated function, found a module
error: the `#[bench]` attribute may only be used on a free function
--> $DIR/gating-of-test-attrs.rs:35:5
|
LL | #[bench]
| ^^^^^^^^ the `#[bench]` macro causes a function to be run as a test and has no effect on non-functions
LL |
LL | struct S;
| --------- expected a non-associated function, found a struct
error: the `#[bench]` attribute may only be used on a free function
--> $DIR/gating-of-test-attrs.rs:39:5
|
LL | #[bench]
| ^^^^^^^^ the `#[bench]` macro causes a function to be run as a test and has no effect on non-functions
LL |
LL | type T = S;
| ----------- expected a non-associated function, found a type alias
error: the `#[bench]` attribute may only be used on a free function
--> $DIR/gating-of-test-attrs.rs:43:5
|
LL | #[bench]
| ^^^^^^^^ the `#[bench]` macro causes a function to be run as a test and has no effect on non-functions
LL |
LL | impl S { }
| ---------- expected a non-associated function, found an implementation
error: aborting due to 12 previous errors
For more information about this error, try `rustc --explain E0658`.

View file

@ -33,7 +33,6 @@
//@ check-pass
#![feature(test)]
#![warn(unused_attributes, unknown_lints)]
//~^ NOTE the lint level is defined here
//~| NOTE the lint level is defined here
@ -250,38 +249,6 @@ mod macro_export {
//~| HELP remove the attribute
}
// At time of unit test authorship, if compiling without `--test` then
// non-crate-level #[test] attributes seem to be ignored.
#[test]
mod test { mod inner { #![test] }
fn f() { }
struct S;
type T = S;
impl S { }
}
// At time of unit test authorship, if compiling without `--test` then
// non-crate-level #[bench] attributes seem to be ignored.
#[bench]
mod bench {
mod inner { #![bench] }
#[bench]
struct S;
#[bench]
type T = S;
#[bench]
impl S { }
}
#[path = "3800"]
mod path {
mod inner { #![path="3800"] }

View file

@ -1,11 +0,0 @@
// AST-based macro attributes expanding to an empty expression produce an error and not ICE.
#![feature(custom_test_frameworks)]
#![feature(stmt_expr_attributes)]
#![feature(test)]
fn main() {
let _ = #[test] 0; //~ ERROR removing an expression is not supported in this position
let _ = #[bench] 1; //~ ERROR removing an expression is not supported in this position
let _ = #[test_case] 2; //~ ERROR removing an expression is not supported in this position
}

View file

@ -1,20 +0,0 @@
error: removing an expression is not supported in this position
--> $DIR/attr-empty-expr.rs:8:13
|
LL | let _ = #[test] 0;
| ^^^^^^^
error: removing an expression is not supported in this position
--> $DIR/attr-empty-expr.rs:9:13
|
LL | let _ = #[bench] 1;
| ^^^^^^^^
error: removing an expression is not supported in this position
--> $DIR/attr-empty-expr.rs:10:13
|
LL | let _ = #[test_case] 2;
| ^^^^^^^^^^^^
error: aborting due to 3 previous errors

View file

@ -5,8 +5,8 @@ macro_rules! cbor_map {
}
fn main() {
cbor_map! { #[test(test)] 4};
//~^ ERROR removing an expression is not supported in this position
cbor_map! { #[test(test)] 4i32};
//~^ ERROR the `#[test]` attribute may only be used on a free function
//~| ERROR attribute must be of the form `#[test]`
//~| WARNING this was previously accepted by the compiler but is being phased out
}

View file

@ -1,13 +1,19 @@
error: removing an expression is not supported in this position
error: the `#[test]` attribute may only be used on a free function
--> $DIR/issue-111749.rs:8:17
|
LL | cbor_map! { #[test(test)] 4};
| ^^^^^^^^^^^^^
LL | cbor_map! { #[test(test)] 4i32};
| ^^^^^^^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
|
help: replace with conditional compilation to make the item only exist when tests are being run
|
LL - cbor_map! { #[test(test)] 4i32};
LL + cbor_map! { #[cfg(test)] 4i32};
|
error: attribute must be of the form `#[test]`
--> $DIR/issue-111749.rs:8:17
|
LL | cbor_map! { #[test(test)] 4};
LL | cbor_map! { #[test(test)] 4i32};
| ^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
@ -20,7 +26,7 @@ Future incompatibility report: Future breakage diagnostic:
error: attribute must be of the form `#[test]`
--> $DIR/issue-111749.rs:8:17
|
LL | cbor_map! { #[test(test)] 4};
LL | cbor_map! { #[test(test)] 4i32};
| ^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!

View file

@ -0,0 +1,10 @@
// Regression test for rust-lang/rust#114920
//
// Applying `#![test]` to the crate root used to ICE,
// when referring to the attribute with full path specifically.
#![core::prelude::v1::test]
//~^ ERROR inner macro attributes are unstable
//~| ERROR the `#[test]` attribute may only be used on a free function
fn main() {}

View file

@ -0,0 +1,25 @@
error[E0658]: inner macro attributes are unstable
--> $DIR/test-on-crate-root.rs:5:4
|
LL | #![core::prelude::v1::test]
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #54726 <https://github.com/rust-lang/rust/issues/54726> for more information
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-crate-root.rs:5:1
|
LL | #![core::prelude::v1::test]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `#[test]` macro causes a function to be run as a test and has no effect on non-functions
|
help: replace with conditional compilation to make the item only exist when tests are being run
|
LL - #![core::prelude::v1::test]
LL + #[cfg(test)]
|
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0658`.

View file

@ -2,6 +2,6 @@
fn align_offset_weird_strides() {
#[test]
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
//~^ ERROR the `#[test]` attribute may only be used on a free function
struct A5(u32, u8);
}

View file

@ -1,4 +1,4 @@
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/issue-109816.rs:4:5
|
LL | #[test]

View file

@ -4,12 +4,12 @@ struct A {}
impl A {
#[test]
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
//~^ ERROR the `#[test]` attribute may only be used on a free function
fn new() -> A {
A {}
}
#[test]
//~^ ERROR the `#[test]` attribute may only be used on a non-associated function
//~^ ERROR the `#[test]` attribute may only be used on a free function
fn recovery_witness() -> A {
A {}
}

View file

@ -1,4 +1,4 @@
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-attr-non-associated-functions.rs:6:5
|
LL | #[test]
@ -10,7 +10,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-attr-non-associated-functions.rs:11:5
|
LL | #[test]

View file

@ -1,9 +1,9 @@
//@ compile-flags: --test
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
mod test {}
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
mod loooooooooooooong_teeeeeeeeeest {
/*
this is a comment
@ -17,37 +17,37 @@ mod loooooooooooooong_teeeeeeeeeest {
*/
}
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
extern "C" {}
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
trait Foo {}
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
impl Foo for i32 {}
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
const FOO: i32 = -1_i32;
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
static BAR: u64 = 10_000_u64;
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
enum MyUnit {
Unit,
}
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
struct NewI32(i32);
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
union Spooky {
x: i32,
y: u32,
}
#[repr(C, align(64))]
#[test] //~ ERROR: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ ERROR: the `#[test]` attribute may only be used on a free function
#[derive(Copy, Clone, Debug)]
struct MoreAttrs {
a: i32,
@ -58,7 +58,7 @@ macro_rules! foo {
() => {};
}
#[test] //~ WARN: the `#[test]` attribute may only be used on a non-associated function
#[test] //~ WARN: the `#[test]` attribute may only be used on a free function
foo!();
// make sure it doesn't erroneously trigger on a real test

View file

@ -1,4 +1,4 @@
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:3:1
|
LL | #[test]
@ -12,7 +12,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:6:1
|
LL | #[test]
@ -32,7 +32,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:20:1
|
LL | #[test]
@ -46,7 +46,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:23:1
|
LL | #[test]
@ -60,7 +60,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:26:1
|
LL | #[test]
@ -74,7 +74,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:29:1
|
LL | #[test]
@ -88,7 +88,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:32:1
|
LL | #[test]
@ -102,7 +102,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:35:1
|
LL | #[test]
@ -118,7 +118,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:40:1
|
LL | #[test]
@ -132,7 +132,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:43:1
|
LL | #[test]
@ -149,7 +149,7 @@ LL - #[test]
LL + #[cfg(test)]
|
error: the `#[test]` attribute may only be used on a non-associated function
error: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:50:1
|
LL | #[test]
@ -167,7 +167,7 @@ LL - #[test]
LL + #[cfg(test)]
|
warning: the `#[test]` attribute may only be used on a non-associated function
warning: the `#[test]` attribute may only be used on a free function
--> $DIR/test-on-not-fn.rs:61:1
|
LL | #[test]