[Issue 23349] New: Disallow assignments in ?: expressions

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Sep 20 14:42:46 UTC 2022


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 at puremagic.com
          Reporter: qs.il.paperinik at 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

--


More information about the Digitalmars-d-bugs mailing list