Spec: can a class method change the type of the object (vtable) ?

kinke noone at nowhere.com
Mon Mar 18 13:50:59 UTC 2024


On Monday, 18 March 2024 at 12:44:46 UTC, Nick Treleaven wrote:
> On Sunday, 17 March 2024 at 12:47:49 UTC, Johan wrote:
>> Although accepted by the compiler currently, I believe this is 
>> forbidden by the spec (can't find it, if it is not in the spec 
>> it should be added):
>> ```
>>     void foo() {
>>         this = new B(); // illegal D code
>>     }
>> ```
>
> With dmd v2.108.0-rc.1 I get:
> ```
> thislval.d(7): Error: cannot modify expression `this` because 
> it is not an lvalue
> ```
> `this` was changed to be an lvalue for a few releases recently, 
> see:
> https://issues.dlang.org/show_bug.cgi?id=24157

Yeah, IIRC, this was only allowed in v2.105.0 and v2.105.1, then 
reverted in v2.105.2. But it never changed the vptr, it just made 
`this` point to another object after the assignment (not 
affecting the callER though).

I think the assumption/optimization should be safe. The only 
exception might be `extern(C++)` ctors in the future, if we 
really made those C++-compatible - they set the vptr (base ctors 
first to their vptr, then derived ctors overriding it later).


More information about the Digitalmars-d mailing list