Skip to content
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

Fix parsing of ranges after unary operators #134900

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

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Dec 29, 2024

Fixes #134899.

This PR aligns the parsing for unary ! and - and * with how unary & is already parsed here.

--- stderr -------------------------------
error: expected expression, found `..`
  --> tests/ui/parser/ranges-precedence.rs:75:12
   |
LL |         ($e:expr) => {};
   |          ------- while parsing argument for this `expr` macro fragment
LL |     }
LL |     expr!(!..0);
   |            ^^ expected expression

error: expected expression, found `..`
  --> tests/ui/parser/ranges-precedence.rs:76:12
   |
LL |         ($e:expr) => {};
   |          ------- while parsing argument for this `expr` macro fragment
...
LL |     expr!(-..0);
   |            ^^ expected expression

error: expected expression, found `..`
  --> tests/ui/parser/ranges-precedence.rs:77:12
   |
LL |         ($e:expr) => {};
   |          ------- while parsing argument for this `expr` macro fragment
...
LL |     expr!(*..0);
   |            ^^ expected expression

error: aborting due to 3 previous errors
------------------------------------------
@rustbot
Copy link
Collaborator

rustbot commented Dec 29, 2024

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Dec 29, 2024
@dtolnay dtolnay added the A-parser Area: The parsing of Rust source code to an AST label Dec 29, 2024
@jieyouxu
Copy link
Member

r? parser (this is likely accepting new code or re-accepting code that regressed a while ago)

@rustbot rustbot assigned davidtwco and unassigned lcnr Dec 30, 2024
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

Implementation looks fine and this does align with the approach that the & operator takes. Funny enough that was implemented in #105701, and it never went through an FCP even though it was tagged as such, and obviously changes the set of parsed expressions in a way that makes new guarantees to the grammar (at least effectively? not certain if the grammar in the reference suggests that should be parsed, and I don't really want to look).

I think this is both clearly a T-lang signoff but also trivial and desirable to make, so r=me after that is approved. They also should perhaps retroactively be aware that we began to allow &.. too in 1.68.

@compiler-errors compiler-errors added T-lang Relevant to the language team, which will review and decide on the PR/issue. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 30, 2024
@oli-obk
Copy link
Contributor

oli-obk commented Feb 5, 2025

Should we crater this for the macro fragment effects this may theoretically have? Even if just to tell folk that their code needs fixing before this hits stable.

@dtolnay
Copy link
Member Author

dtolnay commented Feb 5, 2025

@bors try

@dtolnay
Copy link
Member Author

dtolnay commented Feb 5, 2025

@bors ping

@bors
Copy link
Contributor

bors commented Feb 5, 2025

😪 I'm awake I'm awake

@bors
Copy link
Contributor

bors commented Feb 6, 2025

⌛ Trying commit 462604d with merge 64628e5...

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 6, 2025
Fix parsing of ranges after unary operators

Fixes rust-lang#134899.

This PR aligns the parsing for unary `!` and `-` and `*` with how unary `&` is already parsed [here](https://github.com/rust-lang/rust/blob/5c0a6e68cfdad859615c2888de76505f13e6f01b/compiler/rustc_parse/src/parser/expr.rs#L848-L854).
@compiler-errors compiler-errors added I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination and removed I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination labels Feb 6, 2025
@bors
Copy link
Contributor

bors commented Feb 6, 2025

☀️ Try build successful - checks-actions
Build commit: 64628e5 (64628e57ec7d61fad21c958209d4b2ee40a934d1)

@oli-obk
Copy link
Contributor

oli-obk commented Feb 6, 2025

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-134900 created and queued.
🤖 Automatically detected try build 64628e5
🔍 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-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Feb 6, 2025
@craterbot
Copy link
Collaborator

🚧 Experiment pr-134900 is now running

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

@craterbot
Copy link
Collaborator

🎉 Experiment pr-134900 is completed!
📊 6 regressed and 1 fixed (578841 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

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

oli-obk commented Feb 11, 2025

No one actually relied on this! Great. as errs said, let's ship it once T-lang had a look

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Implementation LGTM, r=me when t-lang are happy.

@davidtwco davidtwco added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 18, 2025
@traviscross
Copy link
Contributor

cc @ehuss

@traviscross
Copy link
Contributor

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Feb 19, 2025

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

Concerns:

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.

@rfcbot 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 Feb 19, 2025
@tmandry
Copy link
Member

tmandry commented Feb 19, 2025

@rfcbot reviewed

@rfcbot 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 Feb 19, 2025
@rfcbot
Copy link

rfcbot commented Feb 19, 2025

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

@traviscross traviscross removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Feb 19, 2025
@nikomatsakis
Copy link
Contributor

@rfcbot reviewed

OK. For future reference I would appreciate a more thorough comment than the one on this PR, which required a lot of context. I read through the source and finally came to understand what is going. Let me write out what I believe is true..

  • In the past, unary operators like ! and - did not accept ..X range expressions. In the code, this comes out from the fact that they invoke parse_expr_prefix which, for reasons I do not know but I presume has to do with precedence, does not accept ranges. This PR changes them to basically accept both prefixes. It should affect the following operators from my read of the code: !, ~, -, *.

I don't think it affects +.

When you look at the reference grammar, it just says that ! and friends are followed by an Expression, which could be a range expression, so this feels like a bug fix from that perspective.

@nikomatsakis
Copy link
Contributor

@rfcbot concern +-operator

Does this PR intentionally not permit +..5 to parse as an expression? From my read of the code, it does not cover this case, but why?

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

nikomatsakis commented Feb 19, 2025

@rfcbot resolve +-operator

Ah, I think I see why now, because we don't treat + as an operator, we just special case the +-literal. I'm gonna resolve my concern for now.

@rfcbot 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 Feb 19, 2025
@rfcbot
Copy link

rfcbot commented Feb 19, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unary operator applied to range fails to parse