[Issue 18317] New: Binop quirks in AST (wrt. typeof(null) and AA) incl. safety hole

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jan 27 18:19:06 UTC 2018


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

          Issue ID: 18317
           Summary: Binop quirks in AST (wrt. typeof(null) and AA) incl.
                    safety hole
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: kinke at gmx.net

I've just learned that the following is apparently valid code (in the context
of analyzing a reported LDC issue,
https://github.com/ldc-developers/ldc/issues/2537):

void main() @safe
{
    typeof(null) nul;
    void* ptr;
    int[string] aa = [ "one": 123 ];

    static assert(!__traits(compiles, ptr + ptr));
    auto sum = nul + nul;
    auto diff = nul - nul;

    static assert(!__traits(compiles, aa + aa));
    static assert(!__traits(compiles, aa + ptr));
    static assert(!__traits(compiles, ptr + aa));
    assert(aa + nul == aa);
    assert(nul + aa == aa);

    static assert(!__traits(compiles, aa - aa));
    static assert(!__traits(compiles, aa - ptr));
    static assert(!__traits(compiles, ptr - aa));
    assert(aa - nul == aa);
    assert(nul - aa == aa);
}

Firstly, these weird binop expressions make it into the AST, so the backend
needs to handle stuff like that.
If `nul + nul` etc. is really supposed to be valid, the front-end could rewrite
the binop as CommaExp `lhs, rhs, typeof(null).init`.
And while `aa + aa` is invalid, `aa + nul` makes it into the AST as `aa +
cast(int[string]) nul`, so that the backend needs to handle additions and
subtractions of associative arrays (semantics?! :D).

Secondly, the last assert leads to a segfault in this @safe code during AA
comparison, as `nul - aa` yields (a pointer to) an invalid AA, so DMD appears
to have chosen pointer subtraction as semantics for AA subtraction.

--


More information about the Digitalmars-d-bugs mailing list