www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Strange behaviour of __traits(allMembers)

reply IchorDev <zxinsworld gmail.com> writes:
`source/mod/submod.d:`
```d
module mod.submod;
enum T1{ x }
```

`source/mod/package.d`:
```d
module mod;
import mod.submod;

enum T2{ y }
enum T3{ z }

static foreach(member; __traits(allMembers, mod)){
   pragma(msg, member);
}
/**Prints:
object
T1
**/
```
I get the members of `mod.submod` instead of `mod`, even though I 
supplied it with `mod`. The same happens if I use 
`mixin(__MODULE__)`.
I've tested this using `dub` with the stock dub.json file (no 
dependencies) with
- dmd 2.101.1
- dmd 2.104.0
- ldc2 1.30.0
- ldc2 1.32.2
- gdc 12.2.0

and I've gotten the same results for all of my tests every time.

If I change `mod.submod` to being a different module instead of a 
submodule, `allMembers` lists the contents of `mod` as expected. 
The members within the two modules don't seem to affect anything.

Importing `mod` from `mod.submod` makes it show up in 
`allMembers`, meaning I can do `__traits(allMembers, 
mod.mod.mod.mod.mod.` (etc.) recursively, and it still prints the 
members of `mod.submod`. Putting `mod.submod` into `allMembers` 
causes this error:
```
Error: undefined identifier `submod` in package `mod`
```


Does anyone understand why this happens?
Is there any way to subvert this behaviour, or is it actually a 
bug?
Jun 18 2023
parent reply FeepingCreature <feepingcreature gmail.com> writes:
On Sunday, 18 June 2023 at 09:48:40 UTC, IchorDev wrote:
 Does anyone understand why this happens?
 Is there any way to subvert this behaviour, or is it actually a 
 bug?
Yes, see also my bug report, https://issues.dlang.org/show_bug.cgi?id=20008 "__traits(allMembers) of packages is complete nonsense".
Jun 18 2023
parent reply IchorDev <zxinsworld gmail.com> writes:
On Sunday, 18 June 2023 at 10:04:14 UTC, FeepingCreature wrote:
 On Sunday, 18 June 2023 at 09:48:40 UTC, IchorDev wrote:
 Does anyone understand why this happens?
 Is there any way to subvert this behaviour, or is it actually 
 a bug?
Yes, see also my bug report, https://issues.dlang.org/show_bug.cgi?id=20008 "__traits(allMembers) of packages is complete nonsense".
Whaaaaaaaaaaaaaaaaaaaaaat why has this not been fixed in the last 4 years!
Jun 18 2023
next sibling parent FeepingCreature <feepingcreature gmail.com> writes:
On Sunday, 18 June 2023 at 10:21:16 UTC, IchorDev wrote:
 On Sunday, 18 June 2023 at 10:04:14 UTC, FeepingCreature wrote:
 On Sunday, 18 June 2023 at 09:48:40 UTC, IchorDev wrote:
 Does anyone understand why this happens?
 Is there any way to subvert this behaviour, or is it actually 
 a bug?
Yes, see also my bug report, https://issues.dlang.org/show_bug.cgi?id=20008 "__traits(allMembers) of packages is complete nonsense".
Whaaaaaaaaaaaaaaaaaaaaaat why has this not been fixed in the last 4 years!
I think because nobody *needs* `__traits(allMembers)` of packages. Mostly people just learn to skip them while scanning modules, and instead of iterating imports, they do the standard hack of "generate a list of all files in the project, string import it, and generate import statements for each". Yeah it's ugly. I guess the lesson is, nothing takes as long to fix as a bug with a well-known workaround.
Jun 18 2023
prev sibling parent reply Dennis <dkorpel gmail.com> writes:
On Sunday, 18 June 2023 at 10:21:16 UTC, IchorDev wrote:
 Whaaaaaaaaaaaaaaaaaaaaaat why has this not been fixed in the 
 last 4 years!
It's now fixed: https://github.com/dlang/dmd/pull/15335
Jun 28 2023
parent IchorDev <zxinsworld gmail.com> writes:
On Wednesday, 28 June 2023 at 10:20:44 UTC, Dennis wrote:
 It's now fixed: https://github.com/dlang/dmd/pull/15335
Yesss! You're a hero indeed!
Jul 05 2023