Debian Codegraph @

mypyc/test-data/irbuild-vec-t.test

raw
1 -- Test cases for vec[t] where t is a boxed, non-vec type (PyObject *).
2 -- Also tests for vec[t | None], which uses the same representation.
3
4 [case testVecTConvertToList]
5 from librt.vecs import vec
6
7 def to_list(v: vec[str]) -> list[str]:
8 return list(v)
9 [out]
10 def to_list(v):
11 v :: vec[str]
12 r0 :: list
13 L0:
14 r0 = VecTApi.to_list(v)
15 return r0
16
17 [case testVecTConvertToTuple]
18 from librt.vecs import vec
19
20 def to_tuple(v: vec[str]) -> tuple[str, ...]:
21 return tuple(v)
22 [out]
23 def to_tuple(v):
24 v :: vec[str]
25 r0 :: tuple
26 L0:
27 r0 = VecTApi.to_tuple(v)
28 return r0
29
30 [case testVecTCreateEmpty]
31 from librt.vecs import vec, append
32
33 class C: pass
34
35 def primitive() -> vec[str]:
36 return vec[str]()
37
38 def native_class() -> vec[C]:
39 return vec[C]()
40 [out]
41 def primitive():
42 r0 :: object
43 r1 :: ptr
44 r2 :: vec[str]
45 L0:
46 r0 = load_address PyUnicode_Type
47 r1 = r0
48 r2 = VecTApi.alloc(0, 0, r1)
49 return r2
50 def native_class():
51 r0 :: object
52 r1 :: ptr
53 r2 :: vec[__main__.C]
54 L0:
55 r0 = __main__.C :: type
56 r1 = r0
57 r2 = VecTApi.alloc(0, 0, r1)
58 return r2
59
60 [case testVecTAppend]
61 from librt.vecs import vec, append
62
63 def f(v: vec[str]) -> vec[str]:
64 return append(v, 'x')
65 [out]
66 def f(v):
67 v :: vec[str]
68 r0 :: str
69 r1 :: object
70 r2 :: ptr
71 r3 :: vec[str]
72 L0:
73 r0 = 'x'
74 r1 = load_address PyUnicode_Type
75 r2 = r1
76 r3 = VecTApi.append(v, r0, r2)
77 return r3
78
79 [case testVecTExtend]
80 from librt.vecs import vec, extend
81 from typing import Iterable
82
83 def f(v: vec[str], it: Iterable[str]) -> vec[str]:
84 return extend(v, it)
85 [out]
86 def f(v, it):
87 v :: vec[str]
88 it, r0 :: object
89 r1 :: ptr
90 r2 :: vec[str]
91 L0:
92 r0 = load_address PyUnicode_Type
93 r1 = r0
94 r2 = VecTApi.extend(v, it, r1)
95 return r2
96
97 [case testVecTExtendVec]
98 from librt.vecs import vec, extend
99
100 def f(v: vec[str], v2: vec[str]) -> vec[str]:
101 return extend(v, v2)
102 [out]
103 def f(v, v2):
104 v, v2 :: vec[str]
105 r0 :: object
106 r1 :: ptr
107 r2 :: vec[str]
108 L0:
109 r0 = load_address PyUnicode_Type
110 r1 = r0
111 r2 = VecTApi.extend_vec(v, v2, r1)
112 return r2
113
114 [case testVecTOptionalCreateEmpty]
115 from librt.vecs import vec, append
116 from typing import Optional
117
118 class C: pass
119
120 def primitive() -> vec[Optional[str]]:
121 return vec[Optional[str]]()
122
123 def native_class() -> vec[Optional[C]]:
124 return vec[Optional[C]]()
125 [out]
126 def primitive():
127 r0 :: object
128 r1, r2 :: ptr
129 r3 :: vec[str | None]
130 L0:
131 r0 = load_address PyUnicode_Type
132 r1 = r0
133 r2 = r1 | 1
134 r3 = VecTApi.alloc(0, 0, r2)
135 return r3
136 def native_class():
137 r0 :: object
138 r1, r2 :: ptr
139 r3 :: vec[__main__.C | None]
140 L0:
141 r0 = __main__.C :: type
142 r1 = r0
143 r2 = r1 | 1
144 r3 = VecTApi.alloc(0, 0, r2)
145 return r3
146
147 [case testVecTOptionalAppend]
148 from librt.vecs import vec, append
149 from typing import Optional
150
151 def f(v: vec[Optional[str]]) -> vec[Optional[str]]:
152 v = append(v, 'x')
153 return append(v, None)
154 [out]
155 def f(v):
156 v :: vec[str | None]
157 r0 :: str
158 r1 :: object
159 r2, r3 :: ptr
160 r4 :: vec[str | None]
161 r5, r6 :: object
162 r7, r8 :: ptr
163 r9 :: vec[str | None]
164 L0:
165 r0 = 'x'
166 r1 = load_address PyUnicode_Type
167 r2 = r1
168 r3 = r2 | 1
169 r4 = VecTApi.append(v, r0, r3)
170 v = r4
171 r5 = box(None, 1)
172 r6 = load_address PyUnicode_Type
173 r7 = r6
174 r8 = r7 | 1
175 r9 = VecTApi.append(v, r5, r8)
176 return r9
177
178 [case testVecTLen_64bit]
179 from librt.vecs import vec
180 from mypy_extensions import i64
181 from typing import Optional
182
183 def f(v: vec[str]) -> i64:
184 return len(v)
185
186 def g(v: vec[Optional[str]]) -> i64:
187 return len(v)
188 [out]
189 def f(v):
190 v :: vec[str]
191 r0 :: native_int
192 L0:
193 r0 = v.len
194 return r0
195 def g(v):
196 v :: vec[str | None]
197 r0 :: native_int
198 L0:
199 r0 = v.len
200 return r0
201
202 [case testVecTGetItem_64bit]
203 from librt.vecs import vec
204 from mypy_extensions import i64
205
206 def f(v: vec[str], n: i64) -> str:
207 return v[n]
208 [out]
209 def f(v, n):
210 v :: vec[str]
211 n :: i64
212 r0 :: native_int
213 r1 :: bit
214 r2 :: i64
215 r3 :: bit
216 r4 :: bool
217 r5 :: i64
218 r6 :: str
219 L0:
220 r0 = v.len
221 r1 = n < r0 :: unsigned
222 if r1 goto L4 else goto L1 :: bool
223 L1:
224 r2 = n + r0
225 r3 = r2 < r0 :: unsigned
226 if r3 goto L3 else goto L2 :: bool
227 L2:
228 r4 = raise IndexError
229 unreachable
230 L3:
231 r5 = r2
232 goto L5
233 L4:
234 r5 = n
235 L5:
236 r6 = vec_get_item_unsafe[str] v, r5
237 return r6
238
239 [case testVecTOptionalGetItem_64bit]
240 from librt.vecs import vec
241 from mypy_extensions import i64
242 from typing import Optional
243
244 def f(v: vec[Optional[str]], n: i64) -> Optional[str]:
245 return v[n]
246 [out]
247 def f(v, n):
248 v :: vec[str | None]
249 n :: i64
250 r0 :: native_int
251 r1 :: bit
252 r2 :: i64
253 r3 :: bit
254 r4 :: bool
255 r5 :: i64
256 r6 :: union[str, None]
257 L0:
258 r0 = v.len
259 r1 = n < r0 :: unsigned
260 if r1 goto L4 else goto L1 :: bool
261 L1:
262 r2 = n + r0
263 r3 = r2 < r0 :: unsigned
264 if r3 goto L3 else goto L2 :: bool
265 L2:
266 r4 = raise IndexError
267 unreachable
268 L3:
269 r5 = r2
270 goto L5
271 L4:
272 r5 = n
273 L5:
274 r6 = vec_get_item_unsafe[union[str, None]] v, r5
275 return r6
276
277 [case testNewTPopLast]
278 from typing import Tuple
279 from librt.vecs import vec, pop
280
281 def pop_last(v: vec[str]) -> Tuple[vec[str], str]:
282 return pop(v)
283 [out]
284 def pop_last(v):
285 v :: vec[str]
286 r0 :: tuple[vec[str], str]
287 L0:
288 r0 = VecTApi.pop(v, -1)
289 return r0
290
291 [case testVecTConstructFromListComprehension]
292 from librt.vecs import vec
293 from mypy_extensions import i64
294
295 def f(n: i64) -> vec[str]:
296 return vec[str](['x' for x in range(i64(5))])
297 [out]
298 def f(n):
299 n :: i64
300 r0 :: object
301 r1 :: ptr
302 r2, r3 :: vec[str]
303 r4, x :: i64
304 r5 :: bit
305 r6 :: str
306 r7 :: object
307 r8 :: ptr
308 r9 :: vec[str]
309 r10 :: i64
310 L0:
311 r0 = load_address PyUnicode_Type
312 r1 = r0
313 r2 = VecTApi.alloc(0, 0, r1)
314 r3 = r2
315 r4 = 0
316 x = r4
317 L1:
318 r5 = r4 < 5 :: signed
319 if r5 goto L2 else goto L4 :: bool
320 L2:
321 x = r4
322 r6 = 'x'
323 r7 = load_address PyUnicode_Type
324 r8 = r7
325 r9 = VecTApi.append(r3, r6, r8)
326 r3 = r9
327 L3:
328 r10 = r4 + 1
329 r4 = r10
330 goto L1
331 L4:
332 return r3
333
334 [case testVecTCheckItemType]
335 from librt.vecs import vec
336 from typing import Tuple, Any, List, TypeVar
337
338 def bad1(v: vec[Tuple[str, str]]) -> None: pass
339 def bad2(v: vec[int]) -> None: pass
340 def bad3(v: vec) -> None: pass
341 def bad4(v: vec[Any]) -> None: pass
342 T = TypeVar("T")
343 def bad5(v: vec[T]) -> None: pass
344
345 def ok1(v: vec[str], v2: vec[bytes]) -> None: pass
346 def ok2(v: vec[Tuple[str, ...]]) -> None: pass
347 def ok3(v: vec[object]) -> None: pass
348 def ok4(v: vec[List[Any]]) -> None: pass
349 def ok5(v: vec[vec[str]]) -> None: pass
350 [out]
351 main:4: error: Invalid item type for "vec"
352 main:5: error: Invalid item type for "vec"
353 main:6: error: Invalid item type for "vec"
354 main:7: error: Invalid item type for "vec"
355 main:9: error: Invalid item type for "vec"
356
357 [case testVecTCheckItemTypeUnion]
358 from librt.vecs import vec
359 from typing import Tuple, Union, Optional, Any, TypeVar
360 from mypy_extensions import i64
361
362 def bad1(v: vec[Union[str, bytes]]) -> None: pass
363 def bad2(v: vec[Union[str, bytes, None]]) -> None: pass
364 def bad3(v: vec[Union[int, None]]) -> None: pass
365 def bad4(v: vec[Union[None, int]]) -> None: pass
366 def bad5(v: vec[Union[i64, None]]) -> None: pass
367 def bad6(v: vec[Union[None, i64]]) -> None: pass
368 def bad7(v: vec[Union[bool, None]]) -> None: pass
369 def bad9(v: vec[Union[float, None]]) -> None: pass
370 def bad10(v: vec[Union[vec[str], None]]) -> None: pass
371 def bad11(v: vec[Optional[vec[str]]]) -> None: pass
372 def bad12(v: vec[Union[str, Any]]) -> None: pass
373 T = TypeVar("T")
374 def bad13(v: vec[Union[str, T]]) -> None: pass
375
376 def ok1(v: vec[Union[str, None]]) -> None: pass
377 def ok2(v: vec[Union[None, str]]) -> None: pass
378 [out]
379 main:5: error: Invalid item type for "vec"
380 main:6: error: Invalid item type for "vec"
381 main:7: error: Invalid item type for "vec"
382 main:8: error: Invalid item type for "vec"
383 main:9: error: Invalid item type for "vec"
384 main:10: error: Invalid item type for "vec"
385 main:11: error: Invalid item type for "vec"
386 main:12: error: Invalid item type for "vec"
387 main:13: error: Invalid item type for "vec"
388 main:14: error: Invalid item type for "vec"
389 main:15: error: Invalid item type for "vec"
390 main:17: error: Invalid item type for "vec"
391
392 [case testVecTCheckItemTypeDuringConstruction]
393 from librt.vecs import vec
394 from typing import Optional, Union
395
396 def f() -> None:
397 vec[Optional[int]]()
398 vec[Union[str, bytes]]()
399 vec[Optional[vec[str]]]()
400
401 [out]
402 main:5: error: Invalid item type for "vec"
403 main:6: error: Invalid item type for "vec"
404 main:7: error: Invalid item type for "vec"
405
406 [case testVecTCreateWithCap_64bit]
407 from librt.vecs import vec
408 from mypy_extensions import i64
409
410 def cap_only() -> vec[str]:
411 return vec[str](capacity=5)
412
413 def cap_variable(n: i64) -> vec[str]:
414 return vec[str](capacity=n)
415 [out]
416 def cap_only():
417 r0 :: object
418 r1 :: ptr
419 r2 :: vec[str]
420 L0:
421 r0 = load_address PyUnicode_Type
422 r1 = r0
423 r2 = VecTApi.alloc(0, 5, r1)
424 return r2
425 def cap_variable(n):
426 n :: i64
427 r0 :: object
428 r1 :: ptr
429 r2 :: vec[str]
430 L0:
431 r0 = load_address PyUnicode_Type
432 r1 = r0
433 r2 = VecTApi.alloc(0, n, r1)
434 return r2
435
436 [case testVecTCreateFromListWithCap_64bit]
437 from librt.vecs import vec
438 from mypy_extensions import i64
439
440 def list_with_cap() -> vec[str]:
441 return vec[str](['a', 'b'], capacity=5)
442 [out]
443 def list_with_cap():
444 r0, r1 :: str
445 r2 :: object
446 r3 :: ptr
447 r4 :: vec[str]
448 r5, r6, r7 :: ptr
449 L0:
450 r0 = 'a'
451 r1 = 'b'
452 r2 = load_address PyUnicode_Type
453 r3 = r2
454 r4 = VecTApi.alloc(2, 5, r3)
455 r5 = r4.items
456 set_mem r5, r0 :: builtins.str*
457 r6 = r5 + 8
458 set_mem r6, r1 :: builtins.str*
459 r7 = r6 + 8
460 keep_alive r4
461 return r4
462
463 [case testVecTConstructFromIterable]
464 from typing import Iterable
465 from librt.vecs import vec
466
467 def f(x: Iterable[str]) -> vec[str]:
468 return vec[str](x)
469 [out]
470 def f(x):
471 x, r0 :: object
472 r1 :: ptr
473 r2 :: vec[str]
474 L0:
475 r0 = load_address PyUnicode_Type
476 r1 = r0
477 r2 = VecTApi.from_iterable(r1, x, 0)
478 return r2
479
480 [case testVecTCreateFromListObject]
481 from librt.vecs import vec
482
483 def from_list(a: list[str]) -> vec[str]:
484 return vec[str](a)
485 [out]
486 def from_list(a):
487 a :: list
488 r0 :: object
489 r1 :: ptr
490 r2 :: vec[str]
491 L0:
492 r0 = load_address PyUnicode_Type
493 r1 = r0
494 r2 = VecTApi.from_iterable(r1, a, 0)
495 return r2
496
497 [case testVecTCreateFromTupleObject]
498 from librt.vecs import vec
499
500 def from_tuple(a: tuple[str, ...]) -> vec[str]:
501 return vec[str](a)
502 [out]
503 def from_tuple(a):
504 a :: tuple
505 r0 :: object
506 r1 :: ptr
507 r2 :: vec[str]
508 L0:
509 r0 = load_address PyUnicode_Type
510 r1 = r0
511 r2 = VecTApi.from_iterable(r1, a, 0)
512 return r2
513
514 [case testVecTBorrowGetItem_64bit]
515 from typing import Final
516
517 from librt.vecs import vec
518 from mypy_extensions import i64
519
520 class A:
521 def __init__(self, x: str) -> None:
522 # Final to allow borrowing on free-threaded builds
523 self.x: Final = x
524
525 def f(v: vec[A], n: i64) -> int:
526 return len(v[n].x)
527 [out]
528 def A.__init__(self, x):
529 self :: __main__.A
530 x :: str
531 L0:
532 self.x = x
533 return 1
534 def f(v, n):
535 v :: vec[__main__.A]
536 n :: i64
537 r0 :: native_int
538 r1 :: bit
539 r2 :: i64
540 r3 :: bit
541 r4 :: bool
542 r5 :: i64
543 r6 :: __main__.A
544 r7 :: str
545 r8 :: native_int
546 r9 :: bit
547 r10 :: short_int
548 L0:
549 r0 = v.len
550 r1 = n < r0 :: unsigned
551 if r1 goto L4 else goto L1 :: bool
552 L1:
553 r2 = n + r0
554 r3 = r2 < r0 :: unsigned
555 if r3 goto L3 else goto L2 :: bool
556 L2:
557 r4 = raise IndexError
558 unreachable
559 L3:
560 r5 = r2
561 goto L5
562 L4:
563 r5 = n
564 L5:
565 r6 = vec_get_item_unsafe_borrow[__main__.A] v, r5
566 r7 = r6.x
567 keep_alive v, r5
568 r8 = CPyStr_Size_size_t(r7)
569 r9 = r8 >= 0 :: signed
570 r10 = r8 << 1
571 return r10