forked from chapel-lang/chapel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the error message for non-associative comparisons. (chapel-la…
…ng#24564) This PR builds on chapel-lang#24155. Closes chapel-lang#24559. In chapel-lang#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 chapel-lang#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
- Loading branch information
Showing
13 changed files
with
1,136 additions
and
860 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
compareAssoc.chpl:6: error: comparison operators are not associative | ||
compareAssoc.chpl:10: error: comparison operators are not associative | ||
compareAssoc.chpl:14: error: comparison operators are not associative | ||
compareAssoc.chpl:18: error: comparison operators are not associative | ||
compareAssoc.chpl:22: error: comparison operators are not associative | ||
compareAssoc.chpl:26: error: comparison operators are not associative | ||
compareAssoc.chpl:27: error: comparison operators are not associative |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
─── error in compareAssoc.chpl:6 [NonAssociativeComparison] ─── | ||
Comparison operators are not associative. | ||
| | ||
6 | var x = a < b < c; | ||
| | ||
Comparisons in the form 'x op y op z' are not supported. | ||
If you wanted to perform elementwise comparison, consider using the following instead: | ||
|
||
a < b && b < c | ||
|
||
If you wanted to use the result of a comparison as an operand in another comparison, consider using parentheses in a subexpression to disambiguate: | ||
| | ||
6 | var x = a < b < c; | ||
| ⎺⎺⎺⎺⎺ | ||
| | ||
|
||
─── error in compareAssoc.chpl:10 [NonAssociativeComparison] ─── | ||
Comparison operators are not associative. | ||
| | ||
10 | var x1 = a < b <= c; | ||
| | ||
Comparisons in the form 'x op y op z' are not supported. | ||
If you wanted to perform elementwise comparison, consider using the following instead: | ||
|
||
a < b && b <= c | ||
|
||
If you wanted to use the result of a comparison as an operand in another comparison, consider using parentheses in a subexpression to disambiguate: | ||
| | ||
10 | var x1 = a < b <= c; | ||
| ⎺⎺⎺⎺⎺ | ||
| | ||
|
||
─── error in compareAssoc.chpl:14 [NonAssociativeComparison] ─── | ||
Comparison operators are not associative. | ||
| | ||
14 | var x2 = a > b <= c; | ||
| | ||
Comparisons in the form 'x op y op z' are not supported. | ||
If you wanted to perform elementwise comaprison, please use '&&' to combine operations. | ||
If you wanted to use the result of a comparison as an operand in another comparison, consider using parentheses in a subexpression to disambiguate: | ||
| | ||
14 | var x2 = a > b <= c; | ||
| ⎺⎺⎺⎺⎺ | ||
| | ||
|
||
─── error in compareAssoc.chpl:18 [NonAssociativeComparison] ─── | ||
Comparison operators are not associative. | ||
| | ||
18 | var x3 = a == b == c; | ||
| | ||
Comparisons in the form 'x op y op z' are not supported. | ||
If you wanted to perform elementwise comaprison, please use '&&' to combine operations. | ||
If you wanted to use the result of a comparison as an operand in another comparison, consider using parentheses in a subexpression to disambiguate: | ||
| | ||
18 | var x3 = a == b == c; | ||
| ⎺⎺⎺⎺⎺⎺ | ||
| | ||
|
||
─── error in compareAssoc.chpl:22 [NonAssociativeComparison] ─── | ||
Comparison operators are not associative. | ||
| | ||
22 | var x4 = a != b != c; | ||
| | ||
Comparisons in the form 'x op y op z' are not supported. | ||
If you wanted to perform elementwise comaprison, please use '&&' to combine operations. | ||
If you wanted to use the result of a comparison as an operand in another comparison, consider using parentheses in a subexpression to disambiguate: | ||
| | ||
22 | var x4 = a != b != c; | ||
| ⎺⎺⎺⎺⎺⎺ | ||
| | ||
|
||
─── error in compareAssoc.chpl:26 [NonAssociativeComparison] ─── | ||
Comparison operators are not associative. | ||
| | ||
26 | var x = a < b < c < d; | ||
| | ||
Comparisons in the form 'x op y op z' are not supported. | ||
If you wanted to perform elementwise comparison, consider using the following instead: | ||
|
||
a < b && b < c && c < d | ||
|
||
If you wanted to use the result of a comparison as an operand in another comparison, consider using parentheses in a subexpression to disambiguate: | ||
| | ||
26 | var x = a < b < c < d; | ||
| ⎺⎺⎺⎺⎺ | ||
| | ||
|
||
─── error in compareAssoc.chpl:27 [NonAssociativeComparison] ─── | ||
Comparison operators are not associative. | ||
| | ||
27 | var x1 = a < b <= c <= d; | ||
| | ||
Comparisons in the form 'x op y op z' are not supported. | ||
If you wanted to perform elementwise comparison, consider using the following instead: | ||
|
||
a < b && b <= c && c <= d | ||
|
||
If you wanted to use the result of a comparison as an operand in another comparison, consider using parentheses in a subexpression to disambiguate: | ||
| | ||
27 | var x1 = a < b <= c <= d; | ||
| ⎺⎺⎺⎺⎺ | ||
| | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var a = 0; | ||
var b = 0; | ||
var c = 0; | ||
var d = 0; | ||
|
||
var x = a < b < c; | ||
var y = (a < b) < c; | ||
var z = a < (b < c); | ||
|
||
var x1 = a < b <= c; | ||
var y1 = (a < b) < c; | ||
var z1 = a < (b < c); | ||
|
||
var x2 = a > b <= c; | ||
var y2 = (a > b) < c; | ||
var z2 = a < (b > c); | ||
|
||
var x3 = a == b == c; | ||
var y3 = (a == b) == c; | ||
var z3 = a == (b == c); | ||
|
||
var x4 = a != b != c; | ||
var y4 = (a != b) != c; | ||
var z4 = a != (b != c); | ||
|
||
var x = a < b < c < d; | ||
var x1 = a < b <= c <= d; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.