[Issue 20025] alias this combined with a copy constructor seems to lead to undefined behaviour.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jul 4 09:28:07 UTC 2019


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

--- Comment #4 from Simen Kjaeraas <simen.kjaras at gmail.com> ---
There seems to be a lot inconsistencies here. This code:

struct S(bool _static, bool _field) {
    static if (_static) static int value = 77;
    else                       int value = 77;
    static if (_field)         int field = 13;
    alias value this;
    this(ref S rhs) { }

    string toString() { return (_static ? "static, " : "non-static, ") ~
(_field ? "field" : "empty"); }
}

void test(T)(int i, ref int j, T t) {
    import std.stdio;
    writeln(t, ":");
    writeln("  by val: ", i);
    writeln("  by ref: ", j);
}

void test(T)(T t) {
    test(t, t, t);
}

unittest {
    test(S!(true, true)());
    test(S!(true, false)());
    test(S!(false, true)());
    test(S!(false, false)());
}

Prints this on my machine (2.087.0):

static, field:
  by val: 77
  by ref: 13
static, empty:
  by val: 7771469
  by ref: 0
non-static, field:
  by val: 77
  by ref: 77
non-static, empty:
  by val: 77
  by ref: 77

And this on run.dlang.io (also 2.087.0):

static, field:
  by val: 2007830860
  by ref: 13
static, empty:
  by val: 2007830868
  by ref: 0
non-static, field:
  by val: 2007830848
  by ref: 77
non-static, empty:
  by val: 2007830860
  by ref: 77

The correct behavior, of course, is what's seen on my machine for both the
non-static cases.

--


More information about the Digitalmars-d-bugs mailing list