Bug 22011 – traits(getMember, Anything, "this") does not bypass visibility
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-06-10T02:09:12Z
Last change time
2021-06-16T07:16:39Z
Assigned to
No Owner
Creator
Adam D. Ruppe
Comments
Comment #0 by destructionator — 2021-06-10T02:09:12Z
Consider the following:
---
module bugtrait;
class A {
private this() {}
private void foo() {}
}
---
module bugtrait2;
import bugtrait;
void main() {
pragma(msg, __traits(getVisibility, __traits(getMember, A, "foo")));
pragma(msg, __traits(getVisibility, __traits(getMember, A, "this")));
}
---
private
Error: no property `this` for type `bugtrait.A`
The "foo" works fine, it bypasses private when getting the member, allowing me to inspect its visibility, deprecation status, etc.
But then for "this", it fails, even though traits(allMembers) returns it, causing an annoying special case.
Comment #1 by jlourenco5691 — 2021-06-10T02:23:21Z
```d
pragma(msg, __traits(getVisibility, __traits(getMember, A, "__ctor")));
```
Comment #2 by destructionator — 2021-06-10T02:24:29Z
__ctor is not returned by allMembers and is not supposed to be used anyway, since the double leading underscore is a reserved detail to the compiler implementation.
Comment #3 by destructionator — 2021-06-10T02:26:33Z
It also doesn't work if the constructor is auto-generated instead of explicitly put in anyway.
Comment #4 by pro.mathias.lang — 2021-06-16T07:16:39Z
The bug is not about visibility, but trying to use `this` to access the ctor.
If you make `this` public, you'll still get the same error message.
> __ctor is not returned by allMembers
It is. https://dlang.org/spec/traits.html#allMembers
We use it directly because there's no alternative.
> But then for "this", it fails, even though traits(allMembers) returns it, causing an annoying special case.
When I add:
> pragma(msg, __traits(allMembers, A));
After the other, I get:
> tuple("__ctor", "foo", "toString", "toHash", "opCmp", "opEquals", "Monitor", "factory")
How do you get the `"this"` ?