fix: correct unary minus/plus/tilde precedence#1236
Open
jorgsowa wants to merge 3 commits into
Open
Conversation
f9de0f1 to
46bb4a0
Compare
czosel
reviewed
May 12, 2026
| ["+", "-", "."], | ||
| ["*", "/", "%"], | ||
| ["!"], | ||
| ["!", "u-", "u+", "u~"], // u- etc. are unary variants; higher than * so -20*5 parses as (-20)*5 |
Collaborator
There was a problem hiding this comment.
Not sure how relevant in practice, but according to https://www.php.net/manual/en/language.operators.precedence.php the unary operators have even higher precedence than instanceof. Would it make sense to reflect this here?
Also, tangentially, are we lacking ++ and --?
Collaborator
Author
There was a problem hiding this comment.
Good point. I changed the precedence of instanceof. Pre and post increment operators, we can tackle in another branch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unary -, +, and ~ shared the same precedence key as binary - and + (level 15), placing them below * (level 16). This caused -20 * 5 + 10 to be parsed as -(20 * 5 + 10) instead of (-20) * 5 + 10.
Add u-, u+, u~ keys to the precedence table at the ! level (17) and look them up in resolvePrecedence so unary operators correctly bind tighter than multiplicative operators.