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

Make comparison operators non-associative #24155

Merged
merged 11 commits into from
Jan 17, 2024

Conversation

DanilaFe
Copy link
Contributor

@DanilaFe DanilaFe commented Jan 5, 2024

This is a potential solution to #24152. In particular, we accept the following program:

config const n = 3;
proc main() {
  if 0 <= n <= 2 {
    writeln("0 <= n <= 2, n=", n);
  }
}

But this relies on the fact that bool can be interpreted numerically, and used in a comparison. It seems like the syntax 0 <= n <= 2 should not be allowed given a binary operation <=, because it's not associative. Our parser marks it %left, which seems wrong; this PR adjusts the comparison operators to be %noassoc.

Reviewed by @mppf and agreed upon synchronously with the rest of the team.

Testing

  • dyno testing
  • paratest (including chpldoc tests)

@DanilaFe DanilaFe changed the title Comparison noassoc Make comparison operators non-associative Jan 5, 2024
@@ -456,7 +456,7 @@
%left TOR
%left TAND
%left TEQUAL TNOTEQUAL
%left TLESSEQUAL TGREATEREQUAL TLESS TGREATER
%nonassoc TLESSEQUAL TGREATEREQUAL TLESS TGREATER
Copy link
Member

Choose a reason for hiding this comment

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

What about != and ==? I would think that they are in the same category.

I really would not like to parse these myself, which I interpret
as additional rationale for the change.

Signed-off-by: Danila Fedorin <[email protected]>
@DanilaFe DanilaFe merged commit 64b1d24 into chapel-lang:main Jan 17, 2024
7 checks passed
DanilaFe added a commit that referenced this pull request Mar 8, 2024
This PR builds on #24155.
Closes #24559.

In #24155, we made `a < b < c`
a syntax error, because it leads to confusing results: a boolean is
implicitly converted to a numeric value, and compared against another
number. I went about disallowing such syntax by simply removing the
associativity from `<`, `>`, etc. However, this meant that Bison simply
reported "syntax error" when it encountered such an expression, which
leaves much to be desired.

This PR improves on that situation by making adjustments to the parser.
Specifically, it enables the parser to note whether or not parentheses
were used for an expression (including an operator comparison). This is
done using the additional location maps added by @dlongnecke-cray in
#23548. The parser is thus
allowed to accept `a < b < c`, and subsequent checks are executed that
detect `a < b < c` (as opposed to `(a < b) < c`, which is valid). These
checks use the Dyno error system to print a nice error message.

<img width="1153" alt="Screen Shot 2024-03-07 at 3 45 34 PM"
src="https://github.com/chapel-lang/chapel/assets/4361282/02ac537a-1b51-4134-b800-1c6986ed9060">

Reviewed by @dlongnecke-cray -- thanks!

## Testing
- [x] paratest
- [x] dyno tests
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