Bug 24781 – alias this on inner structs have wrong behaviour

Status
NEW
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-09-23T16:51:31Z
Last change time
2024-12-13T19:37:32Z
Assigned to
No Owner
Creator
Luís Ferreira
Moved to GitHub: dmd#20517 →

Comments

Comment #0 by contact — 2024-09-23T16:51:31Z
The following code: struct Bar { @property bool isValid() { return true; } } struct Club { @property bool isValid() { return false; } } struct Foo { Bar bar; alias bar this; Foobar foobar; struct Foobar { Club club; alias club this; bool humm() { return isValid(); } } static @property bool isValid() { assert(0); } } void main() { Foo foo; imported!"std.stdio".writeln(foo.foobar.humm); } --- It is expected to print `false` but runs the `assert(0)`. `--preview=fixAliasThis` fixes the behaviour, but this is very very error prone and we found the bug at Weka that is not trivial to find. I strongly suggest either making `--preview=fixAliasThis` the default and add deprecation messages on the cases where its affected by the new behaviour, or error these cases and ask to be explicit. e.g.: this should tell to use either `this.isValid` or `Foo.isValid`.
Comment #1 by razvan.nitu1305 — 2024-09-25T06:43:55Z
I fixed this bug in the past, but I remember that a fair amount of projects were broken by this change because they were relying on the existing behavior, that's why the preview was introduced (which had the effect that people continued using the broken version and relying on it more). Also, in this case it's really hard to issue deprecations since it's essentially silent change of code behavior. So we would either have to make the preview the default and deal with the breakage or continue as is. Of course, there's the dreaded alternative of making the preview the default in a new edition which would be the worse alternative since we will be stuck maintaining both behaviors.
Comment #2 by robert.schadek — 2024-12-13T19:37:32Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20517 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB