Skip to content

Analyser: force parentheses for ambiguous operator combinations #297

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

Closed
wants to merge 1 commit into from

Conversation

chqrlie
Copy link
Contributor

@chqrlie chqrlie commented Jun 21, 2025

  • flag use of combined operators with unintuitive relative precedence as errors
  • fix code like if (buf.st_mode & S_IFMT == S_IFREG) that now requires parentheses: if ((buf.st_mode & S_IFMT) == S_IFREG)
  • update tests

@chqrlie chqrlie changed the title Analyser: force parentheses for ambiguous operator usage Analyser: force parentheses for ambiguous operator combinations Jun 22, 2025
@bvdberg
Copy link
Member

bvdberg commented Jun 22, 2025

Nice..

I dont understand the C_prec is the binary-operator analyser. The c2 only i can understand.

@@ -606,7 +589,8 @@ fn Expr* Parser.parseSizeof(Parser* p) {
while (p.tok.kind != Kind.LSquare) p.consumeToken();
p.error("arrays or subscripts expressions are not allowed inside a sizeof expression");
}
TypeRefHolder ref.init();
TypeRefHolder ref;
ref.init();
Copy link
Member

Choose a reason for hiding this comment

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

why did you change this back to 2 lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unintended, maybe a git rebase glitch, fixed.

@chqrlie
Copy link
Contributor Author

chqrlie commented Jun 22, 2025

I dont understand the C_prec is the binary-operator analyser. The c2 only I can understand.

The C precedence table is used as a reference to check if a combination of operators is ambiguous:

  • if the new c2 operator precedence table and the historical C precedence table give a different relative precedence, the combination is deemed ambiguous and must be explicitly parenthesized.
  • an additional check is added for the relational operators whose combinations are probably unintentional: eg: a < b < c, a == b == c, a > b < c...

@chqrlie chqrlie force-pushed the paren branch 2 times, most recently from a074feb to eda632f Compare June 23, 2025 20:05
* flag use of combined operators with unintuitive relative precedence
  as constraint violations (analyser errors)
* fix code like `if (buf.st_mode & S_IFMT == S_IFREG)` that now requires
  parentheses:  `if ((buf.st_mode & S_IFMT) == S_IFREG)`
* update tests
@bvdberg
Copy link
Member

bvdberg commented Jun 26, 2025

merged...

@bvdberg bvdberg closed this Jun 26, 2025
@chqrlie chqrlie deleted the paren branch June 26, 2025 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants