Skip to content

Commit

Permalink
Fix Issue 18743 - ConditionalExpression and AssignExpression should r…
Browse files Browse the repository at this point in the history
…equire parens
  • Loading branch information
ntrel authored and wilzbach committed Jun 26, 2018
1 parent e4f19f3 commit 0cfc780
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/dmd/parse.d
Expand Up @@ -8489,6 +8489,11 @@ final class Parser(AST) : Lexer
AST.Expression parseAssignExp()
{
auto e = parseCondExp();
// require parens for e.g. a ? a : a = a;
if (precedence[e.op] == PREC.cond && !e.parens && precedence[token.value] == PREC.assign)
dmd.errors.deprecation(e.loc, "`%s` must be parenthesized when next to operator `%s`",
e.toChars(), Token.toChars(token.value));

const loc = token.loc;
switch (token.value)
{
Expand Down
22 changes: 22 additions & 0 deletions test/fail_compilation/bug18743.d
@@ -0,0 +1,22 @@
// REQUIRED_ARGS: -de
/*
TEST_OUTPUT:
---
fail_compilation/bug18743.d(18): Deprecation: `a ? a = 4 : a` must be parenthesized when next to operator `=`
fail_compilation/bug18743.d(19): Deprecation: `a ? --a : a` must be parenthesized when next to operator `+=`
---
*/

void main()
{
int a;

// ok
(a ? a = 4 : a) = 5;
a ? a = 4 : (a = 5);

a ? a = 4 : a = 5;
a ? --a : a += 1;

a ? a = 4 : a++; // ok
}

0 comments on commit 0cfc780

Please sign in to comment.