[Issue 15288] The precedence of the exponentiation operator ^^ is too high.

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Nov 5 10:03:22 PST 2015


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

thomas.bockman at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|The exponentiation operator |The precedence of the
                   |^^ doesn't follow the       |exponentiation operator ^^
                   |standard type promotion     |is too high.
                   |rules.                      |

--- Comment #1 from thomas.bockman at gmail.com ---
So, I just realized what's actually going on here: the exponentiation operator
*is* using the correct promotion rules, but, bizarrely, has higher precedence
than the cast operator:

I expect this:
    cast(N)1 ^^ cast(M)1
To be evaluated like this:
    (cast(N)1) ^^ (cast(M)1)
But it is instead evaluated like this:
    cast(N)(1 ^^ cast(M)1)

I'm not sure if anyone else actually uses this operator with integers, but if
so I'd bet there are some bugs in the wild because of this.

Looking at the operator precedence table on the dlang wiki:
    http://wiki.dlang.org/Operator_precedence
I would say that groups 12 and 13 should be swapped. Otherwise you get truly
weird stuff like:

int[2] xs = [ 1, 2 ];
int* y = &(xs[0]);
int z = *++y ^^ 7; // ERROR: incompatible types for ((y) ^^ (7)): 'int*' and
'int'

Because this:
    *++y ^^ 7
Is interpreted as this:
    *++(y ^^ 7)
Rather than what I would expect:
    (*++y) ^^ 7

Not only is this unexpected, it is also silly:
    * Exponentiation is not even defined for pointer types
    * The result of exponentiation cannot be incremented: it is an rvalue.
    * The dereference operator is not defined for integer types

--


More information about the Digitalmars-d-bugs mailing list