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 testBorrowListObjectWithLiteralIndex_nogil]
596 from typing import cast
597
598 def f() -> str:
599 o = cast(object, [])
600 return cast(list[str], o)[0]
601 [out]
602 def f():
603 r0 :: list
604 o :: object
605 r1 :: list
606 r2 :: object
607 r3 :: str
608 L0:
609 r0 = PyList_New(0)
610 o = r0
611 r1 = borrow cast(list, o)
612 r2 = CPyList_GetItemShort(r1, 0)
613 r3 = cast(str, r2)
614 dec_ref o
615 return r3
616
617 [case testUnaryBranchSpecialCase]
618 def f(x: bool) -> int:
619 if x:
620 return 1
621 return 2
622 [out]
623 def f(x):
624 x :: bool
625 L0:
626 if x goto L1 else goto L2 :: bool
627 L1:
628 return 2
629 L2:
630 return 4
631
632 [case testReturnTuple]
633 from typing import Tuple
634
635 class C: pass
636 def f() -> Tuple[C, C]:
637 a = C()
638 b = C()
639 return a, b
640 [out]
641 def f():
642 r0, a, r1, b, r2, r3 :: __main__.C
643 r4 :: tuple[__main__.C, __main__.C]
644 L0:
645 r0 = C()
646 a = r0
647 r1 = C()
648 b = r1
649 r2 = a
650 r3 = b
651 r4 = (r2, r3)
652 return r4
653
654 [case testDecomposeTuple_withgil]
655 from typing import Tuple
656
657 class C:
658 a: int
659
660 def f() -> int:
661 x, y = g()
662 return x.a + y.a
663
664 def g() -> Tuple[C, C]:
665 return C(), C()
666 [out]
667 def f():
668 r0 :: tuple[__main__.C, __main__.C]
669 r1, r2, r3, x, r4, y :: __main__.C
670 r5, r6, r7 :: int
671 L0:
672 r0 = g()
673 r1 = borrow r0[0]
674 r2 = borrow r0[1]
675 r3 = unborrow r1
676 x = r3
677 r4 = unborrow r2
678 y = r4
679 r5 = borrow x.a
680 r6 = borrow y.a
681 r7 = CPyTagged_Add(r5, r6)
682 dec_ref x
683 dec_ref y
684 return r7
685 def g():
686 r0, r1 :: __main__.C
687 r2 :: tuple[__main__.C, __main__.C]
688 L0:
689 r0 = C()
690 r1 = C()
691 r2 = (r0, r1)
692 return r2
693
694 [case testUnicodeLiteral]
695 def f() -> str:
696 return "some string"
697 [out]
698 def f():
699 r0 :: str
700 L0:
701 r0 = 'some string'
702 inc_ref r0
703 return r0
704
705 [case testPyMethodCall]
706 def g(x: str) -> int:
707 return int(x, base=2)
708 [out]
709 def g(x):
710 x :: str
711 r0, r1 :: object
712 r2 :: object[2]
713 r3 :: object_ptr
714 r4, r5 :: object
715 r6 :: int
716 L0:
717 r0 = load_address PyLong_Type
718 r1 = object 2
719 r2 = [x, r1]
720 r3 = load_address r2
721 r4 = ('base',)
722 r5 = PyObject_Vectorcall(r0, r3, 1, r4)
723 r6 = unbox(int, r5)
724 dec_ref r5
725 return r6
726
727 [case testListAppend]
728 from typing import List
729 def f(a: List[int], x: int) -> None:
730 a.append(x)
731 [out]
732 def f(a, x):
733 a :: list
734 x :: int
735 r0 :: object
736 r1 :: i32
737 r2 :: bit
738 L0:
739 inc_ref x :: int
740 r0 = box(int, x)
741 r1 = PyList_Append(a, r0)
742 dec_ref r0
743 r2 = r1 >= 0 :: signed
744 return 1
745
746 [case testForDict]
747 from typing import Dict
748
749 def f(d: Dict[int, int]) -> None:
750 for key in d:
751 d[key]
752 [out]
753 def f(d):
754 d :: dict
755 r0 :: short_int
756 r1 :: native_int
757 r2 :: object
758 r3 :: tuple[bool, short_int, object]
759 r4 :: short_int
760 r5 :: bool
761 r6 :: object
762 r7, key :: int
763 r8, r9 :: object
764 r10 :: int
765 r11, r12 :: bit
766 L0:
767 r0 = 0
768 r1 = PyDict_Size(d)
769 r2 = CPyDict_GetKeysIter(d)
770 L1:
771 r3 = CPyDict_NextKey(r2, r0)
772 r4 = r3[1]
773 r0 = r4
774 r5 = r3[0]
775 if r5 goto L2 else goto L6 :: bool
776 L2:
777 r6 = r3[2]
778 dec_ref r3
779 r7 = unbox(int, r6)
780 dec_ref r6
781 key = r7
782 r8 = box(int, key)
783 r9 = CPyDict_GetItem(d, r8)
784 dec_ref r8
785 r10 = unbox(int, r9)
786 dec_ref r9
787 dec_ref r10 :: int
788 L3:
789 r11 = CPyDict_CheckSize(d, r1)
790 goto L1
791 L4:
792 r12 = CPy_NoErrOccurred()
793 L5:
794 return 1
795 L6:
796 dec_ref r2
797 dec_ref r3
798 goto L4
799
800 [case testBorrowRefs]
801 def make_garbage(arg: object) -> None:
802 b = True
803 while b:
804 arg = None
805 b = False
806 [out]
807 def make_garbage(arg):
808 arg :: object
809 b :: bool
810 r0 :: object
811 L0:
812 b = 1
813 L1:
814 if b goto L2 else goto L3 :: bool
815 L2:
816 r0 = box(None, 1)
817 inc_ref r0
818 arg = r0
819 dec_ref arg
820 b = 0
821 goto L1
822 L3:
823 return 1
824
825 [case testTupleUnpackUnused]
826 from typing import Tuple
827
828 def f(x: Tuple[str, int]) -> int:
829 a, xi = x
830 return 0
831 [out]
832 def f(x):
833 x :: tuple[str, int]
834 r0 :: str
835 r1 :: int
836 r2, a :: str
837 r3, xi :: int
838 L0:
839 r0 = borrow x[0]
840 r1 = borrow x[1]
841 inc_ref x
842 r2 = unborrow r0
843 a = r2
844 dec_ref a
845 r3 = unborrow r1
846 xi = r3
847 dec_ref xi :: int
848 return 0
849
850 [case testGetElementPtrLifeTime]
851 from typing import List
852
853 def f() -> int:
854 x: List[str] = []
855 return len(x)
856 [out]
857 def f():
858 r0, x :: list
859 r1 :: native_int
860 r2 :: short_int
861 L0:
862 r0 = PyList_New(0)
863 x = r0
864 r1 = var_object_size x
865 dec_ref x
866 r2 = r1 << 1
867 return r2
868
869 [case testSometimesUninitializedVariable]
870 def f(x: bool) -> int:
871 if x:
872 y = 1
873 else:
874 z = 2
875 return y + z
876 [out]
877 def f(x):
878 x :: bool
879 r0, y, r1, z :: int
880 r2, r3 :: bool
881 r4 :: int
882 L0:
883 r0 = <error> :: int
884 y = r0
885 r1 = <error> :: int
886 z = r1
887 if x goto L8 else goto L9 :: bool
888 L1:
889 y = 2
890 goto L3
891 L2:
892 z = 4
893 L3:
894 if is_error(y) goto L10 else goto L5
895 L4:
896 r2 = raise UnboundLocalError('local variable "y" referenced before assignment')
897 unreachable
898 L5:
899 if is_error(z) goto L11 else goto L7
900 L6:
901 r3 = raise UnboundLocalError('local variable "z" referenced before assignment')
902 unreachable
903 L7:
904 r4 = CPyTagged_Add(y, z)
905 xdec_ref y :: int
906 xdec_ref z :: int
907 return r4
908 L8:
909 xdec_ref y :: int
910 goto L1
911 L9:
912 xdec_ref z :: int
913 goto L2
914 L10:
915 xdec_ref z :: int
916 goto L4
917 L11:
918 xdec_ref y :: int
919 goto L6
920
921 [case testVectorcall]
922 from typing import Any
923
924 def call(f: Any, x: int) -> int:
925 return f(x)
926 [out]
927 def call(f, x):
928 f :: object
929 x :: int
930 r0 :: object
931 r1 :: object[1]
932 r2 :: object_ptr
933 r3 :: object
934 r4 :: int
935 L0:
936 inc_ref x :: int
937 r0 = box(int, x)
938 r1 = [r0]
939 r2 = load_address r1
940 r3 = PyObject_Vectorcall(f, r2, 1, 0)
941 dec_ref r0
942 r4 = unbox(int, r3)
943 dec_ref r3
944 return r4
945
946 [case testVectorcallMethod_64bit]
947 from typing import Any
948
949 def call(o: Any, x: int) -> int:
950 return o.m(x)
951 [out]
952 def call(o, x):
953 o :: object
954 x :: int
955 r0 :: str
956 r1 :: object
957 r2 :: object[2]
958 r3 :: object_ptr
959 r4 :: object
960 r5 :: int
961 L0:
962 r0 = 'm'
963 inc_ref x :: int
964 r1 = box(int, x)
965 r2 = [o, r1]
966 r3 = load_address r2
967 r4 = PyObject_VectorcallMethod(r0, r3, 9223372036854775810, 0)
968 dec_ref r1
969 r5 = unbox(int, r4)
970 dec_ref r4
971 return r5
972
973 [case testBorrowAttribute_withgil]
974 def g() -> int:
975 d = D()
976 return d.c.x
977
978 def f(d: D) -> int:
979 return d.c.x
980
981 class C:
982 x: int
983 class D:
984 c: C
985 [out]
986 def g():
987 r0, d :: __main__.D
988 r1 :: __main__.C
989 r2 :: int
990 L0:
991 r0 = D()
992 d = r0
993 r1 = borrow d.c
994 r2 = r1.x
995 dec_ref d
996 return r2
997 def f(d):
998 d :: __main__.D
999 r0 :: __main__.C
1000 r1 :: int
1001 L0:
1002 r0 = borrow d.c
1003 r1 = r0.x
1004 return r1
1005
1006 [case testBorrowAttribute_nogil]
1007 from typing import Final
1008
1009 def g() -> int:
1010 d = D(C(1))
1011 return d.c.x
1012
1013 def f(d: D) -> int:
1014 return d.c.x
1015
1016 class C:
1017 def __init__(self, x: int) -> None:
1018 self.x: Final = x
1019 class D:
1020 def __init__(self, c: C) -> None:
1021 self.c: Final = c
1022 [out]
1023 def g():
1024 r0 :: __main__.C
1025 r1, d :: __main__.D
1026 r2 :: __main__.C
1027 r3 :: int
1028 L0:
1029 r0 = C(2)
1030 r1 = D(r0)
1031 dec_ref r0
1032 d = r1
1033 r2 = borrow d.c
1034 r3 = r2.x
1035 dec_ref d
1036 return r3
1037 def f(d):
1038 d :: __main__.D
1039 r0 :: __main__.C
1040 r1 :: int
1041 L0:
1042 r0 = borrow d.c
1043 r1 = r0.x
1044 return r1
1045 def C.__init__(self, x):
1046 self :: __main__.C
1047 x :: int
1048 L0:
1049 inc_ref x :: int
1050 self.x = x
1051 return 1
1052 def D.__init__(self, c):
1053 self :: __main__.D
1054 c :: __main__.C
1055 L0:
1056 inc_ref c
1057 self.c = c
1058 return 1
1059
1060 [case testBorrowInheritedFinalAttribute_nogil]
1061 from typing import Final
1062
1063 def f(d: D) -> int:
1064 return d.c.x
1065
1066 class B:
1067 def __init__(self, x: int) -> None:
1068 self.x: Final = x
1069 class C(B):
1070 pass
1071 class D:
1072 def __init__(self, c: C) -> None:
1073 self.c: Final = c
1074 [out]
1075 def f(d):
1076 d :: __main__.D
1077 r0 :: __main__.C
1078 r1 :: int
1079 L0:
1080 r0 = borrow d.c
1081 r1 = r0.x
1082 return r1
1083 def B.__init__(self, x):
1084 self :: __main__.B
1085 x :: int
1086 L0:
1087 inc_ref x :: int
1088 self.x = x
1089 return 1
1090 def D.__init__(self, c):
1091 self :: __main__.D
1092 c :: __main__.C
1093 L0:
1094 inc_ref c
1095 self.c = c
1096 return 1
1097
1098 [case testBorrowAttributeTwice_withgil]
1099 def f(e: E) -> int:
1100 return e.d.c.x
1101
1102 class C:
1103 x: int
1104 class D:
1105 c: C
1106 class E:
1107 d: D
1108 [out]
1109 def f(e):
1110 e :: __main__.E
1111 r0 :: __main__.D
1112 r1 :: __main__.C
1113 r2 :: int
1114 L0:
1115 r0 = borrow e.d
1116 r1 = borrow r0.c
1117 r2 = r1.x
1118 return r2
1119
1120 [case testBorrowAttributeIsNone_withgil]
1121 from typing import Optional
1122
1123 def f(c: C) -> bool:
1124 return c.x is not None
1125
1126 def g(c: C) -> bool:
1127 return c.x is None
1128
1129 class C:
1130 x: Optional[str]
1131 [out]
1132 def f(c):
1133 c :: __main__.C
1134 r0 :: union[str, None]
1135 r1 :: object
1136 r2 :: bit
1137 L0:
1138 r0 = borrow c.x
1139 r1 = load_address _Py_NoneStruct
1140 r2 = r0 != r1
1141 return r2
1142 def g(c):
1143 c :: __main__.C
1144 r0 :: union[str, None]
1145 r1 :: object
1146 r2 :: bit
1147 L0:
1148 r0 = borrow c.x
1149 r1 = load_address _Py_NoneStruct
1150 r2 = r0 == r1
1151 return r2
1152
1153 [case testBorrowAttributeNarrowOptional_withgil]
1154 from typing import Optional
1155
1156 def f(c: C) -> bool:
1157 if c.x is not None:
1158 return c.x.b
1159 return False
1160
1161 class C:
1162 x: Optional[D]
1163
1164 class D:
1165 b: bool
1166 [out]
1167 def f(c):
1168 c :: __main__.C
1169 r0 :: union[__main__.D, None]
1170 r1 :: object
1171 r2 :: bit
1172 r3 :: union[__main__.D, None]
1173 r4 :: __main__.D
1174 r5 :: bool
1175 L0:
1176 r0 = borrow c.x
1177 r1 = load_address _Py_NoneStruct
1178 r2 = r0 != r1
1179 if r2 goto L1 else goto L2 :: bool
1180 L1:
1181 r3 = borrow c.x
1182 r4 = borrow cast(__main__.D, r3)
1183 r5 = r4.b
1184 return r5
1185 L2:
1186 return 0
1187
1188 [case testBorrowLenArgument_withgil]
1189 from typing import List
1190
1191 def f(x: C) -> int:
1192 return len(x.a)
1193
1194 class C:
1195 a: List[str]
1196 [out]
1197 def f(x):
1198 x :: __main__.C
1199 r0 :: list
1200 r1 :: native_int
1201 r2 :: short_int
1202 L0:
1203 r0 = borrow x.a
1204 r1 = var_object_size r0
1205 r2 = r1 << 1
1206 return r2
1207
1208 [case testBorrowIsinstanceArgument_withgil]
1209 from typing import List
1210
1211 def f(x: C) -> bool:
1212 if isinstance(x.a, D):
1213 return x.a.b
1214 else:
1215 return True
1216
1217 class C:
1218 a: object
1219
1220 class D:
1221 b: bool
1222 [out]
1223 def f(x):
1224 x :: __main__.C
1225 r0, r1 :: object
1226 r2 :: ptr
1227 r3 :: object
1228 r4 :: bit
1229 r5 :: object
1230 r6 :: __main__.D
1231 r7 :: bool
1232 L0:
1233 r0 = borrow x.a
1234 r1 = __main__.D :: type
1235 r2 = get_element_ptr r0 ob_type :: PyObject
1236 r3 = borrow load_mem r2 :: builtins.object*
1237 r4 = r3 == r1
1238 if r4 goto L1 else goto L2 :: bool
1239 L1:
1240 r5 = borrow x.a
1241 r6 = borrow cast(__main__.D, r5)
1242 r7 = r6.b
1243 return r7
1244 L2:
1245 return 1
1246
1247 [case testBorrowListGetItem1_withgil]
1248 from typing import List
1249
1250 def literal_index(x: C) -> str:
1251 return x.a[0]
1252
1253 def negative_index(x: C) -> str:
1254 return x.a[-1]
1255
1256 def lvar_index(x: C, n: int) -> str:
1257 return x.a[n]
1258
1259 class C:
1260 a: List[str]
1261
1262 [out]
1263 def literal_index(x):
1264 x :: __main__.C
1265 r0 :: list
1266 r1 :: object
1267 r2 :: str
1268 L0:
1269 r0 = borrow x.a
1270 r1 = CPyList_GetItemShort(r0, 0)
1271 r2 = cast(str, r1)
1272 return r2
1273 def negative_index(x):
1274 x :: __main__.C
1275 r0 :: list
1276 r1 :: object
1277 r2 :: str
1278 L0:
1279 r0 = borrow x.a
1280 r1 = CPyList_GetItemShort(r0, -2)
1281 r2 = cast(str, r1)
1282 return r2
1283 def lvar_index(x, n):
1284 x :: __main__.C
1285 n :: int
1286 r0 :: list
1287 r1 :: object
1288 r2 :: str
1289 L0:
1290 r0 = borrow x.a
1291 r1 = CPyList_GetItem(r0, n)
1292 r2 = cast(str, r1)
1293 return r2
1294
1295 [case testBorrowListGetItem1_nogil]
1296 from typing import List
1297
1298 def literal_index(x: C) -> str:
1299 return x.a[0]
1300
1301 def negative_index(x: C) -> str:
1302 return x.a[-1]
1303
1304 def lvar_index(x: C, n: int) -> str:
1305 return x.a[n]
1306
1307 class C:
1308 a: List[str]
1309
1310 [out]
1311 def literal_index(x):
1312 x :: __main__.C
1313 r0 :: list
1314 r1 :: object
1315 r2 :: str
1316 L0:
1317 r0 = x.a
1318 r1 = CPyList_GetItemShort(r0, 0)
1319 dec_ref r0
1320 r2 = cast(str, r1)
1321 return r2
1322 def negative_index(x):
1323 x :: __main__.C
1324 r0 :: list
1325 r1 :: object
1326 r2 :: str
1327 L0:
1328 r0 = x.a
1329 r1 = CPyList_GetItemShort(r0, -2)
1330 dec_ref r0
1331 r2 = cast(str, r1)
1332 return r2
1333 def lvar_index(x, n):
1334 x :: __main__.C
1335 n :: int
1336 r0 :: list
1337 r1 :: object
1338 r2 :: str
1339 L0:
1340 r0 = x.a
1341 r1 = CPyList_GetItem(r0, n)
1342 dec_ref r0
1343 r2 = cast(str, r1)
1344 return r2
1345
1346 [case testBorrowListGetItem2_withgil]
1347 from typing import List
1348
1349 def attr_before_index(x: C) -> str:
1350 return x.a[x.n]
1351
1352 def attr_after_index(a: List[C], i: int) -> int:
1353 return a[i].n
1354
1355 def attr_after_index_literal(a: List[C]) -> int:
1356 return a[0].n
1357
1358 class C:
1359 a: List[str]
1360 n: int
1361 [out]
1362 def attr_before_index(x):
1363 x :: __main__.C
1364 r0 :: list
1365 r1 :: int
1366 r2 :: object
1367 r3 :: str
1368 L0:
1369 r0 = borrow x.a
1370 r1 = borrow x.n
1371 r2 = CPyList_GetItem(r0, r1)
1372 r3 = cast(str, r2)
1373 return r3
1374 def attr_after_index(a, i):
1375 a :: list
1376 i :: int
1377 r0 :: object
1378 r1 :: __main__.C
1379 r2 :: int
1380 L0:
1381 r0 = CPyList_GetItemBorrow(a, i)
1382 r1 = borrow cast(__main__.C, r0)
1383 r2 = r1.n
1384 return r2
1385 def attr_after_index_literal(a):
1386 a :: list
1387 r0 :: object
1388 r1 :: __main__.C
1389 r2 :: int
1390 L0:
1391 r0 = CPyList_GetItemShortBorrow(a, 0)
1392 r1 = borrow cast(__main__.C, r0)
1393 r2 = r1.n
1394 return r2
1395
1396 [case testBorrowListGetItem2_nogil]
1397 from typing import List
1398
1399 def attr_before_index(x: C) -> str:
1400 return x.a[x.n]
1401
1402 def attr_after_index(a: List[C], i: int) -> int:
1403 return a[i].n
1404
1405 def attr_after_index_literal(a: List[C]) -> int:
1406 return a[0].n
1407
1408 class C:
1409 a: List[str]
1410 n: int
1411 [out]
1412 def attr_before_index(x):
1413 x :: __main__.C
1414 r0 :: list
1415 r1 :: int
1416 r2 :: object
1417 r3 :: str
1418 L0:
1419 r0 = x.a
1420 r1 = x.n
1421 r2 = CPyList_GetItem(r0, r1)
1422 dec_ref r0
1423 dec_ref r1 :: int
1424 r3 = cast(str, r2)
1425 return r3
1426 def attr_after_index(a, i):
1427 a :: list
1428 i :: int
1429 r0 :: object
1430 r1 :: __main__.C
1431 r2 :: int
1432 L0:
1433 r0 = CPyList_GetItem(a, i)
1434 r1 = cast(__main__.C, r0)
1435 r2 = r1.n
1436 dec_ref r1
1437 return r2
1438 def attr_after_index_literal(a):
1439 a :: list
1440 r0 :: object
1441 r1 :: __main__.C
1442 r2 :: int
1443 L0:
1444 r0 = CPyList_GetItemShort(a, 0)
1445 r1 = cast(__main__.C, r0)
1446 r2 = r1.n
1447 dec_ref r1
1448 return r2
1449
1450 [case testCannotBorrowListGetItem]
1451 from typing import List
1452
1453 def func_index(x: C) -> str:
1454 return x.a[f()]
1455
1456 def f() -> int: return 0
1457
1458 class C:
1459 a: List[str]
1460 [out]
1461 def func_index(x):
1462 x :: __main__.C
1463 r0 :: list
1464 r1 :: int
1465 r2 :: object
1466 r3 :: str
1467 L0:
1468 r0 = x.a
1469 r1 = f()
1470 r2 = CPyList_GetItem(r0, r1)
1471 dec_ref r0
1472 dec_ref r1 :: int
1473 r3 = cast(str, r2)
1474 return r3
1475 def f():
1476 L0:
1477 return 0
1478
1479 [case testBorrowListGetItemKeepAlive_withgil]
1480 from typing import List
1481
1482 def f() -> str:
1483 a = [C()]
1484 return a[0].s
1485
1486 class C:
1487 s: str
1488 [out]
1489 def f():
1490 r0 :: __main__.C
1491 r1 :: list
1492 r2 :: ptr
1493 a :: list
1494 r3 :: object
1495 r4 :: __main__.C
1496 r5 :: str
1497 L0:
1498 r0 = C()
1499 r1 = PyList_New(1)
1500 r2 = list_items r1
1501 buf_init_item r2, 0, r0
1502 a = r1
1503 r3 = CPyList_GetItemShortBorrow(a, 0)
1504 r4 = borrow cast(__main__.C, r3)
1505 r5 = r4.s
1506 dec_ref a
1507 return r5
1508
1509 [case testBorrowSetAttrObject_withgil]
1510 from typing import Optional
1511
1512 def f(x: Optional[C]) -> None:
1513 if x is not None:
1514 x.b = True
1515
1516 def g(x: D) -> None:
1517 x.c.b = False
1518
1519 class C:
1520 b: bool
1521
1522 class D:
1523 c: C
1524 [out]
1525 def f(x):
1526 x :: union[__main__.C, None]
1527 r0 :: object
1528 r1 :: bit
1529 r2 :: __main__.C
1530 r3 :: bool
1531 L0:
1532 r0 = load_address _Py_NoneStruct
1533 r1 = x != r0
1534 if r1 goto L1 else goto L2 :: bool
1535 L1:
1536 r2 = borrow cast(__main__.C, x)
1537 r2.b = 1; r3 = is_error
1538 L2:
1539 return 1
1540 def g(x):
1541 x :: __main__.D
1542 r0 :: __main__.C
1543 r1 :: bool
1544 L0:
1545 r0 = borrow x.c
1546 r0.b = 0; r1 = is_error
1547 return 1
1548
1549 [case testBorrowIntEquality_withgil]
1550 def add(c: C) -> bool:
1551 return c.x == c.y
1552
1553 class C:
1554 x: int
1555 y: int
1556 [out]
1557 def add(c):
1558 c :: __main__.C
1559 r0, r1 :: int
1560 r2 :: bit
1561 L0:
1562 r0 = borrow c.x
1563 r1 = borrow c.y
1564 r2 = int_eq r0, r1
1565 return r2
1566
1567 [case testBorrowIntLessThan_withgil]
1568 def add(c: C) -> bool:
1569 return c.x < c.y
1570
1571 class C:
1572 x: int
1573 y: int
1574 [out]
1575 def add(c):
1576 c :: __main__.C
1577 r0, r1 :: int
1578 r2 :: bit
1579 L0:
1580 r0 = borrow c.x
1581 r1 = borrow c.y
1582 r2 = int_lt r0, r1
1583 return r2
1584
1585 [case testBorrowIntCompareFinal_withgil]
1586 from typing import Final
1587
1588 X: Final = 10
1589
1590 def add(c: C) -> bool:
1591 return c.x == X
1592
1593 class C:
1594 x: int
1595 [out]
1596 def add(c):
1597 c :: __main__.C
1598 r0 :: int
1599 r1 :: bit
1600 L0:
1601 r0 = borrow c.x
1602 r1 = int_eq r0, 20
1603 return r1
1604
1605 [case testBorrowIntArithmetic_withgil]
1606 def add(c: C) -> int:
1607 return c.x + c.y
1608
1609 def sub(c: C) -> int:
1610 return c.x - c.y
1611
1612 class C:
1613 x: int
1614 y: int
1615 [out]
1616 def add(c):
1617 c :: __main__.C
1618 r0, r1, r2 :: int
1619 L0:
1620 r0 = borrow c.x
1621 r1 = borrow c.y
1622 r2 = CPyTagged_Add(r0, r1)
1623 return r2
1624 def sub(c):
1625 c :: __main__.C
1626 r0, r1, r2 :: int
1627 L0:
1628 r0 = borrow c.x
1629 r1 = borrow c.y
1630 r2 = CPyTagged_Subtract(r0, r1)
1631 return r2
1632
1633 [case testBorrowIntArithmetic_nogil]
1634 def add(c: C) -> int:
1635 return c.x + c.y
1636
1637 def sub(c: C) -> int:
1638 return c.x - c.y
1639
1640 class C:
1641 x: int
1642 y: int
1643 [out]
1644 def add(c):
1645 c :: __main__.C
1646 r0, r1, r2 :: int
1647 L0:
1648 r0 = c.x
1649 r1 = c.y
1650 r2 = CPyTagged_Add(r0, r1)
1651 dec_ref r0 :: int
1652 dec_ref r1 :: int
1653 return r2
1654 def sub(c):
1655 c :: __main__.C
1656 r0, r1, r2 :: int
1657 L0:
1658 r0 = c.x
1659 r1 = c.y
1660 r2 = CPyTagged_Subtract(r0, r1)
1661 dec_ref r0 :: int
1662 dec_ref r1 :: int
1663 return r2
1664
1665 [case testBorrowIntComparisonInIf_withgil]
1666 def add(c: C, n: int) -> bool:
1667 if c.x == c.y:
1668 return True
1669 return False
1670
1671 class C:
1672 x: int
1673 y: int
1674 [out]
1675 def add(c, n):
1676 c :: __main__.C
1677 n, r0, r1 :: int
1678 r2 :: bit
1679 L0:
1680 r0 = borrow c.x
1681 r1 = borrow c.y
1682 r2 = int_eq r0, r1
1683 if r2 goto L1 else goto L2 :: bool
1684 L1:
1685 return 1
1686 L2:
1687 return 0
1688
1689 [case testBorrowIntInPlaceOp_withgil]
1690 def add(c: C, n: int) -> None:
1691 c.x += n
1692
1693 def sub(c: C, n: int) -> None:
1694 c.x -= c.y
1695
1696 class C:
1697 x: int
1698 y: int
1699 [out]
1700 def add(c, n):
1701 c :: __main__.C
1702 n, r0, r1 :: int
1703 r2 :: bool
1704 L0:
1705 r0 = borrow c.x
1706 r1 = CPyTagged_Add(r0, n)
1707 c.x = r1; r2 = is_error
1708 return 1
1709 def sub(c, n):
1710 c :: __main__.C
1711 n, r0, r1, r2 :: int
1712 r3 :: bool
1713 L0:
1714 r0 = borrow c.x
1715 r1 = borrow c.y
1716 r2 = CPyTagged_Subtract(r0, r1)
1717 c.x = r2; r3 = is_error
1718 return 1
1719
1720 [case testCoerceIntToI64_64bit]
1721 from mypy_extensions import i64
1722
1723 def f(x: int) -> i64:
1724 # TODO: On the fast path we shouldn't have a decref. Once we have high-level IR,
1725 # coercion from int to i64 can be a single op, which makes it easier to
1726 # generate optimal refcount handling for this case.
1727 return x + 1
1728 [out]
1729 def f(x):
1730 x, r0 :: int
1731 r1 :: native_int
1732 r2 :: bit
1733 r3, r4 :: i64
1734 r5 :: ptr
1735 r6 :: c_ptr
1736 r7 :: i64
1737 L0:
1738 r0 = CPyTagged_Add(x, 2)
1739 r1 = r0 & 1
1740 r2 = r1 == 0
1741 if r2 goto L1 else goto L2 :: bool
1742 L1:
1743 r3 = r0 >> 1
1744 dec_ref r0 :: int
1745 r4 = r3
1746 goto L3
1747 L2:
1748 r5 = r0 ^ 1
1749 r6 = r5
1750 r7 = CPyLong_AsInt64(r6)
1751 r4 = r7
1752 dec_ref r0 :: int
1753 L3:
1754 return r4
1755
1756 [case testVecTSetItem_64bit]
1757 from librt.vecs import vec
1758 from mypy_extensions import i64
1759
1760 def f(v: vec[str], i: i64, x: str) -> None:
1761 v[i] = x
1762 [out]
1763 def f(v, i, x):
1764 v :: vec[str]
1765 i :: i64
1766 x :: str
1767 r0 :: native_int
1768 r1 :: bit
1769 r2 :: i64
1770 r3 :: bit
1771 r4 :: bool
1772 r5 :: i64
1773 r6 :: ptr
1774 r7 :: i64
1775 r8 :: ptr
1776 r9 :: str
1777 L0:
1778 r0 = v.len
1779 r1 = i < r0 :: unsigned
1780 if r1 goto L4 else goto L1 :: bool
1781 L1:
1782 r2 = i + r0
1783 r3 = r2 < r0 :: unsigned
1784 if r3 goto L3 else goto L2 :: bool
1785 L2:
1786 r4 = raise IndexError
1787 unreachable
1788 L3:
1789 r5 = r2
1790 goto L5
1791 L4:
1792 r5 = i
1793 L5:
1794 r6 = v.items
1795 r7 = r5 * 8
1796 r8 = r6 + r7
1797 r9 = borrow load_mem r8 :: builtins.str*
1798 dec_ref r9
1799 inc_ref x
1800 set_mem r8, x :: builtins.str*
1801 return 1
1802
1803 [case testVecTConstructFromListMultiply_64bit]
1804 from typing import Optional
1805 from librt.vecs import vec
1806 from mypy_extensions import i64
1807
1808 def f(n: i64) -> vec[Optional[str]]:
1809 return vec[Optional[str]]([None] * n)
1810 [out]
1811 def f(n):
1812 n :: i64
1813 r0, r1 :: object
1814 r2, r3 :: ptr
1815 r4 :: vec[str | None]
1816 r5 :: ptr
1817 r6 :: i64
1818 r7, r8 :: ptr
1819 r9 :: bit
1820 r10 :: ptr
1821 L0:
1822 r0 = box(None, 1)
1823 r1 = load_address PyUnicode_Type
1824 r2 = r1
1825 r3 = r2 | 1
1826 r4 = VecTApi.alloc(n, n, r3)
1827 r5 = r4.items
1828 r6 = n * 8
1829 r7 = r5 + r6
1830 r8 = r5
1831 L1:
1832 r9 = r8 < r7 :: unsigned
1833 if r9 goto L2 else goto L3 :: bool
1834 L2:
1835 inc_ref r0
1836 set_mem r8, r0 :: union*
1837 r10 = r8 + 8
1838 r8 = r10
1839 goto L1
1840 L3:
1841 return r4
1842
1843 [case testVecTForLoop_64bit]
1844 from librt.vecs import vec
1845 from mypy_extensions import i64
1846
1847 def f(v: vec[str]) -> i64:
1848 t: i64 = 0
1849 for s in v:
1850 g(s)
1851 return t
1852
1853 def g(s: str) -> None: pass
1854 [out]
1855 def f(v):
1856 v :: vec[str]
1857 t :: i64
1858 r0, r1 :: native_int
1859 r2 :: bit
1860 r3, s :: str
1861 r4 :: None
1862 r5 :: native_int
1863 L0:
1864 t = 0
1865 r0 = 0
1866 L1:
1867 r1 = v.len
1868 r2 = r0 < r1 :: signed
1869 if r2 goto L2 else goto L4 :: bool
1870 L2:
1871 r3 = vec_get_item_unsafe[str] v, r0
1872 s = r3
1873 r4 = g(s)
1874 dec_ref s
1875 L3:
1876 r5 = r0 + 1
1877 r0 = r5
1878 goto L1
1879 L4:
1880 return t
1881 def g(s):
1882 s :: str
1883 L0:
1884 return 1
1885
1886 [case testVecTConstructFromListExpr_64bit]
1887 from librt.vecs import vec
1888
1889 def f() -> vec[str]:
1890 return vec[str](['x', 'y'])
1891 [out]
1892 def f():
1893 r0, r1 :: str
1894 r2 :: object
1895 r3 :: ptr
1896 r4 :: vec[str]
1897 r5, r6, r7 :: ptr
1898 L0:
1899 r0 = 'x'
1900 r1 = 'y'
1901 r2 = load_address PyUnicode_Type
1902 r3 = r2
1903 r4 = VecTApi.alloc(2, 2, r3)
1904 r5 = r4.items
1905 inc_ref r0
1906 set_mem r5, r0 :: builtins.str*
1907 r6 = r5 + 8
1908 inc_ref r1
1909 set_mem r6, r1 :: builtins.str*
1910 r7 = r6 + 8
1911 return r4
1912
1913 [case testVecI64GetItemBorrowVec_64bit]
1914 from librt.vecs import vec
1915 from mypy_extensions import i64
1916
1917 class C:
1918 v: vec[i64]
1919
1920 def f(self, x: i64) -> i64:
1921 return self.v[x]
1922 [out]
1923 def C.f(self, x):
1924 self :: __main__.C
1925 x :: i64
1926 r0 :: vec[i64]
1927 r1 :: native_int
1928 r2 :: bit
1929 r3 :: i64
1930 r4 :: bit
1931 r5 :: bool
1932 r6, r7 :: i64
1933 L0:
1934 r0 = borrow self.v
1935 r1 = r0.len
1936 r2 = x < r1 :: unsigned
1937 if r2 goto L4 else goto L1 :: bool
1938 L1:
1939 r3 = x + r1
1940 r4 = r3 < r1 :: unsigned
1941 if r4 goto L3 else goto L2 :: bool
1942 L2:
1943 r5 = raise IndexError
1944 unreachable
1945 L3:
1946 r6 = r3
1947 goto L5
1948 L4:
1949 r6 = x
1950 L5:
1951 r7 = vec_get_item_unsafe[i64] r0, r6
1952 return r7
1953
1954 [case testVecI64LenBorrowVec_64bit]
1955 from librt.vecs import vec
1956 from mypy_extensions import i64
1957
1958 class C:
1959 v: vec[i64]
1960
1961 def f(self, x: i64) -> i64:
1962 return len(self.v)
1963 [out]
1964 def C.f(self, x):
1965 self :: __main__.C
1966 x :: i64
1967 r0 :: vec[i64]
1968 r1 :: native_int
1969 L0:
1970 r0 = borrow self.v
1971 r1 = r0.len
1972 return r1
1973
1974 [case testVecNestedAppend]
1975 from librt.vecs import vec, append
1976
1977 def f(vv: vec[vec[str]], v: vec[str]) -> vec[vec[str]]:
1978 return append(vv, v)
1979 [out]
1980 def f(vv, v):
1981 vv :: vec[vec[str]]
1982 v :: vec[str]
1983 r0 :: native_int
1984 r1 :: ptr
1985 r2, r3 :: VecNestedBufItem{len:native_int, items:ptr}
1986 r4 :: vec[vec[str]]
1987 L0:
1988 r0 = v.len
1989 r1 = v.items
1990 r2 = set_element undef VecNestedBufItem, len, r0
1991 r3 = set_element r2, items, r1
1992 inc_ref vv
1993 r4 = VecNestedApi.append(vv, r3)
1994 return r4
1995
1996 [case testVecTGetItem_64bit]
1997 from librt.vecs import vec
1998 from mypy_extensions import i64
1999
2000 def f(v: vec[str], n: i64) -> str:
2001 return v[n]
2002 [out]
2003 def f(v, n):
2004 v :: vec[str]
2005 n :: i64
2006 r0 :: native_int
2007 r1 :: bit
2008 r2 :: i64
2009 r3 :: bit
2010 r4 :: bool
2011 r5 :: i64
2012 r6 :: str
2013 L0:
2014 r0 = v.len
2015 r1 = n < r0 :: unsigned
2016 if r1 goto L4 else goto L1 :: bool
2017 L1:
2018 r2 = n + r0
2019 r3 = r2 < r0 :: unsigned
2020 if r3 goto L3 else goto L2 :: bool
2021 L2:
2022 r4 = raise IndexError
2023 unreachable
2024 L3:
2025 r5 = r2
2026 goto L5
2027 L4:
2028 r5 = n
2029 L5:
2030 r6 = vec_get_item_unsafe[str] v, r5
2031 return r6
2032
2033 [case testVecNestedGetItem_64bit]
2034 from librt.vecs import vec
2035 from mypy_extensions import i64
2036
2037 def f(v: vec[vec[str]], n: i64) -> None:
2038 vv = vec[vec[str]]()[n]
2039 [out]
2040 def f(v, n):
2041 v :: vec[vec[str]]
2042 n :: i64
2043 r0 :: object
2044 r1 :: ptr
2045 r2 :: vec[vec[str]]
2046 r3 :: native_int
2047 r4 :: bit
2048 r5 :: i64
2049 r6 :: bit
2050 r7 :: bool
2051 r8 :: i64
2052 r9, vv :: vec[str]
2053 L0:
2054 r0 = load_address PyUnicode_Type
2055 r1 = r0
2056 r2 = VecNestedApi.alloc(0, 0, r1, 1)
2057 r3 = r2.len
2058 r4 = n < r3 :: unsigned
2059 if r4 goto L4 else goto L1 :: bool
2060 L1:
2061 r5 = n + r3
2062 r6 = r5 < r3 :: unsigned
2063 if r6 goto L3 else goto L6 :: bool
2064 L2:
2065 r7 = raise IndexError
2066 unreachable
2067 L3:
2068 r8 = r5
2069 goto L5
2070 L4:
2071 r8 = n
2072 L5:
2073 r9 = vec_get_item_unsafe[vec[str]] r2, r8
2074 dec_ref r2
2075 vv = r9
2076 dec_ref vv
2077 return 1
2078 L6:
2079 dec_ref r2
2080 goto L2
2081
2082 [case testVecNestedGetItemBorrow_64bit]
2083 from librt.vecs import vec
2084 from mypy_extensions import i64
2085
2086 def f(v: vec[vec[i64]], n: i64, m: i64) -> i64:
2087 return v[n][m]
2088 [out]
2089 def f(v, n, m):
2090 v :: vec[vec[i64]]
2091 n, m :: i64
2092 r0 :: native_int
2093 r1 :: bit
2094 r2 :: i64
2095 r3 :: bit
2096 r4 :: bool
2097 r5 :: i64
2098 r6 :: vec[i64]
2099 r7 :: native_int
2100 r8 :: bit
2101 r9 :: i64
2102 r10 :: bit
2103 r11 :: bool
2104 r12, r13 :: i64
2105 L0:
2106 r0 = v.len
2107 r1 = n < r0 :: unsigned
2108 if r1 goto L4 else goto L1 :: bool
2109 L1:
2110 r2 = n + r0
2111 r3 = r2 < r0 :: unsigned
2112 if r3 goto L3 else goto L2 :: bool
2113 L2:
2114 r4 = raise IndexError
2115 unreachable
2116 L3:
2117 r5 = r2
2118 goto L5
2119 L4:
2120 r5 = n
2121 L5:
2122 r6 = vec_get_item_unsafe_borrow[vec[i64]] v, r5
2123 r7 = r6.len
2124 r8 = m < r7 :: unsigned
2125 if r8 goto L9 else goto L6 :: bool
2126 L6:
2127 r9 = m + r7
2128 r10 = r9 < r7 :: unsigned
2129 if r10 goto L8 else goto L7 :: bool
2130 L7:
2131 r11 = raise IndexError
2132 unreachable
2133 L8:
2134 r12 = r9
2135 goto L10
2136 L9:
2137 r12 = m
2138 L10:
2139 r13 = vec_get_item_unsafe[i64] r6, r12
2140 return r13
2141
2142 [case testVecPop]
2143 from librt.vecs import vec, pop, append
2144 from mypy_extensions import i64
2145
2146 def f() -> vec[i64]:
2147 v = vec[i64]()
2148 v = append(v, 7)
2149 v, x = pop(v)
2150 return v
2151 [out]
2152 def f():
2153 r0, v, r1 :: vec[i64]
2154 r2 :: tuple[vec[i64], i64]
2155 r3 :: vec[i64]
2156 r4 :: i64
2157 r5 :: vec[i64]
2158 r6, x :: i64
2159 L0:
2160 r0 = VecI64Api.alloc(0, 0)
2161 v = r0
2162 r1 = VecI64Api.append(v, 7)
2163 v = r1
2164 r2 = VecI64Api.pop(v, -1)
2165 r3 = borrow r2[0]
2166 r4 = borrow r2[1]
2167 r5 = unborrow r3
2168 v = r5
2169 r6 = unborrow r4
2170 x = r6
2171 return v
2172
2173 [case testVecNestedPop]
2174 from librt.vecs import vec, pop
2175
2176 def f(v: vec[vec[str]]) -> vec[str]:
2177 vv, x = pop(v)
2178 return x
2179 [out]
2180 def f(v):
2181 v :: vec[vec[str]]
2182 r0 :: tuple[vec[vec[str]], VecNestedBufItem{len:native_int, items:ptr}]
2183 r1, r2 :: vec[vec[str]]
2184 r3, r4 :: VecNestedBufItem{len:native_int, items:ptr}
2185 r5 :: native_int
2186 r6 :: ptr
2187 r7, r8 :: vec[str]
2188 r9 :: tuple[vec[vec[str]], vec[str]]
2189 r10 :: vec[vec[str]]
2190 r11 :: vec[str]
2191 r12, vv :: vec[vec[str]]
2192 r13, x :: vec[str]
2193 L0:
2194 inc_ref v
2195 r0 = VecNestedApi.pop(v, -1)
2196 r1 = borrow r0[0]
2197 r2 = unborrow r1
2198 r3 = borrow r0[1]
2199 r4 = unborrow r3
2200 r5 = r4.len
2201 r6 = r4.items
2202 r7 = set_element undef vec[str], len, r5
2203 r8 = set_element r7, items, r6
2204 r9 = (r2, r8)
2205 r10 = borrow r9[0]
2206 r11 = borrow r9[1]
2207 r12 = unborrow r10
2208 vv = r12
2209 dec_ref vv
2210 r13 = unborrow r11
2211 x = r13
2212 return x
2213
2214 [case testPropertySetterCallWithRefcountedObject]
2215 class Foo:
2216 def __init__(self) -> None:
2217 self.attr = "unmodified"
2218
2219 class A:
2220 def __init__(self) -> None:
2221 self._foo = Foo()
2222
2223 @property
2224 def foo(self) -> Foo:
2225 return self._foo
2226
2227 @foo.setter
2228 def foo(self, val : Foo) -> None:
2229 self._foo = val
2230
2231 def f(a: A):
2232 a.foo = Foo()
2233 [out]
2234 def Foo.__init__(self):
2235 self :: __main__.Foo
2236 r0 :: str
2237 L0:
2238 r0 = 'unmodified'
2239 inc_ref r0
2240 self.attr = r0
2241 return 1
2242 def A.__init__(self):
2243 self :: __main__.A
2244 r0 :: __main__.Foo
2245 L0:
2246 r0 = Foo()
2247 self._foo = r0
2248 return 1
2249 def A.foo(self):
2250 self :: __main__.A
2251 r0 :: __main__.Foo
2252 L0:
2253 r0 = self._foo
2254 return r0
2255 def A.__mypyc_setter__foo(self, val):
2256 self :: __main__.A
2257 val :: __main__.Foo
2258 r0 :: bool
2259 L0:
2260 inc_ref val
2261 self._foo = val; r0 = is_error
2262 return 1
2263 def f(a):
2264 a :: __main__.A
2265 r0 :: __main__.Foo
2266 r1 :: bool
2267 r2 :: object
2268 L0:
2269 r0 = Foo()
2270 a.foo = r0; r1 = is_error
2271 dec_ref r0
2272 r2 = box(None, 1)
2273 inc_ref r2
2274 return r2
2275
2276 [case testPropertySetterCallWithNonRefcountedObject_64bit]
2277 from mypy_extensions import i64
2278
2279 class A:
2280 def __init__(self) -> None:
2281 self._foo = i64(0)
2282
2283 @property
2284 def foo(self) -> i64:
2285 return self._foo
2286
2287 @foo.setter
2288 def foo(self, val : i64) -> None:
2289 self._foo = val
2290
2291 def f(a: A, i: int):
2292 a.foo = i64(i)
2293 [out]
2294 def A.__init__(self):
2295 self :: __main__.A
2296 L0:
2297 self._foo = 0
2298 return 1
2299 def A.foo(self):
2300 self :: __main__.A
2301 r0 :: i64
2302 L0:
2303 r0 = self._foo
2304 return r0
2305 def A.__mypyc_setter__foo(self, val):
2306 self :: __main__.A
2307 val :: i64
2308 r0 :: bool
2309 L0:
2310 self._foo = val; r0 = is_error
2311 return 1
2312 def f(a, i):
2313 a :: __main__.A
2314 i :: int
2315 r0 :: native_int
2316 r1 :: bit
2317 r2, r3 :: i64
2318 r4 :: ptr
2319 r5 :: c_ptr
2320 r6 :: i64
2321 r7 :: bool
2322 r8 :: object
2323 L0:
2324 r0 = i & 1
2325 r1 = r0 == 0
2326 if r1 goto L1 else goto L2 :: bool
2327 L1:
2328 r2 = i >> 1
2329 r3 = r2
2330 goto L3
2331 L2:
2332 r4 = i ^ 1
2333 r5 = r4
2334 r6 = CPyLong_AsInt64(r5)
2335 r3 = r6
2336 L3:
2337 a.foo = r3; r7 = is_error
2338 r8 = box(None, 1)
2339 inc_ref r8
2340 return r8
2341
2342 [case testBytesConcatRefcount]
2343 def f(a: bytes, b: bytes) -> bytes:
2344 return b"1" + a + b
2345 [out]
2346 def f(a, b):
2347 a, b, r0, r1, r2 :: bytes
2348 L0:
2349 r0 = b'1'
2350 r1 = CPyBytes_Concat(r0, a)
2351 r2 = CPyBytes_Concat(r1, b)
2352 dec_ref r1
2353 return r2
2354
2355 [case testChainedBytesConcatRefcount]
2356 def f(a: bytes, b: bytes, c: bytes) -> bytes:
2357 return b"1" + a + b + c
2358 [out]
2359 def f(a, b, c):
2360 a, b, c, r0, r1, r2, r3 :: bytes
2361 L0:
2362 r0 = b'1'
2363 r1 = CPyBytes_Concat(r0, a)
2364 r2 = CPyBytes_Concat(r1, b)
2365 dec_ref r1
2366 r3 = CPyBytes_Concat(r2, c)
2367 dec_ref r2
2368 return r3
2369
2370 [case testBorrowedFinalAttribute]
2371 from typing import Final
2372
2373 class C:
2374 def __init__(self, s: str) -> None:
2375 self.s: Final = s
2376
2377 def f(c: C, b: bool) -> str:
2378 a = c.s if b else "x"
2379 return a
2380
2381 def g(c: C) -> C:
2382 return C(c.s)
2383 [out]
2384 def C.__init__(self, s):
2385 self :: __main__.C
2386 s :: str
2387 L0:
2388 inc_ref s
2389 self.s = s
2390 return 1
2391 def f(c, b):
2392 c :: __main__.C
2393 b :: bool
2394 r0, r1, r2, a :: str
2395 L0:
2396 if b goto L1 else goto L2 :: bool
2397 L1:
2398 r0 = borrow c.s
2399 inc_ref r0
2400 r1 = r0
2401 goto L3
2402 L2:
2403 r2 = 'x'
2404 inc_ref r2
2405 r1 = r2
2406 L3:
2407 a = r1
2408 return a
2409 def g(c):
2410 c :: __main__.C
2411 r0 :: str
2412 r1 :: __main__.C
2413 L0:
2414 r0 = borrow c.s
2415 r1 = C(r0)
2416 return r1
2417
2418 [case testBorrowedFinalAttribute2_withgil]
2419 from typing import Final
2420
2421 class C:
2422 def __init__(self, s: str) -> None:
2423 self.s: Final = s
2424
2425 def g(c: C, n: int) -> str:
2426 a = [c]
2427 return a[n].s + "a"
2428 [out]
2429 def C.__init__(self, s):
2430 self :: __main__.C
2431 s :: str
2432 L0:
2433 inc_ref s
2434 self.s = s
2435 return 1
2436 def g(c, n):
2437 c :: __main__.C
2438 n :: int
2439 r0 :: list
2440 r1 :: ptr
2441 a :: list
2442 r2 :: object
2443 r3 :: __main__.C
2444 r4, r5, r6 :: str
2445 L0:
2446 r0 = PyList_New(1)
2447 r1 = list_items r0
2448 inc_ref c
2449 buf_init_item r1, 0, c
2450 a = r0
2451 r2 = CPyList_GetItemBorrow(a, n)
2452 r3 = borrow cast(__main__.C, r2)
2453 r4 = r3.s
2454 dec_ref a
2455 r5 = 'a'
2456 r6 = PyUnicode_Concat(r4, r5)
2457 dec_ref r4
2458 return r6
2459
2460 [case testBorrowedFinalAttribute3_withgil]
2461 from typing import Final
2462
2463 class C:
2464 def __init__(self, d: D) -> None:
2465 self.d = d
2466
2467 class CC:
2468 def __init__(self, d: D) -> None:
2469 self.d: Final = d
2470
2471 class D:
2472 def __init__(self, s: str) -> None:
2473 self.f: Final = s
2474
2475 def f() -> str:
2476 c = C(D("x"))
2477 return c.d.f + "a"
2478
2479 def g(d: D) -> str:
2480 c = CC(d)
2481 return c.d.f + "a"
2482 [out]
2483 def C.__init__(self, d):
2484 self :: __main__.C
2485 d :: __main__.D
2486 L0:
2487 inc_ref d
2488 self.d = d
2489 return 1
2490 def CC.__init__(self, d):
2491 self :: __main__.CC
2492 d :: __main__.D
2493 L0:
2494 inc_ref d
2495 self.d = d
2496 return 1
2497 def D.__init__(self, s):
2498 self :: __main__.D
2499 s :: str
2500 L0:
2501 inc_ref s
2502 self.f = s
2503 return 1
2504 def f():
2505 r0 :: str
2506 r1 :: __main__.D
2507 r2, c :: __main__.C
2508 r3 :: __main__.D
2509 r4, r5, r6 :: str
2510 L0:
2511 r0 = 'x'
2512 r1 = D(r0)
2513 r2 = C(r1)
2514 dec_ref r1
2515 c = r2
2516 r3 = borrow c.d
2517 r4 = r3.f
2518 dec_ref c
2519 r5 = 'a'
2520 r6 = PyUnicode_Concat(r4, r5)
2521 dec_ref r4
2522 return r6
2523 def g(d):
2524 d :: __main__.D
2525 r0, c :: __main__.CC
2526 r1 :: __main__.D
2527 r2, r3, r4 :: str
2528 L0:
2529 r0 = CC(d)
2530 c = r0
2531 r1 = borrow c.d
2532 r2 = borrow r1.f
2533 r3 = 'a'
2534 r4 = PyUnicode_Concat(r2, r3)
2535 dec_ref c
2536 return r4
2537
2538 [case testBorrowedFinalAttribute4]
2539 from typing import Final
2540
2541 class C:
2542 def __init__(self, d: D) -> None:
2543 self.d: Final = d
2544
2545 def method(self) -> int:
2546 return self.d.foo()
2547
2548 class D:
2549 def foo(self) -> int:
2550 return 1
2551 [out]
2552 def C.__init__(self, d):
2553 self :: __main__.C
2554 d :: __main__.D
2555 L0:
2556 inc_ref d
2557 self.d = d
2558 return 1
2559 def C.method(self):
2560 self :: __main__.C
2561 r0 :: __main__.D
2562 r1 :: int
2563 L0:
2564 r0 = borrow self.d
2565 r1 = r0.foo()
2566 return r1
2567 def D.foo(self):
2568 self :: __main__.D
2569 L0:
2570 return 2
2571
2572 [case testBorrowedFinalAttribute5_64bit]
2573 from typing import Final
2574 from librt.vecs import vec
2575 from mypy_extensions import i64
2576
2577 class C:
2578 def __init__(self, s: str) -> None:
2579 self.s: Final = s
2580
2581 def g(c: C, n: i64) -> str:
2582 a = vec[C]([c])
2583 return a[n].s + "a"
2584 [out]
2585 def C.__init__(self, s):
2586 self :: __main__.C
2587 s :: str
2588 L0:
2589 inc_ref s
2590 self.s = s
2591 return 1
2592 def g(c, n):
2593 c :: __main__.C
2594 n :: i64
2595 r0 :: object
2596 r1 :: ptr
2597 r2 :: vec[__main__.C]
2598 r3, r4 :: ptr
2599 a :: vec[__main__.C]
2600 r5 :: native_int
2601 r6 :: bit
2602 r7 :: i64
2603 r8 :: bit
2604 r9 :: bool
2605 r10 :: i64
2606 r11 :: __main__.C
2607 r12, r13, r14 :: str
2608 L0:
2609 r0 = __main__.C :: type
2610 r1 = r0
2611 r2 = VecTApi.alloc(1, 1, r1)
2612 r3 = r2.items
2613 inc_ref c
2614 set_mem r3, c :: __main__.C*
2615 r4 = r3 + 8
2616 a = r2
2617 r5 = a.len
2618 r6 = n < r5 :: unsigned
2619 if r6 goto L4 else goto L1 :: bool
2620 L1:
2621 r7 = n + r5
2622 r8 = r7 < r5 :: unsigned
2623 if r8 goto L3 else goto L6 :: bool
2624 L2:
2625 r9 = raise IndexError
2626 unreachable
2627 L3:
2628 r10 = r7
2629 goto L5
2630 L4:
2631 r10 = n
2632 L5:
2633 r11 = vec_get_item_unsafe_borrow[__main__.C] a, r10
2634 r12 = r11.s
2635 dec_ref a
2636 r13 = 'a'
2637 r14 = PyUnicode_Concat(r12, r13)
2638 dec_ref r12
2639 return r14
2640 L6:
2641 dec_ref a
2642 goto L2
2643
2644 [case testBorrowedFinalAttributeReassignViaWalrus]
2645 from typing import Final
2646
2647 class C:
2648 def __init__(self, s: str) -> None:
2649 self.s: Final = s
2650
2651 def f(c: C, d: C) -> str:
2652 return c.s + (c := d).s
2653 [out]
2654 def C.__init__(self, s):
2655 self :: __main__.C
2656 s :: str
2657 L0:
2658 inc_ref s
2659 self.s = s
2660 return 1
2661 def f(c, d):
2662 c, d :: __main__.C
2663 r0, r1, r2 :: str
2664 L0:
2665 r0 = c.s
2666 inc_ref d
2667 c = d
2668 dec_ref c
2669 r1 = borrow d.s
2670 r2 = PyUnicode_Concat(r0, r1)
2671 dec_ref r0
2672 return r2
2673
2674 [case testBorrowedFinalAttributeConditionalMerge]
2675 from typing import Final
2676
2677 class C:
2678 def __init__(self, s: str) -> None:
2679 self.s: Final = s
2680
2681 def f(b: bool) -> str:
2682 return (C("x").s if b else "y") + "z"
2683 [out]
2684 def C.__init__(self, s):
2685 self :: __main__.C
2686 s :: str
2687 L0:
2688 inc_ref s
2689 self.s = s
2690 return 1
2691 def f(b):
2692 b :: bool
2693 r0 :: str
2694 r1 :: __main__.C
2695 r2, r3, r4, r5, r6 :: str
2696 L0:
2697 if b goto L1 else goto L2 :: bool
2698 L1:
2699 r0 = 'x'
2700 r1 = C(r0)
2701 r2 = borrow r1.s
2702 inc_ref r2
2703 r3 = r2
2704 dec_ref r1
2705 goto L3
2706 L2:
2707 r4 = 'y'
2708 inc_ref r4
2709 r3 = r4
2710 L3:
2711 r5 = 'z'
2712 r6 = PyUnicode_Concat(r3, r5)
2713 dec_ref r3
2714 return r6
2715
2716 [case testBorrowedFinalAttributeAndMerge]
2717 from typing import Final
2718
2719 class C:
2720 def __init__(self, s: str) -> None:
2721 self.s: Final = s
2722
2723 def f(s: str) -> str:
2724 return (s and C("x").s) + "z"
2725 [out]
2726 def C.__init__(self, s):
2727 self :: __main__.C
2728 s :: str
2729 L0:
2730 inc_ref s
2731 self.s = s
2732 return 1
2733 def f(s):
2734 s :: str
2735 r0 :: bit
2736 r1, r2 :: str
2737 r3 :: __main__.C
2738 r4, r5, r6 :: str
2739 L0:
2740 r0 = CPyStr_IsTrue(s)
2741 if r0 goto L2 else goto L1 :: bool
2742 L1:
2743 inc_ref s
2744 r1 = s
2745 goto L3
2746 L2:
2747 r2 = 'x'
2748 r3 = C(r2)
2749 r4 = borrow r3.s
2750 inc_ref r4
2751 r1 = r4
2752 dec_ref r3
2753 L3:
2754 r5 = 'z'
2755 r6 = PyUnicode_Concat(r1, r5)
2756 dec_ref r1
2757 return r6
2758
2759 [case testBorrowedFinalAttributeOrMerge]
2760 from typing import Final
2761
2762 class C:
2763 def __init__(self, s: str) -> None:
2764 self.s: Final = s
2765
2766 def f(s: str) -> str:
2767 return (s or C("x").s) + "z"
2768 [out]
2769 def C.__init__(self, s):
2770 self :: __main__.C
2771 s :: str
2772 L0:
2773 inc_ref s
2774 self.s = s
2775 return 1
2776 def f(s):
2777 s :: str
2778 r0 :: bit
2779 r1, r2 :: str
2780 r3 :: __main__.C
2781 r4, r5, r6 :: str
2782 L0:
2783 r0 = CPyStr_IsTrue(s)
2784 if r0 goto L1 else goto L2 :: bool
2785 L1:
2786 inc_ref s
2787 r1 = s
2788 goto L3
2789 L2:
2790 r2 = 'x'
2791 r3 = C(r2)
2792 r4 = borrow r3.s
2793 inc_ref r4
2794 r1 = r4
2795 dec_ref r3
2796 L3:
2797 r5 = 'z'
2798 r6 = PyUnicode_Concat(r1, r5)
2799 dec_ref r1
2800 return r6
2801
2802 [case testBorrowedFinalAttributeChainedComparisonMerge]
2803 from typing import Final
2804
2805 class C:
2806 def __init__(self, s: str) -> None:
2807 self.s: Final = s
2808
2809 def f(s: str) -> bool:
2810 return s == "x" == C("x").s
2811 [out]
2812 def C.__init__(self, s):
2813 self :: __main__.C
2814 s :: str
2815 L0:
2816 inc_ref s
2817 self.s = s
2818 return 1
2819 def f(s):
2820 s, r0 :: str
2821 r1, r2 :: bool
2822 r3 :: str
2823 r4 :: __main__.C
2824 r5 :: str
2825 r6 :: bool
2826 L0:
2827 r0 = 'x'
2828 r1 = CPyStr_EqualLiteral(s, r0, 1)
2829 if r1 goto L2 else goto L1 :: bool
2830 L1:
2831 r2 = r1
2832 goto L3
2833 L2:
2834 r3 = 'x'
2835 r4 = C(r3)
2836 r5 = borrow r4.s
2837 r6 = CPyStr_EqualLiteral(r5, r0, 1)
2838 r2 = r6
2839 dec_ref r4
2840 L3:
2841 return r2
2842
2843 [case testBorrowedFinalInListComprehension_withgil]
2844 from typing import Final
2845
2846 class C:
2847 def __init__(self, s: str) -> None:
2848 self.s: Final = s
2849
2850 def f(a: list[C]) -> list[str]:
2851 return [x.s for x in a]
2852 [out]
2853 def C.__init__(self, s):
2854 self :: __main__.C
2855 s :: str
2856 L0:
2857 inc_ref s
2858 self.s = s
2859 return 1
2860 def f(a):
2861 a :: list
2862 r0 :: native_int
2863 r1 :: list
2864 r2, r3 :: native_int
2865 r4 :: bit
2866 r5 :: object
2867 r6, x :: __main__.C
2868 r7 :: str
2869 r8 :: native_int
2870 L0:
2871 r0 = var_object_size a
2872 r1 = PyList_New(r0)
2873 r2 = 0
2874 L1:
2875 r3 = var_object_size a
2876 r4 = r2 < r3 :: signed
2877 if r4 goto L2 else goto L4 :: bool
2878 L2:
2879 r5 = list_get_item_unsafe a, r2
2880 r6 = cast(__main__.C, r5)
2881 x = r6
2882 r7 = x.s
2883 dec_ref x
2884 CPyList_SetItemUnsafe(r1, r2, r7)
2885 L3:
2886 r8 = r2 + 1
2887 r2 = r8
2888 goto L1
2889 L4:
2890 return r1
2891
2892 [case testBorrowedFinalInSetComprehension_withgil]
2893 from typing import Final
2894
2895 class C:
2896 def __init__(self, d: D) -> None:
2897 self.d: Final = d
2898
2899 class D:
2900 def __init__(self, s: str) -> None:
2901 self.s: Final = s
2902
2903 def use(s: str) -> bool:
2904 return True
2905
2906 def f(a: list[C]) -> set[C]:
2907 return {x for x in a if use(x.d.s)}
2908 [out]
2909 def C.__init__(self, d):
2910 self :: __main__.C
2911 d :: __main__.D
2912 L0:
2913 inc_ref d
2914 self.d = d
2915 return 1
2916 def D.__init__(self, s):
2917 self :: __main__.D
2918 s :: str
2919 L0:
2920 inc_ref s
2921 self.s = s
2922 return 1
2923 def use(s):
2924 s :: str
2925 L0:
2926 return 1
2927 def f(a):
2928 a :: list
2929 r0 :: set
2930 r1, r2 :: native_int
2931 r3 :: bit
2932 r4 :: object
2933 r5, x :: __main__.C
2934 r6 :: __main__.D
2935 r7 :: str
2936 r8 :: bool
2937 r9 :: i32
2938 r10 :: bit
2939 r11 :: native_int
2940 L0:
2941 r0 = PySet_New(0)
2942 r1 = 0
2943 L1:
2944 r2 = var_object_size a
2945 r3 = r1 < r2 :: signed
2946 if r3 goto L2 else goto L5 :: bool
2947 L2:
2948 r4 = list_get_item_unsafe a, r1
2949 r5 = cast(__main__.C, r4)
2950 x = r5
2951 r6 = borrow x.d
2952 r7 = borrow r6.s
2953 r8 = use(r7)
2954 if r8 goto L3 else goto L6 :: bool
2955 L3:
2956 r9 = PySet_Add(r0, x)
2957 dec_ref x
2958 r10 = r9 >= 0 :: signed
2959 L4:
2960 r11 = r1 + 1
2961 r1 = r11
2962 goto L1
2963 L5:
2964 return r0
2965 L6:
2966 dec_ref x
2967 goto L4
2968
2969 [case testBorrowedFinalAttributeThroughCast]
2970 from typing import Final, cast
2971
2972 class C:
2973 def __init__(self, x: object) -> None:
2974 self.x: Final = x
2975
2976 def make() -> object:
2977 return C(1)
2978
2979 def g(a: object) -> object:
2980 return a
2981
2982 def h() -> object:
2983 return g(cast(C, make()).x)
2984 [out]
2985 def C.__init__(self, x):
2986 self :: __main__.C
2987 x :: object
2988 L0:
2989 inc_ref x
2990 self.x = x
2991 return 1
2992 def make():
2993 r0 :: object
2994 r1 :: __main__.C
2995 L0:
2996 r0 = object 1
2997 r1 = C(r0)
2998 return r1
2999 def g(a):
3000 a :: object
3001 L0:
3002 inc_ref a
3003 return a
3004 def h():
3005 r0 :: object
3006 r1 :: __main__.C
3007 r2, r3 :: object
3008 L0:
3009 r0 = make()
3010 r1 = borrow cast(__main__.C, r0)
3011 r2 = borrow r1.x
3012 r3 = g(r2)
3013 dec_ref r0
3014 return r3
3015
3016 [case testBorrowedFinalAttributeCastChain]
3017 from typing import Final, cast
3018
3019 class Inner:
3020 def __init__(self, x: object) -> None:
3021 self.x: Final = x
3022
3023 class Outer:
3024 def __init__(self, inner: object) -> None:
3025 self.inner: Final = inner
3026
3027 def g(a: object) -> object:
3028 return a
3029
3030 def make() -> object:
3031 return Outer(Inner(1))
3032
3033 def h() -> object:
3034 # cast( Inner, <cast(Outer, make()).inner> ).x
3035 o = cast(Outer, make())
3036 return g(cast(Inner, o.inner).x)
3037 [out]
3038 def Inner.__init__(self, x):
3039 self :: __main__.Inner
3040 x :: object
3041 L0:
3042 inc_ref x
3043 self.x = x
3044 return 1
3045 def Outer.__init__(self, inner):
3046 self :: __main__.Outer
3047 inner :: object
3048 L0:
3049 inc_ref inner
3050 self.inner = inner
3051 return 1
3052 def g(a):
3053 a :: object
3054 L0:
3055 inc_ref a
3056 return a
3057 def make():
3058 r0 :: object
3059 r1 :: __main__.Inner
3060 r2 :: __main__.Outer
3061 L0:
3062 r0 = object 1
3063 r1 = Inner(r0)
3064 r2 = Outer(r1)
3065 dec_ref r1
3066 return r2
3067 def h():
3068 r0 :: object
3069 r1, o :: __main__.Outer
3070 r2 :: object
3071 r3 :: __main__.Inner
3072 r4, r5 :: object
3073 L0:
3074 r0 = make()
3075 r1 = cast(__main__.Outer, r0)
3076 o = r1
3077 r2 = borrow o.inner
3078 r3 = borrow cast(__main__.Inner, r2)
3079 r4 = borrow r3.x
3080 r5 = g(r4)
3081 dec_ref o
3082 return r5