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 removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 18, 2025
@jdonszelmann
Copy link
Contributor Author

@rustbot review (@Noratrieb?)

@Noratrieb
Copy link
Member

do you think it would make sense to get quick lang team signoff here? given that it already errors in test mode I'm not sure whether we should treat it as breakage or not

@jdonszelmann
Copy link
Contributor Author

sure, let's do it. cc @rust-lang/lang

@Noratrieb
Copy link
Member

relevant part of the description

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.

From the description. Should either be an easy FCP or a decision to not even FCP, given that test mode already errored.

@traviscross traviscross added T-lang Relevant to the language team I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 28, 2025
// 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 non-associated function
Copy link
Member

Choose a reason for hiding this comment

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

This is inconsistent with the terminology we use in the reference and elsewhere.

Suggested change
//~^ 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

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'm not sure I like that convention, but I do agree with wanting it to be consistent. Changed it :)

@traviscross
Copy link
Contributor

traviscross commented Oct 29, 2025

Sounds right. Thanks @jdonszelmann, @Noratrieb.

This brings us closer in line with what the Reference says in:

https://doc.rust-lang.org/1.90.0/reference/attributes/testing.html#r-attributes.testing.test.allowed-positions

@rfcbot fcp merge

@rust-rfcbot
Copy link
Collaborator

rust-rfcbot commented Oct 29, 2025

Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 29, 2025
@tmandry
Copy link
Member

tmandry commented Oct 29, 2025

@rfcbot reviewed

1 similar comment
@nikomatsakis
Copy link
Contributor

@rfcbot reviewed

@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Oct 29, 2025
@rust-rfcbot
Copy link
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@traviscross traviscross added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Oct 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 30, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

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

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. 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. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE: expected exactly one crate

10 participants