Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/usr.bin/xlint/lint1 tests/lint: show bug in conditiona...



details:   https://anonhg.NetBSD.org/src/rev/c8d2481610b5
branches:  trunk
changeset: 1022597:c8d2481610b5
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Jul 26 18:06:43 2021 +0000

description:
tests/lint: show bug in conditional expression (since 2021-07-15)

Since cgram.y 1.325 from 2021-07-15, conditional expressions did not
accept a comma-expression in the then-branch anymore.  In practice, this
is an edge case though since comma expressions are rare.

diffstat:

 tests/usr.bin/xlint/lint1/expr_precedence.c   |  26 +++++++++++++++++++++++++-
 tests/usr.bin/xlint/lint1/expr_precedence.exp |   1 +
 2 files changed, 26 insertions(+), 1 deletions(-)

diffs (45 lines):

diff -r 671a3af9bc10 -r c8d2481610b5 tests/usr.bin/xlint/lint1/expr_precedence.c
--- a/tests/usr.bin/xlint/lint1/expr_precedence.c       Mon Jul 26 17:27:22 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_precedence.c       Mon Jul 26 18:06:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: expr_precedence.c,v 1.5 2021/07/26 17:27:22 rillig Exp $       */
+/*     $NetBSD: expr_precedence.c,v 1.6 2021/07/26 18:06:43 rillig Exp $       */
 # 3 "expr_precedence.c"
 
 /*
@@ -51,3 +51,27 @@
 
        left = arg;
 }
+
+void
+conditional_associativity(_Bool cond1, _Bool cond2, int a, int b, int c)
+{
+       /* The then-expression can be an arbitrary expression. */
+       var = cond1 ? cond2 ? a : b : c;
+       var = cond1 ? (cond2 ? a : b) : c;
+
+       /* The then-expression can even be a comma-expression. */
+       /* expect+1: error: syntax error ',' [249] *//* FIXME */
+       var = cond1 ? cond2 ? a, b : (b, a) : c;
+
+       var = cond1 ? a : cond2 ? b : c;
+       /*
+        * In almost all programming languages, '?:' is right-associative,
+        * which allows for easy chaining.
+        */
+       var = cond1 ? a : (cond2 ? b : c);
+       /*
+        * In PHP, '?:' is left-associative, which is rather surprising and
+        * requires more parentheses to get the desired effect.
+        */
+       var = (cond1 ? a : cond2) ? b : c;
+}
diff -r 671a3af9bc10 -r c8d2481610b5 tests/usr.bin/xlint/lint1/expr_precedence.exp
--- a/tests/usr.bin/xlint/lint1/expr_precedence.exp     Mon Jul 26 17:27:22 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_precedence.exp     Mon Jul 26 18:06:43 2021 +0000
@@ -2,3 +2,4 @@
 expr_precedence.c(18): error: non-constant initializer [177]
 expr_precedence.c(35): error: 'var' undefined [99]
 expr_precedence.c(35): error: syntax error '=' [249]
+expr_precedence.c(64): error: syntax error ',' [249]



Home | Main Index | Thread Index | Old Index