Bug 21288 – Wrong context pointer for alias this function

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-10-01T08:33:47Z
Last change time
2023-03-16T01:20:52Z
Keywords
pull
Assigned to
No Owner
Creator
Richard Manthorpe

Comments

Comment #0 by rmanth — 2020-10-01T08:33:47Z
struct A { int p; } struct B { A a() { return A.init; } alias a this; } struct C { void foo() { static assert(B.p.stringof == "p"); // Error: this for s needs to be type B not type C } } void foo() { static assert(B.p.stringof == "p"); // totally fine }
Comment #1 by pro.mathias.lang — 2020-10-01T10:11:06Z
Wait, is this code even valid ? Here in `C.foo` & `foo` you are calling `B.p`, as a shortcut for `B.a.p`, but `a()` is not static, hence you should not be able to do that. Indeed if you change the expression to `B.a.p` it gives you a proper error message. So the second case is an accept-invalid, and the first case could use with a better error message.
Comment #2 by rmanth — 2020-10-01T10:57:40Z
It feels like it should be valid. If there is an `alias this` you should really have to know whether something is in `B` or `A`. Since you can do things like `A.p.stringof` or `__traits(getAttributes, A.p)` which requires a symbol, you should probably also be able to do `B.p.stringof` or `__traits(getAttributes, B.p)`.
Comment #3 by pro.mathias.lang — 2020-10-02T19:38:50Z
Right, because it's `stringof`. Just tested and it also trigggers with `__traits(identifier)`. A workaround for this issue would be to use the following: ``` static assert(__traits(identifier, typeof(B.init.a()).p) == "p"); ``` Which works as expected.
Comment #4 by dlang-bot — 2023-03-08T15:15:23Z
@RazvanN7 created dlang/dmd pull request #14969 "Fix Issue 21288 & 10886 - incorrect this pointer" fixing this issue: - Fix Issue 21288 & 10886 - incorrect this pointer https://github.com/dlang/dmd/pull/14969
Comment #5 by dlang-bot — 2023-03-13T09:17:50Z
dlang/dmd pull request #14969 "Fix Issue 21288 & 10886 - incorrect this pointer" was merged into stable: - 30a91c21347eaaebd8b27bb4598726422110c555 by RazvanN7: Fix Issue 21288 & 10886 - incorrect this pointer https://github.com/dlang/dmd/pull/14969
Comment #6 by dlang-bot — 2023-03-16T01:20:52Z
dlang/dmd pull request #14992 "merge stable" was merged into master: - 141c5683f69d5a483d6f1647950249510a025961 by Razvan Nitu: Fix Issue 21288 & 10886 - incorrect this pointer (#14969) https://github.com/dlang/dmd/pull/14992