Debian Codegraph @

gcc/testsuite/gdc.test/compilable/test7815.d

raw
1 // REQUIRED_ARGS: -o-
2 mixin template Helpers()
3 {
4 static if (is(Flags!Move))
5 {
6 Flags!Move flags;
7 }
8 else
9 {
10 pragma(msg, "X: ", __traits(derivedMembers, Flags!Move));
11 }
12 }
13
14 template Flags(T)
15 {
16 mixin({
17 int defs = 1;
18 foreach (name; __traits(derivedMembers, Move))
19 {
20 defs++;
21 }
22 if (defs)
23 {
24 return "struct Flags { bool x; }";
25 }
26 else
27 {
28 return "";
29 }
30 }());
31 }
32
33 struct Move
34 {
35 int a;
36 mixin Helpers!();
37 }
38
39 enum a7815 = Move.init.flags;
40
41 /+
42 This originally was an invalid case:
43 When the Move struct member is analyzed:
44 1. mixin Helpers!() is instantiated.
45 2. In Helpers!(), static if and its condition is(Flags!Move)) evaluated.
46 3. In Flags!Move, string mixin evaluates and CTFE lambda.
47 4. __traits(derivedMembers, Move) tries to see the member of Move.
48 4a. mixin Helpers!() member is analyzed.
49 4b. `static if (is(Flags!Move))` in Helpers!() is evaluated
50 4c. The Flags!Move instantiation is already in progress, so it cannot be resolved.
51 4d. `static if` fails because Flags!Move cannot be determined as a type.
52 5. __traits(derivedMembers, Move) returns a 1-length tuple("a").
53 6. The lambda in Flags!Move returns a string "struct Flags {...}", then
54 Flags!Move is instantiated to a new struct Flags.
55 7. Finally Move struct does not have flags member, then the `enum a7815`
56 definition will fail in its initializer.
57
58 Now, static if will behave like a string mixin: it is invisible during its own expansion.
59 +/