Jump to page: 1 2
Thread overview
[Issue 18743] ConditionalExpression and AssignExpression should require parentheses
[Issue 18743] Conditional expression (ternary operator) can evaluate the third expression even "If it is true"
Apr 07, 2018
Ketmar Dark
Apr 07, 2018
David Nadlinger
Apr 07, 2018
David Nadlinger
Apr 08, 2018
Patrick Schluter
Apr 08, 2018
David Nadlinger
Apr 08, 2018
Stefan
Apr 30, 2018
Nick Treleaven
May 10, 2018
Nick Treleaven
May 31, 2018
Nick Treleaven
Oct 15, 2018
Nick Treleaven
April 07, 2018
https://issues.dlang.org/show_bug.cgi?id=18743

Ketmar Dark <ketmar@ketmar.no-ip.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ketmar@ketmar.no-ip.org

--
April 07, 2018
https://issues.dlang.org/show_bug.cgi?id=18743

David Nadlinger <code@klickverbot.at> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code@klickverbot.at
           Hardware|x86_64                      |All
            Summary|Conditional expression      |ConditionalExpression and
                   |(ternary operator) can      |AssignExpression should
                   |evaluate the third          |require parentheses
                   |expression even "If it is   |
                   |true"                       |
                 OS|Linux                       |All
           Severity|normal                      |enhancement

--- Comment #1 from David Nadlinger <code@klickverbot.at> ---
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.

--
April 07, 2018
https://issues.dlang.org/show_bug.cgi?id=18743

--- Comment #2 from David Nadlinger <code@klickverbot.at> ---
(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.)

--
April 08, 2018
https://issues.dlang.org/show_bug.cgi?id=18743

Patrick Schluter <Patrick.Schluter@ec.europa.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Patrick.Schluter@ec.europa.
                   |                            |eu

--- Comment #3 from Patrick Schluter <Patrick.Schluter@ec.europa.eu> ---
(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.

--
April 08, 2018
https://issues.dlang.org/show_bug.cgi?id=18743

--- Comment #4 from David Nadlinger <code@klickverbot.at> ---
(In reply to Patrick Schluter from comment #3)
> (stt = "AA") is not a lvalue or is it?

It is, just as in C++.

--
April 08, 2018
https://issues.dlang.org/show_bug.cgi?id=18743

Stefan <kdevel@vogtner.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kdevel@vogtner.de

--- Comment #5 from Stefan <kdevel@vogtner.de> ---
(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.

--
April 30, 2018
https://issues.dlang.org/show_bug.cgi?id=18743

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |bootcamp
                 CC|                            |nick@geany.org

--
May 10, 2018
https://issues.dlang.org/show_bug.cgi?id=18743

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
                URL|                            |https://github.com/dlang/dm
                   |                            |d/pull/8237

--
May 31, 2018
https://issues.dlang.org/show_bug.cgi?id=18743

--- Comment #6 from Nick Treleaven <nick@geany.org> ---
This has also come up in 2016 on D.learn: https://forum.dlang.org/post/nn7o1q$27vc$1@digitalmars.com

--
July 04, 2018
https://issues.dlang.org/show_bug.cgi?id=18743

--- Comment #7 from github-bugzilla@puremagic.com ---
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 <RazvanN7@users.noreply.github.com>

--
« First   ‹ Prev
1 2