Module Name: src Committed By: rillig Date: Mon Jul 26 18:06:43 UTC 2021
Modified Files: src/tests/usr.bin/xlint/lint1: expr_precedence.c expr_precedence.exp Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/expr_precedence.c \ src/tests/usr.bin/xlint/lint1/expr_precedence.exp Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/xlint/lint1/expr_precedence.c diff -u src/tests/usr.bin/xlint/lint1/expr_precedence.c:1.5 src/tests/usr.bin/xlint/lint1/expr_precedence.c:1.6 --- src/tests/usr.bin/xlint/lint1/expr_precedence.c:1.5 Mon Jul 26 17:27:22 2021 +++ src/tests/usr.bin/xlint/lint1/expr_precedence.c Mon Jul 26 18:06:43 2021 @@ -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 @@ assignment_associativity(int arg) 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; +} Index: src/tests/usr.bin/xlint/lint1/expr_precedence.exp diff -u src/tests/usr.bin/xlint/lint1/expr_precedence.exp:1.5 src/tests/usr.bin/xlint/lint1/expr_precedence.exp:1.6 --- src/tests/usr.bin/xlint/lint1/expr_precedence.exp:1.5 Mon Jul 26 17:27:22 2021 +++ src/tests/usr.bin/xlint/lint1/expr_precedence.exp Mon Jul 26 18:06:43 2021 @@ -2,3 +2,4 @@ expr_precedence.c(15): error: syntax err 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]