Debian Codegraph @

mypyc/test-data/irbuild-unreachable.test

raw
1 # Test cases for unreachable expressions and statements
2
3 [case testUnreachableMemberExpr]
4 import sys
5
6 def f() -> None:
7 y = sys.platform == "x" and sys.version_info > (3, 5)
8 [out]
9 def f():
10 r0 :: object
11 r1 :: str
12 r2 :: object
13 r3, r4 :: str
14 r5, r6, r7 :: bool
15 r8 :: object
16 r9, y :: bool
17 L0:
18 r0 = sys :: module
19 r1 = 'platform'
20 r2 = CPyObject_GetAttr(r0, r1)
21 r3 = cast(str, r2)
22 r4 = 'x'
23 r5 = CPyStr_EqualLiteral(r3, r4, 1)
24 if r5 goto L2 else goto L1 :: bool
25 L1:
26 r6 = r5
27 goto L3
28 L2:
29 r7 = raise RuntimeError('mypyc internal error: should be unreachable')
30 r8 = box(None, 1)
31 r9 = unbox(bool, r8)
32 r6 = r9
33 L3:
34 y = r6
35 return 1
36
37 [case testUnreachableNameExpr]
38 import sys
39
40 def f() -> None:
41 y = sys.platform == 'x' and foobar
42 [out]
43 def f():
44 r0 :: object
45 r1 :: str
46 r2 :: object
47 r3, r4 :: str
48 r5, r6, r7 :: bool
49 r8 :: object
50 r9, y :: bool
51 L0:
52 r0 = sys :: module
53 r1 = 'platform'
54 r2 = CPyObject_GetAttr(r0, r1)
55 r3 = cast(str, r2)
56 r4 = 'x'
57 r5 = CPyStr_EqualLiteral(r3, r4, 1)
58 if r5 goto L2 else goto L1 :: bool
59 L1:
60 r6 = r5
61 goto L3
62 L2:
63 r7 = raise RuntimeError('mypyc internal error: should be unreachable')
64 r8 = box(None, 1)
65 r9 = unbox(bool, r8)
66 r6 = r9
67 L3:
68 y = r6
69 return 1
70
71 [case testUnreachableFinalNameExpr]
72 from typing import Final
73
74 ZERO: Final = 0
75
76 def f(x: int) -> int:
77 if x is None:
78 return ZERO
79 return ZERO
80 [out]
81 def f(x):
82 x :: int
83 r0, r1 :: object
84 r2 :: bit
85 r3 :: object
86 r4 :: int
87 L0:
88 r0 = load_address _Py_NoneStruct
89 r1 = box(int, x)
90 r2 = r1 == r0
91 if r2 goto L1 else goto L2 :: bool
92 L1:
93 r3 = object 0
94 r4 = unbox(int, r3)
95 return r4
96 L2:
97 return 0
98
99 [case testUnreachableStatementAfterReturn]
100 def f(x: bool) -> int:
101 if x:
102 return 1
103 f(False)
104 return 2
105 [out]
106 def f(x):
107 x :: bool
108 L0:
109 if x goto L1 else goto L2 :: bool
110 L1:
111 return 2
112 L2:
113 return 4
114
115 [case testUnreachableStatementAfterContinue]
116 def c() -> bool:
117 return False
118
119 def f() -> None:
120 n = True
121 while n:
122 if c():
123 continue
124 if int():
125 f()
126 n = False
127 [out]
128 def c():
129 L0:
130 return 0
131 def f():
132 n, r0 :: bool
133 L0:
134 n = 1
135 L1:
136 if n goto L2 else goto L5 :: bool
137 L2:
138 r0 = c()
139 if r0 goto L3 else goto L4 :: bool
140 L3:
141 goto L1
142 L4:
143 n = 0
144 goto L1
145 L5:
146 return 1
147
148 [case testUnreachableStatementAfterBreak]
149 def c() -> bool:
150 return False
151
152 def f() -> None:
153 n = True
154 while n:
155 if c():
156 break
157 if int():
158 f()
159 n = False
160 [out]
161 def c():
162 L0:
163 return 0
164 def f():
165 n, r0 :: bool
166 L0:
167 n = 1
168 L1:
169 if n goto L2 else goto L5 :: bool
170 L2:
171 r0 = c()
172 if r0 goto L3 else goto L4 :: bool
173 L3:
174 goto L5
175 L4:
176 n = 0
177 goto L1
178 L5:
179 return 1
180
181 [case testUnreachableStatementAfterRaise]
182 def f(x: bool) -> int:
183 if x:
184 raise ValueError()
185 print('hello')
186 return 2
187 [out]
188 def f(x):
189 x :: bool
190 r0 :: object
191 r1 :: str
192 r2, r3 :: object
193 L0:
194 if x goto L1 else goto L2 :: bool
195 L1:
196 r0 = builtins :: module
197 r1 = 'ValueError'
198 r2 = CPyObject_GetAttr(r0, r1)
199 r3 = PyObject_Vectorcall(r2, 0, 0, 0)
200 CPy_Raise(r3)
201 unreachable
202 L2:
203 return 4
204
205 [case testUnreachableStatementAfterAssertFalse]
206 def f(x: bool) -> int:
207 if x:
208 assert False
209 print('hello')
210 return 2
211 [out]
212 def f(x):
213 x, r0 :: bool
214 r1 :: str
215 r2 :: object
216 r3 :: str
217 r4 :: object
218 r5 :: object[1]
219 r6 :: object_ptr
220 r7 :: object
221 L0:
222 if x goto L1 else goto L4 :: bool
223 L1:
224 if 0 goto L3 else goto L2 :: bool
225 L2:
226 r0 = raise AssertionError
227 unreachable
228 L3:
229 r1 = 'hello'
230 r2 = builtins :: module
231 r3 = 'print'
232 r4 = CPyObject_GetAttr(r2, r3)
233 r5 = [r1]
234 r6 = load_address r5
235 r7 = PyObject_Vectorcall(r4, r6, 1, 0)
236 keep_alive r1
237 L4:
238 return 4