Bug 18743 – ConditionalExpression and AssignExpression should require parentheses

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-04-07T15:25:02Z
Last change time
2018-10-15T09:06:50Z
Keywords
bootcamp, pull
Assigned to
No Owner
Creator
Ali Cehreli

Comments

Comment #0 by acehreli — 2018-04-07T15:25:02Z
string stt = "none"; true?writeln("AA"):writeln("BB"); ///Out:AA true?stt="AA":stt="BB"; <<<<-----///Out:BB writeln(stt); Copied from the forum discussion: https://forum.dlang.org/post/[email protected] The behavior does not match the spec: https://dlang.org/spec/expression.html#conditional_expressions Ali
Comment #1 by code — 2018-04-07T21:49:59Z
This isn't a bug. In D, ?: has higher precedence than =, so true ? stt = "AA" : stt = "BB" means (true ? (stt = "AA") : stt) = "BB", in line with C and other languages, but notably not C++. The operator precedence should be documented somewhere, though; while there is a table in TDPL (p. 61 ff.) and on the Wiki (https://wiki.dlang.org/Operator_precedence), I can't seem to find it on dlang.org/spec. --- Changing this to an enhancement request, as we should really consider requiring explicit parenthesization for this sort of error-prone code, similar to how we disallow assignment expressions in if statements.
Comment #2 by code — 2018-04-07T21:52:58Z
(Note that this _is_ part of the specification in form of the grammar. Assignment is defined as AssignExpression: ConditionalExpression ConditionalExpression = AssignExpression ConditionalExpression += AssignExpression ConditionalExpression -= AssignExpression ConditionalExpression *= AssignExpression ConditionalExpression /= AssignExpression ConditionalExpression %= AssignExpression ConditionalExpression &= AssignExpression ConditionalExpression |= AssignExpression ConditionalExpression ^= AssignExpression ConditionalExpression ~= AssignExpression ConditionalExpression <<= AssignExpression ConditionalExpression >>= AssignExpression ConditionalExpression >>>= AssignExpression ConditionalExpression ^^= AssignExpression which unambiguously specifies the precedence.)
Comment #3 by Patrick.Schluter — 2018-04-08T16:57:16Z
(In reply to David Nadlinger from comment #1) > This isn't a bug. > > In D, ?: has higher precedence than =, so > > true ? stt = "AA" : stt = "BB" > > means > > (true ? (stt = "AA") : stt) = "BB", > > in line with C and other languages, but notably not C++. > > The operator precedence should be documented somewhere, though; while there > is a table in TDPL (p. 61 ff.) and on the Wiki > (https://wiki.dlang.org/Operator_precedence), I can't seem to find it on > dlang.org/spec. > > --- > > Changing this to an enhancement request, as we should really consider > requiring explicit parenthesization for this sort of error-prone code, > similar to how we disallow assignment expressions in if statements. How on earth can (true ? (stt = "AA") : stt) = "BB" compile? (stt = "AA") is not a lvalue or is it? It looks like D compiles int a, b; (a=1)=2; without problem. That's what surprizing here (at least for a C guy), not the precedence of the ternary.
Comment #4 by code — 2018-04-08T17:00:57Z
(In reply to Patrick Schluter from comment #3) > (stt = "AA") is not a lvalue or is it? It is, just as in C++.
Comment #5 by kdevel — 2018-04-08T20:04:52Z
(In reply to David Nadlinger from comment #1) > (true ? (stt = "AA") : stt) = "BB", > > in line with C and other languages, but notably not C++. The value of the ternary conditional operator of C is not an l-value. Hence the compilations fails (for a conforming compiler). JavaScript/ECMAScript follows C++. C# does probably not allow a free ternary conditional. The only language I found which behaves like D is perl.
Comment #6 by nick — 2018-05-31T11:45:11Z
This has also come up in 2016 on D.learn: https://forum.dlang.org/post/[email protected]
Comment #7 by github-bugzilla — 2018-07-04T13:30:41Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/0cfc780539838d8c6294174a0587fd5b5c33de94 Fix Issue 18743 - ConditionalExpression and AssignExpression should require parens https://github.com/dlang/dmd/commit/0ed6065c6565e69aa9291887b89daf568db230de Merge pull request #8237 from ntrel/cond-parens Fix Issue 18743 - ConditionalExpression with AssignExpression should require parens merged-on-behalf-of: Razvan Nitu <[email protected]>
Comment #8 by github-bugzilla — 2018-08-14T11:44:34Z
Commits pushed to master at https://github.com/dlang/dlang.org https://github.com/dlang/dlang.org/commit/fe4dd55a8a1f486d4db6f93b5fcc72a009367f36 Issue 18743 - ConditionalExpression with AssignExpression requires parens https://github.com/dlang/dlang.org/commit/18f70c41be542222488a1e2460ef0376d6fada21 Merge pull request #2366 from ntrel/cond-assign Issue 18743 - ConditionalExpression with AssignExpression requires parens
Comment #9 by nick — 2018-10-15T09:06:50Z
*** Issue 14186 has been marked as a duplicate of this issue. ***