Debian Codegraph @

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

raw
1
2 module traits_getFunctionAttributes;
3
4 alias tuple(T...) = T;
5
6 void test_getFunctionAttributes()
7 {
8
9 struct S
10 {
11 int noF() { return 0; }
12 int constF() const { return 0; }
13 int immutableF() immutable { return 0; }
14 int inoutF() inout { return 0; }
15 int sharedF() shared { return 0; }
16
17 int x;
18 ref int refF() return { return x; }
19 int propertyF() @property { return 0; }
20 int nothrowF() nothrow { return 0; }
21 int nogcF() @nogc { return 0; }
22
23 int systemF() @system { return 0; }
24 int trustedF() @trusted { return 0; }
25 int safeF() @safe { return 0; }
26
27 int pureF() pure { return 0; }
28
29 int liveF() @live { return 0; }
30 }
31
32 static assert(__traits(getFunctionAttributes, S.noF) == tuple!("@system"));
33 static assert(__traits(getFunctionAttributes, typeof(S.noF)) == tuple!("@system"));
34
35 static assert(__traits(getFunctionAttributes, S.constF) == tuple!("const", "@system"));
36 static assert(__traits(getFunctionAttributes, typeof(S.constF)) == tuple!("const", "@system"));
37
38 static assert(__traits(getFunctionAttributes, S.immutableF) == tuple!("immutable", "@system"));
39 static assert(__traits(getFunctionAttributes, typeof(S.immutableF)) == tuple!("immutable", "@system"));
40
41 static assert(__traits(getFunctionAttributes, S.inoutF) == tuple!("inout", "@system"));
42 static assert(__traits(getFunctionAttributes, typeof(S.inoutF)) == tuple!("inout", "@system"));
43
44 static assert(__traits(getFunctionAttributes, S.sharedF) == tuple!("shared", "@system"));
45 static assert(__traits(getFunctionAttributes, typeof(S.sharedF)) == tuple!("shared", "@system"));
46
47 static assert(__traits(getFunctionAttributes, S.refF) == tuple!("ref", "return", "@system"));
48 static assert(__traits(getFunctionAttributes, typeof(S.refF)) == tuple!("ref", "return", "@system"));
49
50 static assert(__traits(getFunctionAttributes, S.propertyF) == tuple!("@property", "@system"));
51 static assert(__traits(getFunctionAttributes, typeof(&S.propertyF)) == tuple!("@property", "@system"));
52
53 static assert(__traits(getFunctionAttributes, S.nothrowF) == tuple!("nothrow", "@system"));
54 static assert(__traits(getFunctionAttributes, typeof(S.nothrowF)) == tuple!("nothrow", "@system"));
55
56 static assert(__traits(getFunctionAttributes, S.nogcF) == tuple!("@nogc", "@system"));
57 static assert(__traits(getFunctionAttributes, typeof(S.nogcF)) == tuple!("@nogc", "@system"));
58
59 static assert(__traits(getFunctionAttributes, S.systemF) == tuple!("@system"));
60 static assert(__traits(getFunctionAttributes, typeof(S.systemF)) == tuple!("@system"));
61
62 static assert(__traits(getFunctionAttributes, S.trustedF) == tuple!("@trusted"));
63 static assert(__traits(getFunctionAttributes, typeof(S.trustedF)) == tuple!("@trusted"));
64
65 static assert(__traits(getFunctionAttributes, S.safeF) == tuple!("@safe"));
66 static assert(__traits(getFunctionAttributes, typeof(S.safeF)) == tuple!("@safe"));
67
68 static assert(__traits(getFunctionAttributes, S.pureF) == tuple!("pure", "@system"));
69 static assert(__traits(getFunctionAttributes, typeof(S.pureF)) == tuple!("pure", "@system"));
70
71 static assert(__traits(getFunctionAttributes, S.liveF) == tuple!("@live", "@system"));
72 static assert(__traits(getFunctionAttributes, typeof(S.liveF)) == tuple!("@live", "@system"));
73
74 int pure_nothrow() nothrow pure { return 0; }
75 static ref int static_ref_property() @property { return *(new int); }
76 ref int ref_property() @property { return *(new int); }
77 void safe_nothrow() @safe nothrow { }
78 void live_nothrow() nothrow @live { }
79
80 static assert(__traits(getFunctionAttributes, pure_nothrow) == tuple!("pure", "nothrow", "@nogc", "@safe"));
81 static assert(__traits(getFunctionAttributes, typeof(pure_nothrow)) == tuple!("pure", "nothrow", "@nogc", "@safe"));
82
83 static assert(__traits(getFunctionAttributes, static_ref_property) == tuple!("pure", "nothrow", "@property", "ref", "@safe"));
84 static assert(__traits(getFunctionAttributes, typeof(&static_ref_property)) == tuple!("pure", "nothrow", "@property", "ref", "@safe"));
85
86 static assert(__traits(getFunctionAttributes, ref_property) == tuple!("pure", "nothrow", "@property", "ref", "@safe"));
87 static assert(__traits(getFunctionAttributes, typeof(&ref_property)) == tuple!("pure", "nothrow", "@property", "ref", "@safe"));
88
89 static assert(__traits(getFunctionAttributes, safe_nothrow) == tuple!("pure", "nothrow", "@nogc", "@safe"));
90 static assert(__traits(getFunctionAttributes, typeof(safe_nothrow)) == tuple!("pure", "nothrow", "@nogc", "@safe"));
91
92 static assert(__traits(getFunctionAttributes, live_nothrow) == tuple!("pure", "nothrow", "@nogc", "@live", "@safe"));
93 static assert(__traits(getFunctionAttributes, typeof(live_nothrow)) == tuple!("pure", "nothrow", "@nogc", "@live", "@safe"));
94
95 struct S2
96 {
97 int pure_const() const pure { return 0; }
98 int pure_sharedconst() const shared pure { return 0; }
99 }
100
101 static assert(__traits(getFunctionAttributes, S2.pure_const) == tuple!("const", "pure", "@system"));
102 static assert(__traits(getFunctionAttributes, typeof(S2.pure_const)) == tuple!("const", "pure", "@system"));
103
104 static assert(__traits(getFunctionAttributes, S2.pure_sharedconst) == tuple!("const", "shared", "pure", "@system"));
105 static assert(__traits(getFunctionAttributes, typeof(S2.pure_sharedconst)) == tuple!("const", "shared", "pure", "@system"));
106
107 static assert(__traits(getFunctionAttributes, (int a) { }) == tuple!("pure", "nothrow", "@nogc", "@safe"));
108 static assert(__traits(getFunctionAttributes, typeof((int a) { })) == tuple!("pure", "nothrow", "@nogc", "@safe"));
109
110 auto safeDel = delegate() @safe { };
111 static assert(__traits(getFunctionAttributes, safeDel) == tuple!("pure", "nothrow", "@nogc", "@safe"));
112 static assert(__traits(getFunctionAttributes, typeof(safeDel)) == tuple!("pure", "nothrow", "@nogc", "@safe"));
113
114 auto trustedDel = delegate() @trusted { };
115 static assert(__traits(getFunctionAttributes, trustedDel) == tuple!("pure", "nothrow", "@nogc", "@trusted"));
116 static assert(__traits(getFunctionAttributes, typeof(trustedDel)) == tuple!("pure", "nothrow", "@nogc", "@trusted"));
117
118 auto systemDel = delegate() @system { };
119 static assert(__traits(getFunctionAttributes, systemDel) == tuple!("pure", "nothrow", "@nogc", "@system"));
120 static assert(__traits(getFunctionAttributes, typeof(systemDel)) == tuple!("pure", "nothrow", "@nogc", "@system"));
121 }
122
123 void bug19706()
124 {
125 struct S
126 {
127 static int fImpl(Ret)() { return Ret.init; }
128
129 // tells us: `fImpl!int` is @system
130 static assert(__traits(getFunctionAttributes, fImpl!int) == tuple!("pure", "nothrow", "@nogc", "@safe"));
131 }
132 }