D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22011 - traits(getMember, Anything, "this") does not bypass visibility
Summary: traits(getMember, Anything, "this") does not bypass visibility
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-06-10 02:09 UTC by Adam D. Ruppe
Modified: 2021-06-16 07:16 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Adam D. Ruppe 2021-06-10 02:09:12 UTC
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 João Lourenço 2021-06-10 02:23:21 UTC
```d
pragma(msg, __traits(getVisibility, __traits(getMember, A, "__ctor")));
```
Comment 2 Adam D. Ruppe 2021-06-10 02:24:29 UTC
__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 Adam D. Ruppe 2021-06-10 02:26:33 UTC
It also doesn't work if the constructor is auto-generated instead of explicitly put in anyway.
Comment 4 Mathias LANG 2021-06-16 07:16:39 UTC
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"` ?