Debian Codegraph @

mypyc/test-data/refcount.test

raw
1 -- Test cases for reference count insertion.
2
3 [case testReturnLiteral]
4 def f() -> int:
5 return 1
6 [out]
7 def f():
8 L0:
9 return 2
10
11 [case testReturnLocal]
12 def f() -> int:
13 x = 1
14 return x
15 [out]
16 def f():
17 x :: int
18 L0:
19 x = 2
20 return x
21
22 [case testLocalVars]
23 def f() -> int:
24 x = 1
25 y = x
26 x = y
27 return x
28 [out]
29 def f():
30 x, y :: int
31 L0:
32 x = 2
33 y = x
34 x = y
35 return x
36
37 [case testLocalVars2]
38 def f() -> int:
39 x = 1
40 y = x
41 z = x
42 return y + z
43 [out]
44 def f():
45 x, y, z, r0 :: int
46 L0:
47 x = 2
48 inc_ref x :: int
49 y = x
50 z = x
51 r0 = CPyTagged_Add(y, z)
52 dec_ref y :: int
53 dec_ref z :: int
54 return r0
55
56 [case testFreeAtReturn]
57 def f() -> int:
58 x = 1
59 y = 2
60 if x == 1:
61 return x
62 return y
63 [out]
64 def f():
65 x, y :: int
66 r0 :: bit
67 L0:
68 x = 2
69 y = 4
70 r0 = int_eq x, 2
71 if r0 goto L3 else goto L4 :: bool
72 L1:
73 return x
74 L2:
75 return y
76 L3:
77 dec_ref y :: int
78 goto L1
79 L4:
80 dec_ref x :: int
81 goto L2
82
83 [case testArgumentsInOps]
84 def f(a: int, b: int) -> int:
85 x = a + 1
86 y = x + a
87 return y
88 [out]
89 def f(a, b):
90 a, b, r0, x, r1, y :: int
91 L0:
92 r0 = CPyTagged_Add(a, 2)
93 x = r0
94 r1 = CPyTagged_Add(x, a)
95 dec_ref x :: int
96 y = r1
97 return y
98
99 [case testArgumentsInAssign]
100 def f(a: int) -> int:
101 x = a
102 y = a
103 x = 1
104 return x + y
105 [out]
106 def f(a):
107 a, x, y, r0 :: int
108 L0:
109 inc_ref a :: int
110 x = a
111 dec_ref x :: int
112 inc_ref a :: int
113 y = a
114 x = 2
115 r0 = CPyTagged_Add(x, y)
116 dec_ref x :: int
117 dec_ref y :: int
118 return r0
119
120 [case testAssignToArgument1]
121 def f(a: int) -> int:
122 a = 1
123 y = a
124 return y
125 [out]
126 def f(a):
127 a, y :: int
128 L0:
129 a = 2
130 y = a
131 return y
132
133 [case testAssignToArgument2]
134 def f(a: int) -> int:
135 a = 1
136 a = 2
137 a = 3
138 return a
139 [out]
140 def f(a):
141 a :: int
142 L0:
143 a = 2
144 dec_ref a :: int
145 a = 4
146 dec_ref a :: int
147 a = 6
148 return a
149
150 [case testAssignToArgument3]
151 def f(a: int) -> int:
152 x = 1
153 a = x
154 y = x
155 return a
156 [out]
157 def f(a):
158 a, x, y :: int
159 L0:
160 x = 2
161 inc_ref x :: int
162 a = x
163 y = x
164 dec_ref y :: int
165 return a
166
167 [case testReturnArgument]
168 def f(a: int) -> int:
169 return a
170 [out]
171 def f(a):
172 a :: int
173 L0:
174 inc_ref a :: int
175 return a
176
177 [case testConditionalAssignToArgument1]
178 def f(a: int) -> int:
179 if a == a:
180 a = 1
181 else:
182 x = 2
183 y = a + 1
184 return y
185 [out]
186 def f(a):
187 a :: int
188 r0 :: bit
189 x, r1, y :: int
190 L0:
191 r0 = int_eq a, a
192 if r0 goto L1 else goto L2 :: bool
193 L1:
194 a = 2
195 goto L3
196 L2:
197 x = 4
198 dec_ref x :: int
199 goto L4
200 L3:
201 r1 = CPyTagged_Add(a, 2)
202 dec_ref a :: int
203 y = r1
204 return y
205 L4:
206 inc_ref a :: int
207 goto L3
208
209 [case testConditionalAssignToArgument2]
210 def f(a: int) -> int:
211 if a == a:
212 x = 2
213 else:
214 a = 1
215 y = a + 1
216 return y
217 [out]
218 def f(a):
219 a :: int
220 r0 :: bit
221 x, r1, y :: int
222 L0:
223 r0 = int_eq a, a
224 if r0 goto L1 else goto L2 :: bool
225 L1:
226 x = 4
227 dec_ref x :: int
228 goto L4
229 L2:
230 a = 2
231 L3:
232 r1 = CPyTagged_Add(a, 2)
233 dec_ref a :: int
234 y = r1
235 return y
236 L4:
237 inc_ref a :: int
238 goto L3
239
240 [case testConditionalAssignToArgument3]
241 def f(a: int) -> int:
242 if a == a:
243 a = 1
244 return a
245 [out]
246 def f(a):
247 a :: int
248 r0 :: bit
249 L0:
250 r0 = int_eq a, a
251 if r0 goto L1 else goto L3 :: bool
252 L1:
253 a = 2
254 L2:
255 return a
256 L3:
257 inc_ref a :: int
258 goto L2
259
260 [case testAssignRegisterToItself]
261 def f(a: int) -> int:
262 a = a
263 x = 1
264 x = x
265 return x + a
266 -- This is correct but bad code
267 [out]
268 def f(a):
269 a, x, r0 :: int
270 L0:
271 inc_ref a :: int
272 a = a
273 x = 2
274 inc_ref x :: int
275 dec_ref x :: int
276 x = x
277 r0 = CPyTagged_Add(x, a)
278 dec_ref x :: int
279 dec_ref a :: int
280 return r0
281
282 [case testIncrement1]
283 def f(a: int) -> int:
284 a = a + 1
285 x = 1
286 x = x + 1
287 return a + x
288 [out]
289 def f(a):
290 a, r0, x, r1, r2 :: int
291 L0:
292 r0 = CPyTagged_Add(a, 2)
293 a = r0
294 x = 2
295 r1 = CPyTagged_Add(x, 2)
296 dec_ref x :: int
297 x = r1
298 r2 = CPyTagged_Add(a, x)
299 dec_ref a :: int
300 dec_ref x :: int
301 return r2
302
303 [case testIncrement2]
304 def f() -> None:
305 x = 1
306 x = x + 1
307 [out]
308 def f():
309 x, r0 :: int
310 L0:
311 x = 2
312 r0 = CPyTagged_Add(x, 2)
313 dec_ref x :: int
314 x = r0
315 dec_ref x :: int
316 return 1
317
318 [case testAdd1]
319 def f() -> None:
320 y = 1
321 x = y + 1
322 [out]
323 def f():
324 y, r0, x :: int
325 L0:
326 y = 2
327 r0 = CPyTagged_Add(y, 2)
328 dec_ref y :: int
329 x = r0
330 dec_ref x :: int
331 return 1
332
333 [case testAdd2]
334 def f(a: int) -> int:
335 a = a + a
336 x = a
337 x = x + x
338 return x
339 [out]
340 def f(a):
341 a, r0, x, r1 :: int
342 L0:
343 r0 = CPyTagged_Add(a, a)
344 a = r0
345 x = a
346 r1 = CPyTagged_Add(x, x)
347 dec_ref x :: int
348 x = r1
349 return x
350
351 [case testAdd3]
352 def f(a: int) -> int:
353 x = a + a
354 y = x + x
355 return y
356 [out]
357 def f(a):
358 a, r0, x, r1, y :: int
359 L0:
360 r0 = CPyTagged_Add(a, a)
361 x = r0
362 r1 = CPyTagged_Add(x, x)
363 dec_ref x :: int
364 y = r1
365 return y
366
367 [case testAdd4]
368 def f(a: int) -> None:
369 x = a + a
370 y = 1
371 z = y + y
372 [out]
373 def f(a):
374 a, r0, x, y, r1, z :: int
375 L0:
376 r0 = CPyTagged_Add(a, a)
377 x = r0
378 dec_ref x :: int
379 y = 2
380 r1 = CPyTagged_Add(y, y)
381 dec_ref y :: int
382 z = r1
383 dec_ref z :: int
384 return 1
385
386 [case testAdd5]
387 def f(a: int) -> None:
388 a = a + a
389 x = 1
390 x = x + x
391 [out]
392 def f(a):
393 a, r0, x, r1 :: int
394 L0:
395 r0 = CPyTagged_Add(a, a)
396 a = r0
397 dec_ref a :: int
398 x = 2
399 r1 = CPyTagged_Add(x, x)
400 dec_ref x :: int
401 x = r1
402 dec_ref x :: int
403 return 1
404
405 [case testReturnInMiddleOfFunction]
406 def f() -> int:
407 x = 1
408 y = 2
409 z = 3
410 if z == z:
411 return z
412 a = 1
413 return x + y - a
414 [out]
415 def f():
416 x, y, z :: int
417 r0 :: bit
418 a, r1, r2 :: int
419 L0:
420 x = 2
421 y = 4
422 z = 6
423 r0 = int_eq z, z
424 if r0 goto L3 else goto L4 :: bool
425 L1:
426 return z
427 L2:
428 a = 2
429 r1 = CPyTagged_Add(x, y)
430 dec_ref x :: int
431 dec_ref y :: int
432 r2 = CPyTagged_Subtract(r1, a)
433 dec_ref r1 :: int
434 dec_ref a :: int
435 return r2
436 L3:
437 dec_ref x :: int
438 dec_ref y :: int
439 goto L1
440 L4:
441 dec_ref z :: int
442 goto L2
443
444 [case testLoop]
445 def f(a: int) -> int:
446 sum = 0
447 i = 0
448 while i <= a:
449 sum = sum + i
450 i = i + 1
451 return sum
452 [out]
453 def f(a):
454 a, sum, i :: int
455 r0 :: bit
456 r1, r2 :: int
457 L0:
458 sum = 0
459 i = 0
460 L1:
461 r0 = int_le i, a
462 if r0 goto L2 else goto L4 :: bool
463 L2:
464 r1 = CPyTagged_Add(sum, i)
465 dec_ref sum :: int
466 sum = r1
467 r2 = CPyTagged_Add(i, 2)
468 dec_ref i :: int
469 i = r2
470 goto L1
471 L3:
472 return sum
473 L4:
474 dec_ref i :: int
475 goto L3
476
477 [case testCall]
478 def f(a: int) -> int:
479 return f(a + 1)
480 [out]
481 def f(a):
482 a, r0, r1 :: int
483 L0:
484 r0 = CPyTagged_Add(a, 2)
485 r1 = f(r0)
486 dec_ref r0 :: int
487 return r1
488
489 [case testError]
490 def f(x: List[int]) -> None: pass # E: Name "List" is not defined \
491 # N: Did you forget to import it from "typing"? (Suggestion: "from typing import List")
492
493 [case testNewList]
494 def f() -> int:
495 a = [0, 1]
496 return 0
497 [out]
498 def f():
499 r0 :: list
500 r1, r2 :: object
501 r3 :: ptr
502 a :: list
503 L0:
504 r0 = PyList_New(2)
505 r1 = object 0
506 r2 = object 1
507 r3 = list_items r0
508 inc_ref r1
509 buf_init_item r3, 0, r1
510 inc_ref r2
511 buf_init_item r3, 1, r2
512 a = r0
513 dec_ref a
514 return 0
515
516 [case testListSet]
517 from typing import List
518 def f(a: List[int], b: List[int]) -> None:
519 a[0] = b[0]
520 [out]
521 def f(a, b):
522 a, b :: list
523 r0 :: object
524 r1 :: int
525 r2 :: object
526 r3 :: bit
527 L0:
528 r0 = CPyList_GetItemShort(b, 0)
529 r1 = unbox(int, r0)
530 dec_ref r0
531 r2 = box(int, r1)
532 r3 = CPyList_SetItem(a, 0, r2)
533 return 1
534
535 [case testTupleRefcount]
536 from typing import Tuple
537 def f(x: Tuple[Tuple[int, bool], bool]) -> int:
538 return x[0][0]
539 [out]
540 def f(x):
541 x :: tuple[tuple[int, bool], bool]
542 r0 :: tuple[int, bool]
543 r1 :: int
544 L0:
545 r0 = x[0]
546 r1 = r0[0]
547 dec_ref r0
548 return r1
549
550 [case testUserClassRefCount]
551 class C:
552 x: 'C'
553 def f() -> None:
554 c = C()
555 c.x = C()
556 [out]
557 def f():
558 r0, c, r1 :: __main__.C
559 r2 :: bool
560 L0:
561 r0 = C()
562 c = r0
563 r1 = C()
564 c.x = r1; r2 = is_error
565 dec_ref c
566 return 1
567
568 [case testCastRefCount]
569 class C: pass
570
571 def f() -> None:
572 a = [C()]
573 d = a[0]
574 [out]
575 def f():
576 r0 :: __main__.C
577 r1 :: list
578 r2 :: ptr
579 a :: list
580 r3 :: object
581 r4, d :: __main__.C
582 L0:
583 r0 = C()
584 r1 = PyList_New(1)
585 r2 = list_items r1
586 buf_init_item r2, 0, r0
587 a = r1
588 r3 = CPyList_GetItemShort(a, 0)
589 dec_ref a
590 r4 = cast(__main__.C, r3)
591 d = r4
592 dec_ref d
593 return 1
594
595 [case testUnaryBranchSpecialCase]
596 def f(x: bool) -> int:
597 if x:
598 return 1
599 return 2
600 [out]
601 def f(x):
602 x :: bool
603 L0:
604 if x goto L1 else goto L2 :: bool
605 L1:
606 return 2
607 L2:
608 return 4
609
610 [case testReturnTuple]
611 from typing import Tuple
612
613 class C: pass
614 def f() -> Tuple[C, C]:
615 a = C()
616 b = C()
617 return a, b
618 [out]
619 def f():
620 r0, a, r1, b, r2, r3 :: __main__.C
621 r4 :: tuple[__main__.C, __main__.C]
622 L0:
623 r0 = C()
624 a = r0
625 r1 = C()
626 b = r1
627 r2 = a
628 r3 = b
629 r4 = (r2, r3)
630 return r4
631
632 [case testDecomposeTuple]
633 from typing import Tuple
634
635 class C:
636 a: int
637
638 def f() -> int:
639 x, y = g()
640 return x.a + y.a
641
642 def g() -> Tuple[C, C]:
643 return C(), C()
644 [out]
645 def f():
646 r0 :: tuple[__main__.C, __main__.C]
647 r1, r2, r3, x, r4, y :: __main__.C
648 r5, r6, r7 :: int
649 L0:
650 r0 = g()
651 r1 = borrow r0[0]
652 r2 = borrow r0[1]
653 r3 = unborrow r1
654 x = r3
655 r4 = unborrow r2
656 y = r4
657 r5 = borrow x.a
658 r6 = borrow y.a
659 r7 = CPyTagged_Add(r5, r6)
660 dec_ref x
661 dec_ref y
662 return r7
663 def g():
664 r0, r1 :: __main__.C
665 r2 :: tuple[__main__.C, __main__.C]
666 L0:
667 r0 = C()
668 r1 = C()
669 r2 = (r0, r1)
670 return r2
671
672 [case testUnicodeLiteral]
673 def f() -> str:
674 return "some string"
675 [out]
676 def f():
677 r0 :: str
678 L0:
679 r0 = 'some string'
680 inc_ref r0
681 return r0
682
683 [case testPyMethodCall]
684 def g(x: str) -> int:
685 return int(x, base=2)
686 [out]
687 def g(x):
688 x :: str
689 r0, r1 :: object
690 r2 :: object[2]
691 r3 :: object_ptr
692 r4, r5 :: object
693 r6 :: int
694 L0:
695 r0 = load_address PyLong_Type
696 r1 = object 2
697 r2 = [x, r1]
698 r3 = load_address r2
699 r4 = ('base',)
700 r5 = PyObject_Vectorcall(r0, r3, 1, r4)
701 r6 = unbox(int, r5)
702 dec_ref r5
703 return r6
704
705 [case testListAppend]
706 from typing import List
707 def f(a: List[int], x: int) -> None:
708 a.append(x)
709 [out]
710 def f(a, x):
711 a :: list
712 x :: int
713 r0 :: object
714 r1 :: i32
715 r2 :: bit
716 L0:
717 inc_ref x :: int
718 r0 = box(int, x)
719 r1 = PyList_Append(a, r0)
720 dec_ref r0
721 r2 = r1 >= 0 :: signed
722 return 1
723
724 [case testForDict]
725 from typing import Dict
726
727 def f(d: Dict[int, int]) -> None:
728 for key in d:
729 d[key]
730 [out]
731 def f(d):
732 d :: dict
733 r0 :: short_int
734 r1 :: native_int
735 r2 :: object
736 r3 :: tuple[bool, short_int, object]
737 r4 :: short_int
738 r5 :: bool
739 r6 :: object
740 r7, key :: int
741 r8, r9 :: object
742 r10 :: int
743 r11, r12 :: bit
744 L0:
745 r0 = 0
746 r1 = PyDict_Size(d)
747 r2 = CPyDict_GetKeysIter(d)
748 L1:
749 r3 = CPyDict_NextKey(r2, r0)
750 r4 = r3[1]
751 r0 = r4
752 r5 = r3[0]
753 if r5 goto L2 else goto L6 :: bool
754 L2:
755 r6 = r3[2]
756 dec_ref r3
757 r7 = unbox(int, r6)
758 dec_ref r6
759 key = r7
760 r8 = box(int, key)
761 r9 = CPyDict_GetItem(d, r8)
762 dec_ref r8
763 r10 = unbox(int, r9)
764 dec_ref r9
765 dec_ref r10 :: int
766 L3:
767 r11 = CPyDict_CheckSize(d, r1)
768 goto L1
769 L4:
770 r12 = CPy_NoErrOccurred()
771 L5:
772 return 1
773 L6:
774 dec_ref r2
775 dec_ref r3
776 goto L4
777
778 [case testBorrowRefs]
779 def make_garbage(arg: object) -> None:
780 b = True
781 while b:
782 arg = None
783 b = False
784 [out]
785 def make_garbage(arg):
786 arg :: object
787 b :: bool
788 r0 :: object
789 L0:
790 b = 1
791 L1:
792 if b goto L2 else goto L3 :: bool
793 L2:
794 r0 = box(None, 1)
795 inc_ref r0
796 arg = r0
797 dec_ref arg
798 b = 0
799 goto L1
800 L3:
801 return 1
802
803 [case testTupleUnpackUnused]
804 from typing import Tuple
805
806 def f(x: Tuple[str, int]) -> int:
807 a, xi = x
808 return 0
809 [out]
810 def f(x):
811 x :: tuple[str, int]
812 r0 :: str
813 r1 :: int
814 r2, a :: str
815 r3, xi :: int
816 L0:
817 r0 = borrow x[0]
818 r1 = borrow x[1]
819 inc_ref x
820 r2 = unborrow r0
821 a = r2
822 dec_ref a
823 r3 = unborrow r1
824 xi = r3
825 dec_ref xi :: int
826 return 0
827
828 [case testGetElementPtrLifeTime]
829 from typing import List
830
831 def f() -> int:
832 x: List[str] = []
833 return len(x)
834 [out]
835 def f():
836 r0, x :: list
837 r1 :: native_int
838 r2 :: short_int
839 L0:
840 r0 = PyList_New(0)
841 x = r0
842 r1 = var_object_size x
843 dec_ref x
844 r2 = r1 << 1
845 return r2
846
847 [case testSometimesUninitializedVariable]
848 def f(x: bool) -> int:
849 if x:
850 y = 1
851 else:
852 z = 2
853 return y + z
854 [out]
855 def f(x):
856 x :: bool
857 r0, y, r1, z :: int
858 r2, r3 :: bool
859 r4 :: int
860 L0:
861 r0 = <error> :: int
862 y = r0
863 r1 = <error> :: int
864 z = r1
865 if x goto L8 else goto L9 :: bool
866 L1:
867 y = 2
868 goto L3
869 L2:
870 z = 4
871 L3:
872 if is_error(y) goto L10 else goto L5
873 L4:
874 r2 = raise UnboundLocalError('local variable "y" referenced before assignment')
875 unreachable
876 L5:
877 if is_error(z) goto L11 else goto L7
878 L6:
879 r3 = raise UnboundLocalError('local variable "z" referenced before assignment')
880 unreachable
881 L7:
882 r4 = CPyTagged_Add(y, z)
883 xdec_ref y :: int
884 xdec_ref z :: int
885 return r4
886 L8:
887 xdec_ref y :: int
888 goto L1
889 L9:
890 xdec_ref z :: int
891 goto L2
892 L10:
893 xdec_ref z :: int
894 goto L4
895 L11:
896 xdec_ref y :: int
897 goto L6
898
899 [case testVectorcall]
900 from typing import Any
901
902 def call(f: Any, x: int) -> int:
903 return f(x)
904 [out]
905 def call(f, x):
906 f :: object
907 x :: int
908 r0 :: object
909 r1 :: object[1]
910 r2 :: object_ptr
911 r3 :: object
912 r4 :: int
913 L0:
914 inc_ref x :: int
915 r0 = box(int, x)
916 r1 = [r0]
917 r2 = load_address r1
918 r3 = PyObject_Vectorcall(f, r2, 1, 0)
919 dec_ref r0
920 r4 = unbox(int, r3)
921 dec_ref r3
922 return r4
923
924 [case testVectorcallMethod_64bit]
925 from typing import Any
926
927 def call(o: Any, x: int) -> int:
928 return o.m(x)
929 [out]
930 def call(o, x):
931 o :: object
932 x :: int
933 r0 :: str
934 r1 :: object
935 r2 :: object[2]
936 r3 :: object_ptr
937 r4 :: object
938 r5 :: int
939 L0:
940 r0 = 'm'
941 inc_ref x :: int
942 r1 = box(int, x)
943 r2 = [o, r1]
944 r3 = load_address r2
945 r4 = PyObject_VectorcallMethod(r0, r3, 9223372036854775810, 0)
946 dec_ref r1
947 r5 = unbox(int, r4)
948 dec_ref r4
949 return r5
950
951 [case testBorrowAttribute]
952 def g() -> int:
953 d = D()
954 return d.c.x
955
956 def f(d: D) -> int:
957 return d.c.x
958
959 class C:
960 x: int
961 class D:
962 c: C
963 [out]
964 def g():
965 r0, d :: __main__.D
966 r1 :: __main__.C
967 r2 :: int
968 L0:
969 r0 = D()
970 d = r0
971 r1 = borrow d.c
972 r2 = r1.x
973 dec_ref d
974 return r2
975 def f(d):
976 d :: __main__.D
977 r0 :: __main__.C
978 r1 :: int
979 L0:
980 r0 = borrow d.c
981 r1 = r0.x
982 return r1
983
984 [case testBorrowAttributeTwice]
985 def f(e: E) -> int:
986 return e.d.c.x
987
988 class C:
989 x: int
990 class D:
991 c: C
992 class E:
993 d: D
994 [out]
995 def f(e):
996 e :: __main__.E
997 r0 :: __main__.D
998 r1 :: __main__.C
999 r2 :: int
1000 L0:
1001 r0 = borrow e.d
1002 r1 = borrow r0.c
1003 r2 = r1.x
1004 return r2
1005
1006 [case testBorrowAttributeIsNone]
1007 from typing import Optional
1008
1009 def f(c: C) -> bool:
1010 return c.x is not None
1011
1012 def g(c: C) -> bool:
1013 return c.x is None
1014
1015 class C:
1016 x: Optional[str]
1017 [out]
1018 def f(c):
1019 c :: __main__.C
1020 r0 :: union[str, None]
1021 r1 :: object
1022 r2 :: bit
1023 L0:
1024 r0 = borrow c.x
1025 r1 = load_address _Py_NoneStruct
1026 r2 = r0 != r1
1027 return r2
1028 def g(c):
1029 c :: __main__.C
1030 r0 :: union[str, None]
1031 r1 :: object
1032 r2 :: bit
1033 L0:
1034 r0 = borrow c.x
1035 r1 = load_address _Py_NoneStruct
1036 r2 = r0 == r1
1037 return r2
1038
1039 [case testBorrowAttributeNarrowOptional]
1040 from typing import Optional
1041
1042 def f(c: C) -> bool:
1043 if c.x is not None:
1044 return c.x.b
1045 return False
1046
1047 class C:
1048 x: Optional[D]
1049
1050 class D:
1051 b: bool
1052 [out]
1053 def f(c):
1054 c :: __main__.C
1055 r0 :: union[__main__.D, None]
1056 r1 :: object
1057 r2 :: bit
1058 r3 :: union[__main__.D, None]
1059 r4 :: __main__.D
1060 r5 :: bool
1061 L0:
1062 r0 = borrow c.x
1063 r1 = load_address _Py_NoneStruct
1064 r2 = r0 != r1
1065 if r2 goto L1 else goto L2 :: bool
1066 L1:
1067 r3 = borrow c.x
1068 r4 = borrow cast(__main__.D, r3)
1069 r5 = r4.b
1070 return r5
1071 L2:
1072 return 0
1073
1074 [case testBorrowLenArgument]
1075 from typing import List
1076
1077 def f(x: C) -> int:
1078 return len(x.a)
1079
1080 class C:
1081 a: List[str]
1082 [out]
1083 def f(x):
1084 x :: __main__.C
1085 r0 :: list
1086 r1 :: native_int
1087 r2 :: short_int
1088 L0:
1089 r0 = borrow x.a
1090 r1 = var_object_size r0
1091 r2 = r1 << 1
1092 return r2
1093
1094 [case testBorrowIsinstanceArgument]
1095 from typing import List
1096
1097 def f(x: C) -> bool:
1098 if isinstance(x.a, D):
1099 return x.a.b
1100 else:
1101 return True
1102
1103 class C:
1104 a: object
1105
1106 class D:
1107 b: bool
1108 [out]
1109 def f(x):
1110 x :: __main__.C
1111 r0, r1 :: object
1112 r2 :: ptr
1113 r3 :: object
1114 r4 :: bit
1115 r5 :: object
1116 r6 :: __main__.D
1117 r7 :: bool
1118 L0:
1119 r0 = borrow x.a
1120 r1 = __main__.D :: type
1121 r2 = get_element_ptr r0 ob_type :: PyObject
1122 r3 = borrow load_mem r2 :: builtins.object*
1123 r4 = r3 == r1
1124 if r4 goto L1 else goto L2 :: bool
1125 L1:
1126 r5 = borrow x.a
1127 r6 = borrow cast(__main__.D, r5)
1128 r7 = r6.b
1129 return r7
1130 L2:
1131 return 1
1132
1133 [case testBorrowListGetItem1]
1134 from typing import List
1135
1136 def literal_index(x: C) -> str:
1137 return x.a[0]
1138
1139 def negative_index(x: C) -> str:
1140 return x.a[-1]
1141
1142 def lvar_index(x: C, n: int) -> str:
1143 return x.a[n]
1144
1145 class C:
1146 a: List[str]
1147
1148 [out]
1149 def literal_index(x):
1150 x :: __main__.C
1151 r0 :: list
1152 r1 :: object
1153 r2 :: str
1154 L0:
1155 r0 = borrow x.a
1156 r1 = CPyList_GetItemShort(r0, 0)
1157 r2 = cast(str, r1)
1158 return r2
1159 def negative_index(x):
1160 x :: __main__.C
1161 r0 :: list
1162 r1 :: object
1163 r2 :: str
1164 L0:
1165 r0 = borrow x.a
1166 r1 = CPyList_GetItemShort(r0, -2)
1167 r2 = cast(str, r1)
1168 return r2
1169 def lvar_index(x, n):
1170 x :: __main__.C
1171 n :: int
1172 r0 :: list
1173 r1 :: object
1174 r2 :: str
1175 L0:
1176 r0 = borrow x.a
1177 r1 = CPyList_GetItem(r0, n)
1178 r2 = cast(str, r1)
1179 return r2
1180
1181 [case testBorrowListGetItem2]
1182 from typing import List
1183
1184 def attr_before_index(x: C) -> str:
1185 return x.a[x.n]
1186
1187 def attr_after_index(a: List[C], i: int) -> int:
1188 return a[i].n
1189
1190 def attr_after_index_literal(a: List[C]) -> int:
1191 return a[0].n
1192
1193 class C:
1194 a: List[str]
1195 n: int
1196 [out]
1197 def attr_before_index(x):
1198 x :: __main__.C
1199 r0 :: list
1200 r1 :: int
1201 r2 :: object
1202 r3 :: str
1203 L0:
1204 r0 = borrow x.a
1205 r1 = borrow x.n
1206 r2 = CPyList_GetItem(r0, r1)
1207 r3 = cast(str, r2)
1208 return r3
1209 def attr_after_index(a, i):
1210 a :: list
1211 i :: int
1212 r0 :: object
1213 r1 :: __main__.C
1214 r2 :: int
1215 L0:
1216 r0 = CPyList_GetItemBorrow(a, i)
1217 r1 = borrow cast(__main__.C, r0)
1218 r2 = r1.n
1219 return r2
1220 def attr_after_index_literal(a):
1221 a :: list
1222 r0 :: object
1223 r1 :: __main__.C
1224 r2 :: int
1225 L0:
1226 r0 = CPyList_GetItemShortBorrow(a, 0)
1227 r1 = borrow cast(__main__.C, r0)
1228 r2 = r1.n
1229 return r2
1230
1231 [case testCannotBorrowListGetItem]
1232 from typing import List
1233
1234 def func_index(x: C) -> str:
1235 return x.a[f()]
1236
1237 def f() -> int: return 0
1238
1239 class C:
1240 a: List[str]
1241 [out]
1242 def func_index(x):
1243 x :: __main__.C
1244 r0 :: list
1245 r1 :: int
1246 r2 :: object
1247 r3 :: str
1248 L0:
1249 r0 = x.a
1250 r1 = f()
1251 r2 = CPyList_GetItem(r0, r1)
1252 dec_ref r0
1253 dec_ref r1 :: int
1254 r3 = cast(str, r2)
1255 return r3
1256 def f():
1257 L0:
1258 return 0
1259
1260 [case testBorrowListGetItemKeepAlive]
1261 from typing import List
1262
1263 def f() -> str:
1264 a = [C()]
1265 return a[0].s
1266
1267 class C:
1268 s: str
1269 [out]
1270 def f():
1271 r0 :: __main__.C
1272 r1 :: list
1273 r2 :: ptr
1274 a :: list
1275 r3 :: object
1276 r4 :: __main__.C
1277 r5 :: str
1278 L0:
1279 r0 = C()
1280 r1 = PyList_New(1)
1281 r2 = list_items r1
1282 buf_init_item r2, 0, r0
1283 a = r1
1284 r3 = CPyList_GetItemShortBorrow(a, 0)
1285 r4 = borrow cast(__main__.C, r3)
1286 r5 = r4.s
1287 dec_ref a
1288 return r5
1289
1290 [case testBorrowSetAttrObject]
1291 from typing import Optional
1292
1293 def f(x: Optional[C]) -> None:
1294 if x is not None:
1295 x.b = True
1296
1297 def g(x: D) -> None:
1298 x.c.b = False
1299
1300 class C:
1301 b: bool
1302
1303 class D:
1304 c: C
1305 [out]
1306 def f(x):
1307 x :: union[__main__.C, None]
1308 r0 :: object
1309 r1 :: bit
1310 r2 :: __main__.C
1311 r3 :: bool
1312 L0:
1313 r0 = load_address _Py_NoneStruct
1314 r1 = x != r0
1315 if r1 goto L1 else goto L2 :: bool
1316 L1:
1317 r2 = borrow cast(__main__.C, x)
1318 r2.b = 1; r3 = is_error
1319 L2:
1320 return 1
1321 def g(x):
1322 x :: __main__.D
1323 r0 :: __main__.C
1324 r1 :: bool
1325 L0:
1326 r0 = borrow x.c
1327 r0.b = 0; r1 = is_error
1328 return 1
1329
1330 [case testBorrowIntEquality]
1331 def add(c: C) -> bool:
1332 return c.x == c.y
1333
1334 class C:
1335 x: int
1336 y: int
1337 [out]
1338 def add(c):
1339 c :: __main__.C
1340 r0, r1 :: int
1341 r2 :: bit
1342 L0:
1343 r0 = borrow c.x
1344 r1 = borrow c.y
1345 r2 = int_eq r0, r1
1346 return r2
1347
1348 [case testBorrowIntLessThan]
1349 def add(c: C) -> bool:
1350 return c.x < c.y
1351
1352 class C:
1353 x: int
1354 y: int
1355 [out]
1356 def add(c):
1357 c :: __main__.C
1358 r0, r1 :: int
1359 r2 :: bit
1360 L0:
1361 r0 = borrow c.x
1362 r1 = borrow c.y
1363 r2 = int_lt r0, r1
1364 return r2
1365
1366 [case testBorrowIntCompareFinal]
1367 from typing import Final
1368
1369 X: Final = 10
1370
1371 def add(c: C) -> bool:
1372 return c.x == X
1373
1374 class C:
1375 x: int
1376 [out]
1377 def add(c):
1378 c :: __main__.C
1379 r0 :: int
1380 r1 :: bit
1381 L0:
1382 r0 = borrow c.x
1383 r1 = int_eq r0, 20
1384 return r1
1385
1386 [case testBorrowIntArithmetic]
1387 def add(c: C) -> int:
1388 return c.x + c.y
1389
1390 def sub(c: C) -> int:
1391 return c.x - c.y
1392
1393 class C:
1394 x: int
1395 y: int
1396 [out]
1397 def add(c):
1398 c :: __main__.C
1399 r0, r1, r2 :: int
1400 L0:
1401 r0 = borrow c.x
1402 r1 = borrow c.y
1403 r2 = CPyTagged_Add(r0, r1)
1404 return r2
1405 def sub(c):
1406 c :: __main__.C
1407 r0, r1, r2 :: int
1408 L0:
1409 r0 = borrow c.x
1410 r1 = borrow c.y
1411 r2 = CPyTagged_Subtract(r0, r1)
1412 return r2
1413
1414 [case testBorrowIntComparisonInIf]
1415 def add(c: C, n: int) -> bool:
1416 if c.x == c.y:
1417 return True
1418 return False
1419
1420 class C:
1421 x: int
1422 y: int
1423 [out]
1424 def add(c, n):
1425 c :: __main__.C
1426 n, r0, r1 :: int
1427 r2 :: bit
1428 L0:
1429 r0 = borrow c.x
1430 r1 = borrow c.y
1431 r2 = int_eq r0, r1
1432 if r2 goto L1 else goto L2 :: bool
1433 L1:
1434 return 1
1435 L2:
1436 return 0
1437
1438 [case testBorrowIntInPlaceOp]
1439 def add(c: C, n: int) -> None:
1440 c.x += n
1441
1442 def sub(c: C, n: int) -> None:
1443 c.x -= c.y
1444
1445 class C:
1446 x: int
1447 y: int
1448 [out]
1449 def add(c, n):
1450 c :: __main__.C
1451 n, r0, r1 :: int
1452 r2 :: bool
1453 L0:
1454 r0 = borrow c.x
1455 r1 = CPyTagged_Add(r0, n)
1456 c.x = r1; r2 = is_error
1457 return 1
1458 def sub(c, n):
1459 c :: __main__.C
1460 n, r0, r1, r2 :: int
1461 r3 :: bool
1462 L0:
1463 r0 = borrow c.x
1464 r1 = borrow c.y
1465 r2 = CPyTagged_Subtract(r0, r1)
1466 c.x = r2; r3 = is_error
1467 return 1
1468
1469 [case testCoerceIntToI64_64bit]
1470 from mypy_extensions import i64
1471
1472 def f(x: int) -> i64:
1473 # TODO: On the fast path we shouldn't have a decref. Once we have high-level IR,
1474 # coercion from int to i64 can be a single op, which makes it easier to
1475 # generate optimal refcount handling for this case.
1476 return x + 1
1477 [out]
1478 def f(x):
1479 x, r0 :: int
1480 r1 :: native_int
1481 r2 :: bit
1482 r3, r4 :: i64
1483 r5 :: ptr
1484 r6 :: c_ptr
1485 r7 :: i64
1486 L0:
1487 r0 = CPyTagged_Add(x, 2)
1488 r1 = r0 & 1
1489 r2 = r1 == 0
1490 if r2 goto L1 else goto L2 :: bool
1491 L1:
1492 r3 = r0 >> 1
1493 dec_ref r0 :: int
1494 r4 = r3
1495 goto L3
1496 L2:
1497 r5 = r0 ^ 1
1498 r6 = r5
1499 r7 = CPyLong_AsInt64(r6)
1500 r4 = r7
1501 dec_ref r0 :: int
1502 L3:
1503 return r4
1504
1505 [case testVecTSetItem_64bit]
1506 from librt.vecs import vec
1507 from mypy_extensions import i64
1508
1509 def f(v: vec[str], i: i64, x: str) -> None:
1510 v[i] = x
1511 [out]
1512 def f(v, i, x):
1513 v :: vec[str]
1514 i :: i64
1515 x :: str
1516 r0 :: native_int
1517 r1 :: bit
1518 r2 :: i64
1519 r3 :: bit
1520 r4 :: bool
1521 r5 :: i64
1522 r6 :: ptr
1523 r7 :: i64
1524 r8 :: ptr
1525 r9 :: str
1526 L0:
1527 r0 = v.len
1528 r1 = i < r0 :: unsigned
1529 if r1 goto L4 else goto L1 :: bool
1530 L1:
1531 r2 = i + r0
1532 r3 = r2 < r0 :: unsigned
1533 if r3 goto L3 else goto L2 :: bool
1534 L2:
1535 r4 = raise IndexError
1536 unreachable
1537 L3:
1538 r5 = r2
1539 goto L5
1540 L4:
1541 r5 = i
1542 L5:
1543 r6 = v.items
1544 r7 = r5 * 8
1545 r8 = r6 + r7
1546 r9 = borrow load_mem r8 :: builtins.str*
1547 dec_ref r9
1548 inc_ref x
1549 set_mem r8, x :: builtins.str*
1550 return 1
1551
1552 [case testVecTConstructFromListMultiply_64bit]
1553 from typing import Optional
1554 from librt.vecs import vec
1555 from mypy_extensions import i64
1556
1557 def f(n: i64) -> vec[Optional[str]]:
1558 return vec[Optional[str]]([None] * n)
1559 [out]
1560 def f(n):
1561 n :: i64
1562 r0, r1 :: object
1563 r2, r3 :: ptr
1564 r4 :: vec[str | None]
1565 r5 :: ptr
1566 r6 :: i64
1567 r7, r8 :: ptr
1568 r9 :: bit
1569 r10 :: ptr
1570 L0:
1571 r0 = box(None, 1)
1572 r1 = load_address PyUnicode_Type
1573 r2 = r1
1574 r3 = r2 | 1
1575 r4 = VecTApi.alloc(n, n, r3)
1576 r5 = r4.items
1577 r6 = n * 8
1578 r7 = r5 + r6
1579 r8 = r5
1580 L1:
1581 r9 = r8 < r7 :: unsigned
1582 if r9 goto L2 else goto L3 :: bool
1583 L2:
1584 inc_ref r0
1585 set_mem r8, r0 :: union*
1586 r10 = r8 + 8
1587 r8 = r10
1588 goto L1
1589 L3:
1590 return r4
1591
1592 [case testVecTForLoop_64bit]
1593 from librt.vecs import vec
1594 from mypy_extensions import i64
1595
1596 def f(v: vec[str]) -> i64:
1597 t: i64 = 0
1598 for s in v:
1599 g(s)
1600 return t
1601
1602 def g(s: str) -> None: pass
1603 [out]
1604 def f(v):
1605 v :: vec[str]
1606 t :: i64
1607 r0, r1 :: native_int
1608 r2 :: bit
1609 r3 :: ptr
1610 r4 :: native_int
1611 r5 :: ptr
1612 r6, s :: str
1613 r7 :: None
1614 r8 :: native_int
1615 L0:
1616 t = 0
1617 r0 = 0
1618 L1:
1619 r1 = v.len
1620 r2 = r0 < r1 :: signed
1621 if r2 goto L2 else goto L4 :: bool
1622 L2:
1623 r3 = v.items
1624 r4 = r0 * 8
1625 r5 = r3 + r4
1626 r6 = load_mem r5 :: builtins.str*
1627 s = r6
1628 r7 = g(s)
1629 dec_ref s
1630 L3:
1631 r8 = r0 + 1
1632 r0 = r8
1633 goto L1
1634 L4:
1635 return t
1636 def g(s):
1637 s :: str
1638 L0:
1639 return 1
1640
1641 [case testVecTConstructFromListExpr_64bit]
1642 from librt.vecs import vec
1643
1644 def f() -> vec[str]:
1645 return vec[str](['x', 'y'])
1646 [out]
1647 def f():
1648 r0, r1 :: str
1649 r2 :: object
1650 r3 :: ptr
1651 r4 :: vec[str]
1652 r5, r6, r7 :: ptr
1653 L0:
1654 r0 = 'x'
1655 r1 = 'y'
1656 r2 = load_address PyUnicode_Type
1657 r3 = r2
1658 r4 = VecTApi.alloc(2, 2, r3)
1659 r5 = r4.items
1660 inc_ref r0
1661 set_mem r5, r0 :: builtins.str*
1662 r6 = r5 + 8
1663 inc_ref r1
1664 set_mem r6, r1 :: builtins.str*
1665 r7 = r6 + 8
1666 return r4
1667
1668 [case testVecI64GetItemBorrowVec_64bit]
1669 from librt.vecs import vec
1670 from mypy_extensions import i64
1671
1672 class C:
1673 v: vec[i64]
1674
1675 def f(self, x: i64) -> i64:
1676 return self.v[x]
1677 [out]
1678 def C.f(self, x):
1679 self :: __main__.C
1680 x :: i64
1681 r0 :: vec[i64]
1682 r1 :: native_int
1683 r2 :: bit
1684 r3 :: i64
1685 r4 :: bit
1686 r5 :: bool
1687 r6 :: i64
1688 r7 :: ptr
1689 r8 :: i64
1690 r9 :: ptr
1691 r10 :: i64
1692 L0:
1693 r0 = borrow self.v
1694 r1 = r0.len
1695 r2 = x < r1 :: unsigned
1696 if r2 goto L4 else goto L1 :: bool
1697 L1:
1698 r3 = x + r1
1699 r4 = r3 < r1 :: unsigned
1700 if r4 goto L3 else goto L2 :: bool
1701 L2:
1702 r5 = raise IndexError
1703 unreachable
1704 L3:
1705 r6 = r3
1706 goto L5
1707 L4:
1708 r6 = x
1709 L5:
1710 r7 = r0.items
1711 r8 = r6 * 8
1712 r9 = r7 + r8
1713 r10 = load_mem r9 :: i64*
1714 return r10
1715
1716 [case testVecI64LenBorrowVec_64bit]
1717 from librt.vecs import vec
1718 from mypy_extensions import i64
1719
1720 class C:
1721 v: vec[i64]
1722
1723 def f(self, x: i64) -> i64:
1724 return len(self.v)
1725 [out]
1726 def C.f(self, x):
1727 self :: __main__.C
1728 x :: i64
1729 r0 :: vec[i64]
1730 r1 :: native_int
1731 L0:
1732 r0 = borrow self.v
1733 r1 = r0.len
1734 return r1
1735
1736 [case testVecNestedAppend]
1737 from librt.vecs import vec, append
1738
1739 def f(vv: vec[vec[str]], v: vec[str]) -> vec[vec[str]]:
1740 return append(vv, v)
1741 [out]
1742 def f(vv, v):
1743 vv :: vec[vec[str]]
1744 v :: vec[str]
1745 r0 :: native_int
1746 r1 :: ptr
1747 r2, r3 :: VecNestedBufItem{len:native_int, items:ptr}
1748 r4 :: vec[vec[str]]
1749 L0:
1750 r0 = v.len
1751 r1 = v.items
1752 r2 = set_element undef VecNestedBufItem, len, r0
1753 r3 = set_element r2, items, r1
1754 inc_ref vv
1755 r4 = VecNestedApi.append(vv, r3)
1756 return r4
1757
1758 [case testVecTGetItem_64bit]
1759 from librt.vecs import vec
1760 from mypy_extensions import i64
1761
1762 def f(v: vec[str], n: i64) -> str:
1763 return v[n]
1764 [out]
1765 def f(v, n):
1766 v :: vec[str]
1767 n :: i64
1768 r0 :: native_int
1769 r1 :: bit
1770 r2 :: i64
1771 r3 :: bit
1772 r4 :: bool
1773 r5 :: i64
1774 r6 :: ptr
1775 r7 :: i64
1776 r8 :: ptr
1777 r9 :: str
1778 L0:
1779 r0 = v.len
1780 r1 = n < r0 :: unsigned
1781 if r1 goto L4 else goto L1 :: bool
1782 L1:
1783 r2 = n + r0
1784 r3 = r2 < r0 :: unsigned
1785 if r3 goto L3 else goto L2 :: bool
1786 L2:
1787 r4 = raise IndexError
1788 unreachable
1789 L3:
1790 r5 = r2
1791 goto L5
1792 L4:
1793 r5 = n
1794 L5:
1795 r6 = v.items
1796 r7 = r5 * 8
1797 r8 = r6 + r7
1798 r9 = load_mem r8 :: builtins.str*
1799 return r9
1800
1801 [case testVecNestedGetItem_64bit]
1802 from librt.vecs import vec
1803 from mypy_extensions import i64
1804
1805 def f(v: vec[vec[str]], n: i64) -> None:
1806 vv = vec[vec[str]]()[n]
1807 [out]
1808 def f(v, n):
1809 v :: vec[vec[str]]
1810 n :: i64
1811 r0 :: object
1812 r1 :: ptr
1813 r2 :: vec[vec[str]]
1814 r3 :: native_int
1815 r4 :: bit
1816 r5 :: i64
1817 r6 :: bit
1818 r7 :: bool
1819 r8 :: i64
1820 r9 :: ptr
1821 r10 :: i64
1822 r11 :: ptr
1823 r12, vv :: vec[str]
1824 L0:
1825 r0 = load_address PyUnicode_Type
1826 r1 = r0
1827 r2 = VecNestedApi.alloc(0, 0, r1, 1)
1828 r3 = r2.len
1829 r4 = n < r3 :: unsigned
1830 if r4 goto L4 else goto L1 :: bool
1831 L1:
1832 r5 = n + r3
1833 r6 = r5 < r3 :: unsigned
1834 if r6 goto L3 else goto L6 :: bool
1835 L2:
1836 r7 = raise IndexError
1837 unreachable
1838 L3:
1839 r8 = r5
1840 goto L5
1841 L4:
1842 r8 = n
1843 L5:
1844 r9 = r2.items
1845 r10 = r8 * 16
1846 r11 = r9 + r10
1847 r12 = load_mem r11 :: vec[str]*
1848 dec_ref r2
1849 vv = r12
1850 dec_ref vv
1851 return 1
1852 L6:
1853 dec_ref r2
1854 goto L2
1855
1856 [case testVecNestedGetItemBorrow_64bit]
1857 from librt.vecs import vec
1858 from mypy_extensions import i64
1859
1860 def f(v: vec[vec[i64]], n: i64, m: i64) -> i64:
1861 return v[n][m]
1862 [out]
1863 def f(v, n, m):
1864 v :: vec[vec[i64]]
1865 n, m :: i64
1866 r0 :: native_int
1867 r1 :: bit
1868 r2 :: i64
1869 r3 :: bit
1870 r4 :: bool
1871 r5 :: i64
1872 r6 :: ptr
1873 r7 :: i64
1874 r8 :: ptr
1875 r9 :: vec[i64]
1876 r10 :: native_int
1877 r11 :: bit
1878 r12 :: i64
1879 r13 :: bit
1880 r14 :: bool
1881 r15 :: i64
1882 r16 :: ptr
1883 r17 :: i64
1884 r18 :: ptr
1885 r19 :: i64
1886 L0:
1887 r0 = v.len
1888 r1 = n < r0 :: unsigned
1889 if r1 goto L4 else goto L1 :: bool
1890 L1:
1891 r2 = n + r0
1892 r3 = r2 < r0 :: unsigned
1893 if r3 goto L3 else goto L2 :: bool
1894 L2:
1895 r4 = raise IndexError
1896 unreachable
1897 L3:
1898 r5 = r2
1899 goto L5
1900 L4:
1901 r5 = n
1902 L5:
1903 r6 = v.items
1904 r7 = r5 * 16
1905 r8 = r6 + r7
1906 r9 = borrow load_mem r8 :: vec[i64]*
1907 r10 = r9.len
1908 r11 = m < r10 :: unsigned
1909 if r11 goto L9 else goto L6 :: bool
1910 L6:
1911 r12 = m + r10
1912 r13 = r12 < r10 :: unsigned
1913 if r13 goto L8 else goto L7 :: bool
1914 L7:
1915 r14 = raise IndexError
1916 unreachable
1917 L8:
1918 r15 = r12
1919 goto L10
1920 L9:
1921 r15 = m
1922 L10:
1923 r16 = r9.items
1924 r17 = r15 * 8
1925 r18 = r16 + r17
1926 r19 = load_mem r18 :: i64*
1927 return r19
1928
1929 [case testVecPop]
1930 from librt.vecs import vec, pop, append
1931 from mypy_extensions import i64
1932
1933 def f() -> vec[i64]:
1934 v = vec[i64]()
1935 v = append(v, 7)
1936 v, x = pop(v)
1937 return v
1938 [out]
1939 def f():
1940 r0, v, r1 :: vec[i64]
1941 r2 :: tuple[vec[i64], i64]
1942 r3 :: vec[i64]
1943 r4 :: i64
1944 r5 :: vec[i64]
1945 r6, x :: i64
1946 L0:
1947 r0 = VecI64Api.alloc(0, 0)
1948 v = r0
1949 r1 = VecI64Api.append(v, 7)
1950 v = r1
1951 r2 = VecI64Api.pop(v, -1)
1952 r3 = borrow r2[0]
1953 r4 = borrow r2[1]
1954 r5 = unborrow r3
1955 v = r5
1956 r6 = unborrow r4
1957 x = r6
1958 return v
1959
1960 [case testVecNestedPop]
1961 from librt.vecs import vec, pop
1962
1963 def f(v: vec[vec[str]]) -> vec[str]:
1964 vv, x = pop(v)
1965 return x
1966 [out]
1967 def f(v):
1968 v :: vec[vec[str]]
1969 r0 :: tuple[vec[vec[str]], VecNestedBufItem{len:native_int, items:ptr}]
1970 r1, r2 :: vec[vec[str]]
1971 r3, r4 :: VecNestedBufItem{len:native_int, items:ptr}
1972 r5 :: native_int
1973 r6 :: ptr
1974 r7, r8 :: vec[str]
1975 r9 :: tuple[vec[vec[str]], vec[str]]
1976 r10 :: vec[vec[str]]
1977 r11 :: vec[str]
1978 r12, vv :: vec[vec[str]]
1979 r13, x :: vec[str]
1980 L0:
1981 inc_ref v
1982 r0 = VecNestedApi.pop(v, -1)
1983 r1 = borrow r0[0]
1984 r2 = unborrow r1
1985 r3 = borrow r0[1]
1986 r4 = unborrow r3
1987 r5 = r4.len
1988 r6 = r4.items
1989 r7 = set_element undef vec[str], len, r5
1990 r8 = set_element r7, items, r6
1991 r9 = (r2, r8)
1992 r10 = borrow r9[0]
1993 r11 = borrow r9[1]
1994 r12 = unborrow r10
1995 vv = r12
1996 dec_ref vv
1997 r13 = unborrow r11
1998 x = r13
1999 return x
2000
2001 [case testPropertySetterCallWithRefcountedObject]
2002 class Foo:
2003 def __init__(self) -> None:
2004 self.attr = "unmodified"
2005
2006 class A:
2007 def __init__(self) -> None:
2008 self._foo = Foo()
2009
2010 @property
2011 def foo(self) -> Foo:
2012 return self._foo
2013
2014 @foo.setter
2015 def foo(self, val : Foo) -> None:
2016 self._foo = val
2017
2018 def f(a: A):
2019 a.foo = Foo()
2020 [out]
2021 def Foo.__init__(self):
2022 self :: __main__.Foo
2023 r0 :: str
2024 L0:
2025 r0 = 'unmodified'
2026 inc_ref r0
2027 self.attr = r0
2028 return 1
2029 def A.__init__(self):
2030 self :: __main__.A
2031 r0 :: __main__.Foo
2032 L0:
2033 r0 = Foo()
2034 self._foo = r0
2035 return 1
2036 def A.foo(self):
2037 self :: __main__.A
2038 r0 :: __main__.Foo
2039 L0:
2040 r0 = self._foo
2041 return r0
2042 def A.__mypyc_setter__foo(self, val):
2043 self :: __main__.A
2044 val :: __main__.Foo
2045 r0 :: bool
2046 L0:
2047 inc_ref val
2048 self._foo = val; r0 = is_error
2049 return 1
2050 def f(a):
2051 a :: __main__.A
2052 r0 :: __main__.Foo
2053 r1 :: bool
2054 r2 :: object
2055 L0:
2056 r0 = Foo()
2057 a.foo = r0; r1 = is_error
2058 dec_ref r0
2059 r2 = box(None, 1)
2060 inc_ref r2
2061 return r2
2062
2063 [case testPropertySetterCallWithNonRefcountedObject_64bit]
2064 from mypy_extensions import i64
2065
2066 class A:
2067 def __init__(self) -> None:
2068 self._foo = i64(0)
2069
2070 @property
2071 def foo(self) -> i64:
2072 return self._foo
2073
2074 @foo.setter
2075 def foo(self, val : i64) -> None:
2076 self._foo = val
2077
2078 def f(a: A, i: int):
2079 a.foo = i64(i)
2080 [out]
2081 def A.__init__(self):
2082 self :: __main__.A
2083 L0:
2084 self._foo = 0
2085 return 1
2086 def A.foo(self):
2087 self :: __main__.A
2088 r0 :: i64
2089 L0:
2090 r0 = self._foo
2091 return r0
2092 def A.__mypyc_setter__foo(self, val):
2093 self :: __main__.A
2094 val :: i64
2095 r0 :: bool
2096 L0:
2097 self._foo = val; r0 = is_error
2098 return 1
2099 def f(a, i):
2100 a :: __main__.A
2101 i :: int
2102 r0 :: native_int
2103 r1 :: bit
2104 r2, r3 :: i64
2105 r4 :: ptr
2106 r5 :: c_ptr
2107 r6 :: i64
2108 r7 :: bool
2109 r8 :: object
2110 L0:
2111 r0 = i & 1
2112 r1 = r0 == 0
2113 if r1 goto L1 else goto L2 :: bool
2114 L1:
2115 r2 = i >> 1
2116 r3 = r2
2117 goto L3
2118 L2:
2119 r4 = i ^ 1
2120 r5 = r4
2121 r6 = CPyLong_AsInt64(r5)
2122 r3 = r6
2123 L3:
2124 a.foo = r3; r7 = is_error
2125 r8 = box(None, 1)
2126 inc_ref r8
2127 return r8
2128
2129 [case testBytesConcatRefcount]
2130 def f(a: bytes, b: bytes) -> bytes:
2131 return b"1" + a + b
2132 [out]
2133 def f(a, b):
2134 a, b, r0, r1, r2 :: bytes
2135 L0:
2136 r0 = b'1'
2137 r1 = CPyBytes_Concat(r0, a)
2138 r2 = CPyBytes_Concat(r1, b)
2139 dec_ref r1
2140 return r2
2141
2142 [case testChainedBytesConcatRefcount]
2143 def f(a: bytes, b: bytes, c: bytes) -> bytes:
2144 return b"1" + a + b + c
2145 [out]
2146 def f(a, b, c):
2147 a, b, c, r0, r1, r2, r3 :: bytes
2148 L0:
2149 r0 = b'1'
2150 r1 = CPyBytes_Concat(r0, a)
2151 r2 = CPyBytes_Concat(r1, b)
2152 dec_ref r1
2153 r3 = CPyBytes_Concat(r2, c)
2154 dec_ref r2
2155 return r3