irbuild-generics.test 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. [case testGenericFunction]
  2. from typing import TypeVar, List
  3. T = TypeVar('T')
  4. def f(x: T) -> T:
  5. return x
  6. def g(x: List[T]) -> List[T]:
  7. return [x[0]]
  8. def h(x: int, y: List[int]) -> None:
  9. x = f(x)
  10. y = g(y)
  11. [out]
  12. def f(x):
  13. x :: object
  14. L0:
  15. return x
  16. def g(x):
  17. x :: list
  18. r0 :: object
  19. r1 :: list
  20. r2, r3 :: ptr
  21. L0:
  22. r0 = CPyList_GetItemShort(x, 0)
  23. r1 = PyList_New(1)
  24. r2 = get_element_ptr r1 ob_item :: PyListObject
  25. r3 = load_mem r2 :: ptr*
  26. set_mem r3, r0 :: builtins.object*
  27. keep_alive r1
  28. return r1
  29. def h(x, y):
  30. x :: int
  31. y :: list
  32. r0, r1 :: object
  33. r2 :: int
  34. r3 :: list
  35. L0:
  36. r0 = box(int, x)
  37. r1 = f(r0)
  38. r2 = unbox(int, r1)
  39. x = r2
  40. r3 = g(y)
  41. y = r3
  42. return 1
  43. [case testGenericAttrAndTypeApplication]
  44. from typing import TypeVar, Generic
  45. T = TypeVar('T')
  46. class C(Generic[T]):
  47. x: T
  48. def f() -> None:
  49. c = C[int]()
  50. c.x = 1
  51. 2 + c.x
  52. [out]
  53. def f():
  54. r0, c :: __main__.C
  55. r1 :: object
  56. r2 :: bool
  57. r3 :: object
  58. r4, r5 :: int
  59. L0:
  60. r0 = C()
  61. c = r0
  62. r1 = object 1
  63. c.x = r1; r2 = is_error
  64. r3 = borrow c.x
  65. r4 = unbox(int, r3)
  66. r5 = CPyTagged_Add(4, r4)
  67. keep_alive c
  68. return 1
  69. [case testGenericMethod]
  70. from typing import TypeVar, Generic
  71. T = TypeVar('T')
  72. class C(Generic[T]):
  73. x: T
  74. def __init__(self, x: T) -> None:
  75. self.x = x
  76. def get(self) -> T:
  77. return self.x
  78. def set(self, y: T) -> None:
  79. self.x = y
  80. def f(x: C[int]) -> None:
  81. y = x.get()
  82. x.set(y + 1)
  83. x = C(2)
  84. [out]
  85. def C.__init__(self, x):
  86. self :: __main__.C
  87. x :: object
  88. r0 :: bool
  89. L0:
  90. self.x = x; r0 = is_error
  91. return 1
  92. def C.get(self):
  93. self :: __main__.C
  94. r0 :: object
  95. L0:
  96. r0 = self.x
  97. return r0
  98. def C.set(self, y):
  99. self :: __main__.C
  100. y :: object
  101. r0 :: bool
  102. L0:
  103. self.x = y; r0 = is_error
  104. return 1
  105. def f(x):
  106. x :: __main__.C
  107. r0 :: object
  108. r1, y, r2 :: int
  109. r3 :: object
  110. r4 :: None
  111. r5 :: object
  112. r6 :: __main__.C
  113. L0:
  114. r0 = x.get()
  115. r1 = unbox(int, r0)
  116. y = r1
  117. r2 = CPyTagged_Add(y, 2)
  118. r3 = box(int, r2)
  119. r4 = x.set(r3)
  120. r5 = object 2
  121. r6 = C(r5)
  122. x = r6
  123. return 1
  124. [case testMax]
  125. from typing import TypeVar
  126. T = TypeVar('T')
  127. def f(x: T, y: T) -> T:
  128. return max(x, y)
  129. [out]
  130. def f(x, y):
  131. x, y, r0 :: object
  132. r1 :: i32
  133. r2 :: bit
  134. r3 :: bool
  135. r4 :: object
  136. L0:
  137. r0 = PyObject_RichCompare(y, x, 4)
  138. r1 = PyObject_IsTrue(r0)
  139. r2 = r1 >= 0 :: signed
  140. r3 = truncate r1: i32 to builtins.bool
  141. if r3 goto L1 else goto L2 :: bool
  142. L1:
  143. r4 = y
  144. goto L3
  145. L2:
  146. r4 = x
  147. L3:
  148. return r4