Make equality, comparison and 'in' operators not associative.
I'm not using a --incompatible-change flag because it's not available in the
parser. We could pass it, but I think this is trivial to fix and unlikely to
happen in real code (if it does, there was most likely a bug).
RELNOTES[INC]:
Operators for equality, comparison, 'in' and 'not in' are no longer associative,
e.g. x < y < z is now a syntax error. Before, it was parsed as: (x < y) < z.
PiperOrigin-RevId: 159422042
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java b/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
index b5eedb2..20a7084 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java
@@ -120,6 +120,35 @@
}
@Test
+ public void testNonAssociativeOperators() throws Exception {
+ setFailFast(false);
+
+ parseExpression("0 < 2 < 4");
+ assertContainsError("Operator '<' is not associative with operator '<'");
+ clearEvents();
+
+ parseExpression("0 == 2 < 4");
+ assertContainsError("Operator '==' is not associative with operator '<'");
+ clearEvents();
+
+ parseExpression("1 in [1, 2] == True");
+ assertContainsError("Operator 'in' is not associative with operator '=='");
+ clearEvents();
+
+ parseExpression("1 >= 2 <= 3");
+ assertContainsError("Operator '>=' is not associative with operator '<='");
+ clearEvents();
+ }
+
+ @Test
+ public void testNonAssociativeOperatorsWithParens() throws Exception {
+ parseExpression("(0 < 2) < 4");
+ parseExpression("(0 == 2) < 4");
+ parseExpression("(1 in [1, 2]) == True");
+ parseExpression("1 >= (2 <= 3)");
+ }
+
+ @Test
public void testUnaryMinusExpr() throws Exception {
FuncallExpression e = (FuncallExpression) parseExpression("-5");
FuncallExpression e2 = (FuncallExpression) parseExpression("- 5");