Debian Codegraph @

tests/cli/folding/fold-stack-switching-gc.wast

raw
1 ;; RUN: wast --assert default,snapshot-folded --snapshot tests/snapshots % -f stack-switching
2
3 ;; This file tests validation only, focusing on GC types and subtyping.
4
5 ;;;;
6 ;;;;; WasmFX types
7 ;;;;
8
9
10 (module
11 (type $f (func))
12
13 (type $ft1 (sub (func (param (ref $f)) (result (ref func)))))
14 (type $ct1 (sub (cont $ft1)))
15
16 (type $ft2 (func (param (ref any)) (result (ref func))))
17 (type $ct2 (cont $ft2))
18
19 (type $ft3 (sub $ft1 (func (param (ref func)) (result (ref $f)))))
20 (type $ct3 (cont $ft3))
21
22 ;; Okay: Continuation types are covariant, have declared $ft3 <: $ft1
23 (type $ct_sub (sub $ct1 (cont $ft3)))
24
25 (func $test
26 (param $p1 (ref $ct1))
27 (param $p2 (ref $ct_sub))
28
29 ;; Okay: (ref $ct_sub) <: (ref $ct1)
30 (local.set $p1 (local.get $p2))
31
32 )
33
34 )
35
36 (assert_invalid
37 (module
38 (type $f (func))
39
40 (type $ft1 (sub (func (param (ref $f)) (result (ref func)))))
41 (type $ct1 (sub (cont $ft1)))
42
43 (type $ft2 (func (param (ref func)) (result (ref $f))))
44
45 ;; Error: $ft2 and ft1 have compatible types, but have not declared $ft2 <: ft1
46 (type $error (sub $ct1 (cont $ft2)))
47
48 )
49 "sub type must match super type"
50 )
51
52 (assert_invalid
53 (module
54 (type $f (func))
55
56 (type $ft1 (sub (func (param (ref $f)) (result (ref func)))))
57 (type $ct1 (sub (cont $ft1)))
58
59 (type $ft2 (func (param (ref func)) (result (ref $f))))
60 (type $ct2 (cont $ft2))
61
62 (func $test
63 (param $p1 (ref $ct1))
64 (param $p2 (ref $ct2))
65
66 ;; Error $ct2 and $ct1 have generally compatible types,
67 ;; but have not declared $ft2 <: ft1 or $ct2 <: $ct1
68 ;; Thus, $ct2 </: $ct1.
69 (local.set $p1 (local.get $p2))
70
71 )
72
73 )
74 "type mismatch"
75 )
76
77 (assert_invalid
78 (module
79 (type $f (func))
80
81 (type $ft1 (sub (func (param (ref $f)) (result (ref func)))))
82 (type $ct1 (sub (cont $ft1)))
83 (type $ft3 (sub $ft1 (func (param (ref func)) (result (ref $f)))))
84 (type $ct3 (cont $ft3))
85
86 (func $error
87 (param $p1 (ref $ct1))
88 ;;(param $p2 (ref $ct2))
89 (param $p3 (ref $ct3))
90
91 ;; Error $ct3 and $ct1 have generally compatible types,
92 ;; (in particular: declared $ft3 <: ft1,
93 ;; but have not declared $ct3 <: $ct1
94 ;; $ct3 </: $ct1
95 (local.set $p1 (local.get $p3))
96
97 )
98
99 )
100 "type mismatch"
101 )
102
103 (assert_invalid
104 (module
105
106 (func $error
107 (param $p_any (ref any))
108 (param $p_cont (ref cont))
109
110 ;; Error: cont </: any
111 (local.set $p_any (local.get $p_cont))
112 )
113 )
114
115 "type mismatch"
116 )
117
118 ;;;;
119 ;;;; cont_bind instructions
120 ;;;;
121
122 (module $non_final
123
124 (type $ft1 (func (param i32) (result (ref func))))
125 (type $ct1 (sub (cont $ft1)))
126
127 (type $ft0 (func (result (ref func))))
128 (type $ct0 (sub (cont $ft0)))
129
130 (func $test1 (param $x (ref $ct1))
131 (i32.const 123)
132 (local.get $x)
133 ;; Smoke test: using non-final types here
134 (cont.bind $ct1 $ct0)
135 (drop)
136 )
137 )
138
139
140 (module $subtyping0
141
142 (type $f (func))
143
144 (type $ft0_sup (func (result (ref func))))
145 (type $ct0_sup (cont $ft0_sup))
146
147 (type $ft1 (func (param i32) (result (ref $f))))
148 (type $ct1 (cont $ft1))
149
150 (type $ft0 (func (result (ref $f))))
151 (type $ct0 (cont $ft0))
152
153
154 (func $test2 (param $x (ref $ct1))
155 (i32.const 123)
156 (local.get $x)
157 ;; Okay: The most natural second continuation type would be $ct0.
158 ;; But we have (func (result (ref $f))) <: (func (result (ref $func)))
159 ;; This holds without us needing to declare any subtyping relations at all.
160 (cont.bind $ct1 $ct0_sup)
161 (drop)
162 )
163
164 )
165
166 (module $recursive
167
168 (rec
169 (type $ft0 (func (result (ref $ct_rec))))
170 (type $ft1 (func (param i32) (result (ref $ct_rec))))
171 (type $ct_rec (cont $ft1))
172 )
173 (type $ct0 (cont $ft0))
174
175 (rec
176 (type $ft0' (func (result (ref $ct_rec'))))
177 (type $ft1' (func (param i32) (result (ref $ct_rec'))))
178 (type $ct_rec' (cont $ft1'))
179 )
180 (type $ct1 (cont $ft1'))
181
182
183 ;; Okay: Some simple test where the types involved in cont.bind
184 ;; are part of a recursion group
185 ;; (more concretely: two equivalent recursion groups)
186 (func $test (param $x (ref $ct1))
187 (i32.const 123)
188 (local.get $x)
189 (cont.bind $ct1 $ct0)
190 (drop)
191 )
192
193 )
194
195 (module $recursive_subtyping
196
197 ;; We define types such that $ft0 <: $ft0_sup and $ct_rec <: $ct_rec_sup
198 (rec
199 (type $ft0_sup (sub (func (result (ref $ct_rec_sup)))))
200 (type $ft0 (sub $ft0_sup (func (result (ref $ct_rec)))))
201 (type $ft1 (sub (func (param i32) (result (ref $ct_rec)))))
202
203 (type $ct_rec_sup (sub (cont $ft0_sup)))
204 (type $ct_rec (sub $ct_rec_sup (cont $ft0)))
205 )
206
207 (type $ct0_sup (cont $ft0_sup))
208 (type $ct0 (cont $ft0))
209 (type $ct1 (cont $ft1))
210
211
212 (func $test (param $x (ref $ct1))
213 (i32.const 123)
214 (local.get $x)
215 (cont.bind $ct1 $ct0)
216 (drop)
217
218 (i32.const 123)
219 (local.get $x)
220 ;; Okay: We have (func (result (ref $ct_rec))) <: (func (result (ref
221 ;; $ct_rec_sup)))
222 (cont.bind $ct1 $ct0_sup)
223 (drop)
224 )
225
226 )
227
228 ;;;;
229 ;;;; resume instructions
230 ;;;;
231
232
233 (module $subtyping-resume0
234
235 (type $ft0 (func))
236 (type $ct0 (cont $ft0))
237
238 (type $ft_sup (func (param (ref $ft0))))
239 (type $ct_sup (cont $ft_sup))
240
241 (type $ft_sub (func (param (ref func))))
242 (type $ct_sub (cont $ft_sub)) ;; unused
243
244 (tag $t (result (ref func)))
245
246 (func $test0
247 (param $p (ref $ct0))
248
249 ;; Here we test subtyping with respect to the continuations received by handlers.
250 ;;
251 ;; The most "straightforward" type of the continuation in $handler would be (ref
252 ;; $ct_sub). Instead, we use $ct_sup. According to the spec, we neither need
253 ;; to declare $ft_sub <: $ft_sup or $ct_sub <: $ct_sup for this to work. We
254 ;; have (func (param (ref func))) <: (func (param (ref $ft0))), which is
255 ;; sufficient
256 (block $handler (result (ref $ct_sup))
257 (local.get $p)
258 (resume $ct0 (on $t $handler))
259 (return)
260 )
261 (unreachable)
262 )
263 )
264
265 (module $subtyping-resume1
266
267 (type $ft0 (func))
268 (type $ct0 (cont $ft0))
269
270 (tag $t (param (ref $ft0)))
271
272 (func $test0
273 (param $p (ref $ct0))
274
275 ;; Here we test subtyping with respect to the payloads received by handlers.
276 ;; Instead of just (ref $ft0), then handler takes func.
277 (block $handler (result (ref func) (ref $ct0))
278 (local.get $p)
279 (resume $ct0 (on $t $handler))
280 (return)
281 )
282 (unreachable)
283 )
284
285 )
286
287 (assert_invalid
288 (module
289
290 (type $ft0 (func))
291 (type $ct0 (cont $ft0))
292
293 (type $ft_sup (sub (func (param (ref $ft0)))))
294 (type $ct_sup (sub (cont $ft_sup)))
295
296 (type $ft_sub (sub $ft_sup (func (param (ref func)))))
297 (type $ct_sub (cont $ft_sub))
298
299 (tag $t (param (ref $ct_sub)))
300
301 (func $test0
302 (param $p (ref $ct0))
303
304 ;; This is similar to $subtyping1, but this time we use a continuation as payload.
305 ;; But we did not actually declare $ct_sub <: $ct_sub.
306 ;;
307 ;; This is mostly just to check the following:
308 ;; For the continuation received by every handler, we see through the
309 ;; continuation type and do structural subtyping on the underlying
310 ;; function type.
311 ;; However, for continuations that are just payloads ($ct_sup here), we do
312 ;; ordinary nominal subtyping.
313 (block $handler (result (ref $ct_sup) (ref $ct0))
314 (local.get $p)
315 (resume $ct0 (on $t $handler))
316 (return)
317 )
318 (unreachable)
319 )
320 )
321 "type mismatch"
322 )