Skip to content

Conversation

jdonszelmann
Copy link
Contributor

@jdonszelmann jdonszelmann commented Oct 18, 2025

This PR does a couple of things. First of all, I found an ICE 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 #114920

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 18, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 18, 2025

noratrieb is not on the review rotation at the moment.
They may take a while to respond.


if !is_bench {
err.with_span_suggestion(attr_sp,
"replace with conditional compilation to make the item only exist when tests are being run",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only give the suggestion of cfg(test) when we're dealing with test attrs

@@ -0,0 +1,48 @@
#![feature(test)]

// test is a built-in macro, not a built-in attribute, but it kind of acts like both.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New test for test attributes. The previous tests simply asserted no errors would be given but that's not true anymore, and I think they should error!

// non-crate-level #[test] attributes seem to be ignored.

#[test]
mod test { mod inner { #![test] }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these were weird here anyway, test isn't a built-in attribute but a built-in macro!

@@ -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};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to an i32 cause signum wouldn't be valid on just 4

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 non-associated function
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the correct error here, not something about removing expressions. I think noting that the target is wrong is more important.

@@ -0,0 +1,8 @@
// ICE when applying `#![test]` to the crate root,
// though only when specified with a full path. `#![test]` is not enough.
#![core::prelude::v1::test]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests the specific ICE from the PR description

LL | let _ = #[test] 0;
| ^^^^^^^

error: removing an expression is not supported in this position
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test was testing for "empty expressions". this is super niche, can only happen with ast-based attributes (only like 10 in the compiler) and none of the examples in this test should actually remove the expression, they should instead report that the target is wrong. I can't find any other examples of node-removing attributes so I think this test is simply superfluous now.

@jdonszelmann
Copy link
Contributor Author

@bors try
@craterbot check

@craterbot
Copy link
Collaborator

🚨 Error: missing start toolchain

🆘 If you have any trouble with Crater please ask in t-infra on Zulip
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Oct 18, 2025
Fix ICE when applying test macro to crate root
@matthiaskrgr
Copy link
Member

does this fix #114920 ?

@rust-log-analyzer

This comment has been minimized.

@jdonszelmann
Copy link
Contributor Author

yea, that's actually exactly what it fixes. there was a crashtest for it I just notice but the fix is rather trivial in the end

@jdonszelmann
Copy link
Contributor Author

@bors try cancel

@rust-bors
Copy link

rust-bors bot commented Oct 18, 2025

Try build cancelled. Cancelled workflows:

@rustbot
Copy link
Collaborator

rustbot commented Oct 18, 2025

This PR changes a file inside tests/crashes. If a crash was fixed, please move into the corresponding ui subdir and add 'Fixes #' to the PR description to autoclose the issue upon merge.

@jdonszelmann
Copy link
Contributor Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Oct 18, 2025
Fix ICE when applying test macro to crate root
@rust-bors
Copy link

rust-bors bot commented Oct 18, 2025

☀️ Try build successful (CI)
Build commit: 73ffebf (73ffebff6f34db7eb167bf9edcf2afa19c3073e9, parent: ab1d2444533d829e2d5cff6634cd3c70de6d7103)

@jdonszelmann
Copy link
Contributor Author

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-147841 created and queued.
🤖 Automatically detected try build 73ffebf
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 18, 2025
@craterbot
Copy link
Collaborator

🚧 Experiment pr-147841 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-crater Status: Waiting on a crater run to be completed. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE: expected exactly one crate

6 participants