www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23349] New: Disallow assignments in ?: expressions

https://issues.dlang.org/show_bug.cgi?id=23349

          Issue ID: 23349
           Summary: Disallow assignments in ?: expressions
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: qs.il.paperinik gmail.com

Currently, conditional expressions (the formal name for the tenery operator
`?:`) allow for an assignment in its then-part (between question mark and
colon), but not its else-part (after colon). There’s even an example in the
spec[1]:
    test ? a = b : (c = 2);
This is something compilers would warn about, and here, D’s no-warning policy
is correct: Make it an error and require parentheses:
    test ? (a = b) : (c = 2);

In the grammar, it is a simple change:
From 
ConditionalExpression: OrOrExpression OrOrExpression ? Expression : ConditionalExpression to ConditionalExpression: OrOrExpression OrOrExpression ? ConditionalExpression : ConditionalExpression Note: The only Expressions below ConditionalExpression are CommaExpression and AssignExpression. [1]: https://dlang.org/spec/expression.html#conditional_expressions --
Sep 20 2022