Debian Codegraph @

gcc/testsuite/gdc.test/fail_compilation/testInference.d

raw
1 /*
2 TEST_OUTPUT:
3 ---
4 fail_compilation/testInference.d(24): Error: return value `this.a` of type `inout(A8998)` does not match return type `immutable(A8998)`, and cannot be implicitly converted
5 ---
6 */
7
8 class A8998
9 {
10 int i;
11 }
12 class C8998
13 {
14 A8998 a;
15
16 this()
17 {
18 a = new A8998();
19 }
20
21 // WRONG: Returns immutable(A8998)
22 immutable(A8998) get() inout pure
23 {
24 return a; // should be error
25 }
26 }
27
28 /*
29 TEST_OUTPUT:
30 ---
31 fail_compilation/testInference.d(39): Error: return value `s` of type `const(char[])` does not match return type `string`, and cannot be implicitly converted
32 fail_compilation/testInference.d(44): Error: return value `a` of type `int[]` does not match return type `immutable(int[])`, and cannot be implicitly converted
33 fail_compilation/testInference.d(49): Error: return value `a` of type `int[]` does not match return type `immutable(int[])`, and cannot be implicitly converted
34 fail_compilation/testInference.d(54): Error: return value `a` of type `int[]` does not match return type `immutable(int[])`, and cannot be implicitly converted
35 ---
36 */
37 string foo(in char[] s) pure
38 {
39 return s; //
40 }
41 immutable(int[]) x1() /*pure*/
42 {
43 int[] a = new int[](10);
44 return a; //
45 }
46 immutable(int[]) x2(int len) /*pure*/
47 {
48 int[] a = new int[](len);
49 return a;
50 }
51 immutable(int[]) x3(immutable(int[]) org) /*pure*/
52 {
53 int[] a = new int[](org.length);
54 return a; //
55 }
56
57
58 /*
59 TEST_OUTPUT:
60 ---
61 fail_compilation/testInference.d(94): Error: return value `c` of type `testInference.C1` does not match return type `immutable(C1)`, and cannot be implicitly converted
62 fail_compilation/testInference.d(95): Error: return value `c` of type `testInference.C1` does not match return type `immutable(C1)`, and cannot be implicitly converted
63 fail_compilation/testInference.d(96): Error: return value `c` of type `testInference.C3` does not match return type `immutable(C3)`, and cannot be implicitly converted
64 fail_compilation/testInference.d(97): Error: return value `c` of type `testInference.C3` does not match return type `immutable(C3)`, and cannot be implicitly converted
65 fail_compilation/testInference.d(100): Error: undefined identifier `X1`, did you mean function `x1`?
66 fail_compilation/testInference.d(106): Error: return value `s` of type `S1` does not match return type `immutable(S1)`, and cannot be implicitly converted
67 fail_compilation/testInference.d(109): Error: return value `a` of type `int*[]` does not match return type `immutable(int*[])`, and cannot be implicitly converted
68 fail_compilation/testInference.d(110): Error: return value `a` of type `const(int)*[]` does not match return type `immutable(int*[])`, and cannot be implicitly converted
69 fail_compilation/testInference.d(114): Error: return value `s` of type `S2` does not match return type `immutable(S2)`, and cannot be implicitly converted
70 fail_compilation/testInference.d(115): Error: return value `s` of type `S2` does not match return type `immutable(S2)`, and cannot be implicitly converted
71 fail_compilation/testInference.d(116): Error: return value `s` of type `S2` does not match return type `immutable(S2)`, and cannot be implicitly converted
72 fail_compilation/testInference.d(118): Error: return value `a` of type `const(int)*[]` does not match return type `immutable(int*[])`, and cannot be implicitly converted
73 ---
74 */
75 immutable(Object) get(inout int*) pure
76 {
77 auto o = new Object;
78 return o; // should be ok
79 }
80 immutable(Object) get(immutable Object) pure
81 {
82 auto o = new Object;
83 return o; // should be ok
84 }
85 inout(Object) get(inout Object) pure
86 {
87 auto o = new Object;
88 return o; // should be ok (, but cannot in current conservative rule)
89 }
90
91 class C1 { C2 c2; }
92 class C2 { C3 c3; }
93 class C3 { C1 c1; }
94 immutable(C1) recursive1(C3 pc) pure { auto c = new C1(); return c; } // should be error, because pc.c1 == C1
95 immutable(C1) recursive2(C2 pc) pure { auto c = new C1(); return c; } // should be error, because pc.c3.c1 == C1
96 immutable(C3) recursive3(C1 pc) pure { auto c = new C3(); return c; } // should be error, c.c1 may be pc
97 immutable(C3) recursive4(C2 pc) pure { auto c = new C3(); return c; } // should be error, c.c1.c2 may be pc
98 immutable(C1) recursive5(shared C2 pc) pure { auto c = new C1(); return c; }
99 immutable(C1) recursive6(immutable C2 pc) pure { auto c = new C1(); return c; }
100 immutable(C1) recursiveE(immutable C2 pc) pure { auto c = new X1(); return c; }
101
102 class C4 { C4[] arr; }
103 immutable(C4)[] recursive7(int[] arr) pure { auto a = new C4[](1); return a; }
104
105 struct S1 { int* ptr; }
106 immutable(S1) foo1a( int*[] prm) pure { S1 s; return s; } // NG
107 immutable(S1) foo1b( const int*[] prm) pure { S1 s; return s; } // OK
108 immutable(S1) foo1c(immutable int*[] prm) pure { S1 s; return s; } // OK
109 immutable(int*[]) bar1a( S1 prm) pure { int *[] a; return a; } // NG
110 immutable(int*[]) bar1b( S1 prm) pure { const(int)*[] a; return a; } // NG
111 immutable(int*[]) bar1c( S1 prm) pure { immutable(int)*[] a; return a; } // OK
112
113 struct S2 { const(int)* ptr; }
114 immutable(S2) foo2a( int*[] prm) pure { S2 s; return s; } // OK
115 immutable(S2) foo2b( const int*[] prm) pure { S2 s; return s; } // NG
116 immutable(S2) foo2c(immutable int*[] prm) pure { S2 s; return s; } // NG
117 immutable(int*[]) bar2a( S2 prm) pure { int *[] a; return a; } // OK
118 immutable(int*[]) bar2b( S2 prm) pure { const(int)*[] a; return a; } // NG
119 immutable(int*[]) bar2c( S2 prm) pure { immutable(int)*[] a; return a; } // OK
120
121
122 /*
123 TEST_OUTPUT:
124 ---
125 fail_compilation/testInference.d(134): Error: return value `f10063(cast(inout(void*))p)` of type `inout(void)*` does not match return type `immutable(void)*`, and cannot be implicitly converted
126 ---
127 */
128 inout(void)* f10063(inout void* p) pure
129 {
130 return p;
131 }
132 immutable(void)* g10063(inout int* p) pure
133 {
134 return f10063(p);
135 }
136
137 /*
138 TEST_OUTPUT:
139 ---
140 fail_compilation/testInference.d(154): Error: `pure` function `testInference.bar14049` cannot call impure function `testInference.foo14049!int.foo14049`
141 fail_compilation/testInference.d(149): which calls `() => impure14049()`
142 fail_compilation/testInference.d(148): which calls `impure14049`
143 fail_compilation/testInference.d(143): and accessing mutable static data `i` makes it fail to infer `pure`
144 ---
145 */
146 #line 143
147 auto impure14049() { static int i = 1; return i; }
148
149 void foo14049(T)(T val)
150 {
151 auto n = () @trusted {
152 return impure14049();
153 }();
154 }
155
156 void bar14049() pure
157 {
158 foo14049(1);
159 }
160
161 /*
162 TEST_OUTPUT:
163 ---
164 fail_compilation/testInference.d(166): Error: `pure` function `testInference.f14160` cannot access mutable static data `g14160`
165 ---
166 */
167 int g14160;
168 int* f14160() pure
169 {
170 return &g14160; // should be rejected
171 }
172
173 /*
174 TEST_OUTPUT:
175 ---
176 fail_compilation/testInference.d(180): Error: `pure` function `testInference.test12422` cannot call impure function `testInference.test12422.bar12422!().bar12422`
177 fail_compilation/testInference.d(179): which calls `foo12422`
178 ---
179 */
180 #line 175
181 int g12422;
182 void foo12422() { ++g12422; }
183 void test12422() pure
184 {
185 void bar12422()() { foo12422(); }
186 bar12422();
187 }
188
189 /*
190 TEST_OUTPUT:
191 ---
192 fail_compilation/testInference.d(198): Error: `pure` function `testInference.test13729a` cannot call impure function `testInference.test13729a.foo`
193 fail_compilation/testInference.d(196): and accessing mutable static data `g13729` makes it fail to infer `pure`
194 fail_compilation/testInference.d(206): Error: `pure` function `testInference.test13729b` cannot call impure function `testInference.test13729b.foo!().foo`
195 fail_compilation/testInference.d(204): and accessing mutable static data `g13729` makes it fail to infer `pure`
196 ---
197 */
198
199 #line 190
200 int g13729;
201
202 void test13729a() pure
203 {
204 static void foo() // typed as impure
205 {
206 g13729++;
207 }
208 foo(); // cannot call impure function
209 }
210 void test13729b() pure
211 {
212 static void foo()() // inferred to impure
213 {
214 g13729++;
215 }
216 foo(); // cannot call impure function
217 }
218
219 /*
220 TEST_OUTPUT:
221 ---
222 fail_compilation/testInference.d(225): Error: `testInference.test17086` called with argument types `(bool)` matches multiple overloads exactly:
223 fail_compilation/testInference.d(219): `testInference.test17086!(bool, false).test17086(bool x)`
224 and:
225 fail_compilation/testInference.d(220): `testInference.test17086!(bool, false).test17086(bool y)`
226 ---
227 */
228
229 void test17086 (T, T V = T.init) (T x) { assert(x.foo); }
230 void test17086 (T, T V = T.init) (T y) { assert(y.bar); }
231
232 void test17086_call ()
233 {
234 bool f;
235 test17086(f);
236 }
237
238 /*
239 TEST_OUTPUT:
240 ---
241 fail_compilation/testInference.d(238): Error: `pure` function `testInference.test20047_pure_function` cannot call impure function `testInference.test20047_pure_function.bug`
242 fail_compilation/testInference.d(237): which calls `test20047_impure_function`
243 ---
244 */
245 #line 234
246 void test20047_impure_function() {}
247 void test20047_pure_function() pure
248 {
249 static void bug() { return test20047_impure_function(); }
250 bug();
251 }