[Issue 5363] const + alias this = wrong code

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Oct 6 09:08:57 UTC 2018


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

--- Comment #9 from Walter Bright <bugzilla at digitalmars.com> ---
I simplified it a bit more, so it happens at compile time:

  struct S {
    int dummy;
    alias dummy this;
  }
  int foo(int){ return 1; }
  int foo(const(S)){ return 2; }

  void test() {
    static assert(foo(cast(const)S.init) == 2); // pass
    static assert(foo(S.init) == 2); // fail
  }

Here's the trouble. With alias this, the conversion of S to int is considered
an 'exact' match, while the conversion of S to const is considered a 'constant'
match, which is not as good as 'exact'. Hence, the 'exact' is selected and
foo(int) is called.

The levels of conversion are:

    nomatch
    convert
    constant
    exact

We could change an alias this conversion to a 'convert' match, but it is
unknown how much breakage this will cause. It seems to be a rather drastic
change.

The compiler source code is the TypeStruct.implicitConvTo() function in
mtype.d.

--


More information about the Digitalmars-d-bugs mailing list