Skip to content

Port #[crate_name] to the new attribute parsing infrastructure #137729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jdonszelmann
Copy link
Contributor

@jdonszelmann jdonszelmann commented Feb 27, 2025

r? @fmease

Closes #137687

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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 Feb 27, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@jdonszelmann jdonszelmann force-pushed the fix-137687 branch 2 times, most recently from 4e90cee to 21838d5 Compare February 27, 2025 16:51
@jdonszelmann
Copy link
Contributor Author

@fmease and I decided to delay this PR for a bit until more diagnostic infra for attrs lands (hopefully next week) to make the duplicate error a warning to be consistent with current master.

@fmease fmease added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 6, 2025
@fmease
Copy link
Member

fmease commented Mar 6, 2025

r=fmease,bjorn3 once that PR lands and you can downgrade the error to a lint warning again.

@bors delegate+

@bors
Copy link
Collaborator

bors commented Mar 6, 2025

✌️ @jdonszelmann, you can now approve this pull request!

If @fmease told you to "r=me" after making some further change, please make that change, then do @bors r=@fmease

@bors

This comment was marked as resolved.

@wesleywiser
Copy link
Member

Hi @jdonszelmann and @fmease, #137687 has reached beta so it would be great if we could resolve that via a beta-backport of this PR. Do you think it's possible to resolve the review feedback and merge this PR in the next week or so?

I can probably pitch in to help if necessary 🙂

@jdonszelmann
Copy link
Contributor Author

Hi there. We wanted to delay this until attribute diagnostics. Something that should not be a lot of work but I have not worked as much on recently as I maybe should. I can't to this tomorrow, but likely next week. The pr has been reviewed but there was a bit of an issue around incremental, which I now know how to solve but just haven't yet. Is that a reasonable timeframe? I am pretty busy with organising rust week atm, but knowing the solution I guess the only important thing is that it gets quick reviews? Let me know how that sounds

@wesleywiser
Copy link
Member

Just to clarify, will this PR then depend on the attribute diagnostics work? (Is that #138164?)

Generally, for backports, we try to keep the backport as small as possible so if this PR can be made to work independently of that PR (possibly even with a degraded diagnostics experience), that would be highly preferred.

@jdonszelmann
Copy link
Contributor Author

No. We decided to delay this because it's relatively low prio and any nice and permanent solution for this depends on that. I'll make a temp fix that at least doesn't ice separately then, that sounds better for now. I don't mind doing it, I know the problem now and then I also know how to fix it permanently later. You'll hear from me

@theemathas
Copy link
Contributor

theemathas commented May 1, 2025

Note that #137687 actually has two separate problems/subissues:

  1. A problem (lower priority) involving the #[crate_name] attribute.
  2. A problem (higher priority, found in a crater run) involving user-defined macro_rules and derive macros.

This PR only appears to address the first problem. The second problem would probably need to have a separate fix.

@jdonszelmann
Copy link
Contributor Author

jdonszelmann commented May 2, 2025

ok this is weird, I've copied just the tests over to master to fix it in a temporary way, but can no longer reporoduce the issue. Something else must have solved it in the meantime. Let me see what.

@jieyouxu
Copy link
Member

jieyouxu commented May 3, 2025

(I know I've said this in the other PR too, but just in case)

Just a heads up: I split #137687 into two issues:

@fmease fmease changed the title add test to reproduce #137687 and fix it by converting #[crate_name] to a new-style attribute parser Convert #[crate_name] to a new-style attribute parser Aug 21, 2025
@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 21, 2025
@fmease fmease changed the title Convert #[crate_name] to a new-style attribute parser Port #[crate_name] to the new attribute parsing infrastructure Aug 21, 2025
@bors
Copy link
Collaborator

bors commented Aug 22, 2025

☔ The latest upstream changes (presumably #145728) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot
Copy link
Collaborator

rustbot commented Aug 23, 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.

@jdonszelmann
Copy link
Contributor Author

This PR just keeps on giving hehe

@jdonszelmann
Copy link
Contributor Author

rebased to incorporate @joshtriplett 's changes to early lints which makes things much nicer.

@jdonszelmann jdonszelmann force-pushed the fix-137687 branch 2 times, most recently from 1a239f7 to 5dede49 Compare August 23, 2025 12:32
@@ -361,12 +362,8 @@ fn expr_to_lit(
// the error because an earlier error will have already
// been reported.
let msg = "attribute value must be a literal";
let mut err = psess.dcx().struct_span_err(span, msg);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

there's a subtle bug in this version, fixed in my last commit. When an expression in an attribute is a macro, and the macro expands to a literal, and we parse an attribute with ShouldEmit::Nothing, we would delay that as a bug expecting a second parse to emit the error. However, when the macro then expands to a literal, we don't hit the same codepath and we never emit an error about this. The delayed bug turns into an ICE.

A very similar problem was happening in February already (#137687) which was recognized and even a backport was made for it. However, in later versions it still made it to stable.

The last commit of this PR adds tests for these cases and fixes this by, when we expect a literal, but have ShouldEmit::Nothing, we don't delay a bug, instead we do nothing. This is correct because if the macro indeed turns into a literal, the second parse might very well be correct. We shouldn't have delayed a bug at all.

Specifically for crate_name, we parse with ShouldEmit::EarlyFatal so when it cares about this being a literal, it will error about it as it should.

error: attribute value must be a literal
--> $DIR/path-attr-in-const-block.rs:6:19
|
LL | #![path = foo!()]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

extra error, but this is correct/expected. bonus!

@@ -0,0 +1,6 @@
//@ check-pass
#[deprecated = concat !()]
Copy link
Contributor Author

@jdonszelmann jdonszelmann Aug 23, 2025

Choose a reason for hiding this comment

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

regression test for a new case of #137687

Copy link
Contributor Author

@jdonszelmann jdonszelmann Aug 23, 2025

Choose a reason for hiding this comment

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

@@ -0,0 +1,9 @@
#![deny(unused)]

#[crate_name = concat !()]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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: expr in place where literal is expected (builtin attr parsing)
10 participants