Thread overview
[Issue 20076] [2.087.0] structs won't copy when using alias this and being assigned a const instance of said struct
Jul 23, 2019
Ethan Watson
Jul 23, 2019
Ethan Watson
Jul 23, 2019
Simen Kjaeraas
Dec 17, 2022
Iain Buclaw
Apr 04, 2023
RazvanN
July 23, 2019
https://issues.dlang.org/show_bug.cgi?id=20076

--- Comment #1 from Ethan Watson <gooberman@gmail.com> ---
More digging makes me think instead that because I'm using class objects, it actually needs a const-removing cast. And since it's not there, it's silently assuming the alias this is the correct thing to use.

So I can deal with it now at least, but that error message is another case of "Confusing D error message"

--
July 23, 2019
https://issues.dlang.org/show_bug.cgi?id=20076

Ethan Watson <gooberman@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|blocker                     |major

--
July 23, 2019
https://issues.dlang.org/show_bug.cgi?id=20076

Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras@gmail.com

--- Comment #2 from Simen Kjaeraas <simen.kjaras@gmail.com> ---
Reduced example:

struct S {
    int* ip;

    this(int* t1) { }

    alias ip this;
}

unittest {
    const S t3;

    // constructor S.this(int* t1) is not callable using argument types
(const(int*))
    S t4 = t3;
}


As you say, it has to do with the use of classes (or in the above example,
pointers): const(T*) simply cannot be implicitly converted to T* without an
explicit cast - and it shouldn't.

In addition, you have a constructor that looks somewhat like the type of the alias this, so DMD tries that when a direct conversion fails.

Removing the alias this in the above code results in the message 'cannot implicitly convert expression t3 of type const(S) to S', which may be more elucidating. It seems to me the correct behavior would be for DMD to print both error messages, probably with a hint that alias this is involved in one of the cases.

In a way, alias this is like an overload, and the error messages should reflect this.

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=20076

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P2

--
April 04, 2023
https://issues.dlang.org/show_bug.cgi?id=20076

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |razvan.nitu1305@gmail.com
         Resolution|---                         |WORKSFORME

--- Comment #3 from RazvanN <razvan.nitu1305@gmail.com> ---
The error message for the reduced test case is now:

test.d(13): Error: constructor `test.S.this(int* t1)` is not callable using
argument types `(const(int*))`
test.d(13):        cannot pass argument `t3.ip` of type `const(int)*` to
parameter `int* t1`

The alias this is not mentioned because once the compiler uses it it just has `t3.ip`, it does not have any information on how the expressions is obtained. I think this is the best we can do with regards to the error message.

So this seems to have been improved. Closing as WORKSFORME.

--