Strange behaviour of __traits(allMembers)

IchorDev zxinsworld at gmail.com
Sun Jun 18 09:48:40 UTC 2023


`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?


More information about the Digitalmars-d-learn mailing list