Bug 20076 – [2.087.0] structs won't copy when using alias this and being assigned a const instance of said struct

Status
RESOLVED
Resolution
WORKSFORME
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2019-07-23T16:21:50Z
Last change time
2023-04-04T10:40:35Z
Assigned to
No Owner
Creator
Ethan Watson

Attachments

IDFilenameSummaryContent-TypeSize
1756file_20076.txtCode example. The last two lines should be functionally identical to the prior two lines. Currently they error; and as long as alias this resolves they won't give the correct output...text/plain551

Comments

Comment #0 by gooberman — 2019-07-23T16:21:50Z
Created attachment 1756 Code example. The last two lines should be functionally identical to the prior two lines. Currently they error; and as long as alias this resolves they won't give the correct output... Assigning a T to a const( T ) results in a proper struct copy. Assigning a const( T ) to a T results in alias this resolving on const( T ) and tries to construct T with it. This is quite whack, and resolving alias this in that manner breaks a lot of assumptions in my code about how copying structs is meant to work. Haven't been able to work around it with opAssign yet.
Comment #1 by gooberman — 2019-07-23T16:33:13Z
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"
Comment #2 by simen.kjaras — 2019-07-23T20:27:21Z
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.
Comment #3 by razvan.nitu1305 — 2023-04-04T10:40:35Z
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.