1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
--- destructuring-group-1 ---
// This wasn't allowed.
#let ((x)) = 1
#test(x, 1)
--- destructuring-group-2 ---
// This also wasn't allowed.
#let ((a, b)) = (1, 2)
#test(a, 1)
#test(b, 2)
--- destructuring-dict-underscore ---
// Here, `best` was accessed as a variable, where it shouldn't have.
#{
(best: _) = (best: "brr")
}
--- destructuring-dict-array-at ---
// Same here.
#{
let array = (1, 2, 3, 4)
(test: array.at(1), best: _) = (test: "baz", best: "brr")
test(array, (1, "baz", 3, 4))
}
--- destructuring-dict-bad ---
// Error: 7-10 expected identifier, found group
// Error: 12-14 expected pattern, found integer
#let ((a): 10) = "world"
--- destructuring-bad-duplicate ---
// Here, `a` is not duplicate, where it was previously identified as one.
#let f((a: b), (c,), a) = (a, b, c)
#test(f((a: 1), (2,), 3), (3, 1, 2))
--- destructuring-non-atomic ---
// Ensure that we can't have non-atomic destructuring.
#let x = 1
#let c = [#() = ()]
#test(c.children.last(), [()])
--- destructuring-let-array ---
// Simple destructuring.
#let (a, b) = (1, 2)
#test(a, 1)
#test(b, 2)
--- destructuring-let-array-single-item ---
#let (a,) = (1,)
#test(a, 1)
--- destructuring-let-array-placeholders ---
// Destructuring with multiple placeholders.
#let (a, _, c, _) = (1, 2, 3, 4)
#test(a, 1)
#test(c, 3)
--- destructuring-let-array-with-sink-at-end ---
// Destructuring with a sink.
#let (a, b, ..c) = (1, 2, 3, 4, 5, 6)
#test(a, 1)
#test(b, 2)
#test(c, (3, 4, 5, 6))
--- destructuring-let-array-with-sink-in-middle ---
// Destructuring with a sink in the middle.
#let (a, ..b, c) = (1, 2, 3, 4, 5, 6)
#test(a, 1)
#test(b, (2, 3, 4, 5))
#test(c, 6)
--- destructuring-let-array-with-sink-at-start-empty ---
// Destructuring with an empty sink.
#let (..a, b, c) = (1, 2)
#test(a, ())
#test(b, 1)
#test(c, 2)
--- destructuring-let-array-with-sink-in-middle-empty ---
// Destructuring with an empty sink.
#let (a, ..b, c) = (1, 2)
#test(a, 1)
#test(b, ())
#test(c, 2)
--- destructuring-let-array-with-sink-at-end-empty ---
// Destructuring with an empty sink.
#let (a, b, ..c) = (1, 2)
#test(a, 1)
#test(b, 2)
#test(c, ())
--- destructuring-let-array-with-sink-empty ---
// Destructuring with an empty sink and empty array.
#let (..a) = ()
#test(a, ())
--- destructuring-let-array-with-unnamed-sink ---
// Destructuring with unnamed sink.
#let (a, .., b) = (1, 2, 3, 4)
#test(a, 1)
#test(b, 4)
// Error: 10-11 duplicate binding: a
#let (a, a) = (1, 2)
// Error: 12-15 only one destructuring sink is allowed
#let (..a, ..a) = (1, 2)
// Error: 12-13 duplicate binding: a
#let (a, ..a) = (1, 2)
// Error: 13-14 duplicate binding: a
#let (a: a, a) = (a: 1, b: 2)
// Error: 13-20 expected pattern, found function call
#let (a, b: b.at(0)) = (a: 1, b: 2)
// Error: 7-14 expected pattern, found function call
#let (a.at(0),) = (1,)
--- destructuring-let-empty-array ---
#let () = ()
--- destructuring-let-empty-array-too-many-elements ---
// Error: 6-8 too many elements to destructure
// Hint: 6-8 the provided array has a length of 2, but the pattern expects an empty array
#let () = (1, 2)
--- destructuring-let-array-too-few-elements ---
// Error: 6-15 not enough elements to destructure
// Hint: 6-15 the provided array has a length of 2, but the pattern expects 3 elements
#let (a, b, c) = (1, 2)
--- destructuring-let-array-too-few-elements-with-sink ---
// Error: 6-20 not enough elements to destructure
// Hint: 6-20 the provided array has a length of 2, but the pattern expects at least 3 elements
#let (..a, b, c, d) = (1, 2)
--- destructuring-let-array-too-few-elements-with-sink-1-element ---
// Error: 6-14 not enough elements to destructure
// Hint: 6-14 the provided array has a length of 0, but the pattern expects at least 1 element
#let (..a, b) = ()
--- destructuring-let-array-bool-invalid ---
// Error: 6-12 cannot destructure boolean
#let (a, b) = true
--- destructuring-let-dict ---
// Simple destructuring.
#let (a: a, b, x: c) = (a: 1, b: 2, x: 3)
#test(a, 1)
#test(b, 2)
#test(c, 3)
--- destructuring-let-dict-with-sink-at-end ---
// Destructuring with a sink.
#let (a: _, ..b) = (a: 1, b: 2, c: 3)
#test(b, (b: 2, c: 3))
--- destructuring-let-dict-with-sink-in-middle ---
// Destructuring with a sink in the middle.
#let (a: _, ..b, c: _) = (a: 1, b: 2, c: 3)
#test(b, (b: 2))
--- destructuring-let-dict-with-sink-at-end-empty ---
// Destructuring with an empty sink.
#let (a: _, ..b) = (a: 1)
#test(b, (:))
--- destructuring-let-dict-with-sink-empty ---
// Destructuring with an empty sink and empty dict.
#let (..a) = (:)
#test(a, (:))
--- destructuring-let-dict-with-unnamed-sink ---
// Destructuring with unnamed sink.
#let (a, ..) = (a: 1, b: 2)
#test(a, 1)
--- destructuring-let-nested ---
// Nested destructuring.
#let ((a, b), (key: c)) = ((1, 2), (key: 3))
#test((a, b, c), (1, 2, 3))
--- destructuring-let-dict-key-string-invalid ---
// Keyed destructuring is not currently supported.
// Error: 7-18 expected pattern, found string
#let ("spacy key": val) = ("spacy key": 123)
#val
--- destructuring-let-dict-key-expr-invalid ---
// Keyed destructuring is not currently supported.
#let x = "spacy key"
// Error: 7-10 expected identifier, found group
#let ((x): v) = ("spacy key": 123)
--- destructuring-let-array-trailing-placeholders ---
// Trailing placeholders.
// Error: 6-21 not enough elements to destructure
// Hint: 6-21 the provided array has a length of 1, but the pattern expects 5 elements
#let (a, _, _, _, _) = (1,)
#test(a, 1)
--- destructuring-let-dict-patterns-invalid ---
// Error: 10-13 expected pattern, found string
// Error: 18-19 expected pattern, found integer
#let (a: "a", b: 2) = (a: 1, b: 2)
--- destructuring-let-dict-shorthand-missing-key ---
// Error: 10-11 dictionary does not contain key "b"
#let (a, b) = (a: 1)
--- destructuring-let-dict-missing-key ---
// Error: 10-11 dictionary does not contain key "b"
#let (a, b: b) = (a: 1)
--- destructuring-let-dict-from-array ---
// Error: 7-11 cannot destructure named pattern from an array
#let (a: a, b) = (1, 2, 3)
--- destructuring-during-loop-continue ---
// Test continue while destructuring.
// Should output "one = I \ two = II \ one = I".
#for num in (1, 2, 3, 1) {
let (word, roman) = if num == 1 {
("one", "I")
} else if num == 2 {
("two", "II")
} else {
continue
}
[#word = #roman \ ]
}
--- destructuring-assign ---
// Test destructuring assignments.
#let a = none
#let b = none
#let c = none
#((a,) = (1,))
#test(a, 1)
#((_, a, b, _) = (1, 2, 3, 4))
#test(a, 2)
#test(b, 3)
#((a, b, ..c) = (1, 2, 3, 4, 5, 6))
#test(a, 1)
#test(b, 2)
#test(c, (3, 4, 5, 6))
#((a: a, b, x: c) = (a: 1, b: 2, x: 3))
#test(a, 1)
#test(b, 2)
#test(c, 3)
#let a = (1, 2)
#((a: a.at(0), b) = (a: 3, b: 4))
#test(a, (3, 2))
#test(b, 4)
#let a = (1, 2)
#((a.at(0), b) = (3, 4))
#test(a, (3, 2))
#test(b, 4)
#((a, ..b) = (1, 2, 3, 4))
#test(a, 1)
#test(b, (2, 3, 4))
#let a = (1, 2)
#((b, ..a.at(0)) = (1, 2, 3, 4))
#test(a, ((2, 3, 4), 2))
#test(b, 1)
--- destructuring-assign-commas ---
// Test comma placement in destructuring assignment.
#let array = (1, 2, 3)
#((key: array.at(1)) = (key: "hi"))
#test(array, (1, "hi", 3))
#let array = (1, 2, 3)
#((array.at(1)) = ("hi"))
#test(array, (1, "hi", 3))
#let array = (1, 2, 3)
#((array.at(1),) = ("hi",))
#test(array, (1, "hi", 3))
#let array = (1, 2, 3)
#((array.at(1)) = ("hi",))
#test(array, (1, ("hi",), 3))
--- destructuring-assign-nested ---
// Test nested destructuring assignment.
#let a
#let b
#let c
#(((a, b), (key: c)) = ((1, 2), (key: 3)))
#test((a, b, c), (1, 2, 3))
--- destructuring-assign-nested-invalid ---
#let array = (1, 2, 3)
// Error: 3-17 cannot destructure string
#((array.at(1),) = ("hi"))
#test(array, (1, ("hi",), 3))
--- issue-3275-normal-variable ---
// Normal variable.
#for x in (1, 2) {}
#for x in (a: 1, b: 2) {}
#for x in "foo" {}
#for x in bytes("😊") {}
--- issue-3275-placeholder ---
// Placeholder.
#for _ in (1, 2) {}
#for _ in (a: 1, b: 2) {}
#for _ in "foo" {}
#for _ in bytes("😊") {}
--- issue-3275-destructuring ---
// Destructuring.
#for (a,b,c) in (("a", 1, bytes(())), ("b", 2, bytes(""))) {}
#for (a, ..) in (("a", 1, bytes(())), ("b", 2, bytes(""))) {}
#for (k, v) in (a: 1, b: 2, c: 3) {}
#for (.., v) in (a: 1, b: 2, c: 3) {}
--- issue-3275-loop-over-content ---
// Error: 11-17 cannot loop over content
#for x in [1, 2] {}
--- issue-3275-loop-over-arguments ---
// Error: 11-25 cannot loop over arguments
#for _ in arguments("a") {}
--- issue-3275-loop-over-integer ---
// Error: 16-21 cannot loop over integer
#for (x, y) in 12306 {}
--- issue-3275-destructuring-loop-over-content ---
// Error: 16-22 cannot loop over content
#for (x, y) in [1, 2] {}
--- issue-3275-destructuring-loop-over-string ---
// Error: 6-12 cannot destructure values of string
#for (x, y) in "foo" {}
--- issue-3275-destructuring-loop-over-string-array ---
// Error: 6-12 cannot destructure string
#for (x, y) in ("foo", "bar") {}
--- issue-3275-destructuring-loop-over-bytes ---
// Error: 6-12 cannot destructure values of bytes
#for (x, y) in bytes("😊") {}
--- issue-3275-destructuring-loop-over-bytes-array ---
// Error: 6-12 cannot destructure bytes
#for (x, y) in (bytes((1,2)), bytes((1,2))) {}
--- issue-3275-destructuring-loop-over-int-array ---
// Error: 6-12 cannot destructure integer
#for (x, y) in (1, 2) {}
--- issue-3275-destructuring-loop-over-2d-array-1 ---
// Error: 6-12 not enough elements to destructure
// Hint: 6-12 the provided array has a length of 1, but the pattern expects 2 elements
#for (x, y) in ((1,), (2,)) {}
--- issue-3275-destructuring-loop-over-2d-array-2 ---
// Error: 6-12 too many elements to destructure
// Hint: 6-12 the provided array has a length of 3, but the pattern expects 2 elements
#for (x, y) in ((1,2,3), (4,5,6)) {}
--- issue-4573-destructuring-unclosed-delimiter ---
// Tests a case where parsing within an incorrectly predicted paren expression
// (the "outer" assignment) would put the parser in an invalid state when
// reloading a stored prediction (the "inner" assignment) and cause a panic when
// generating the unclosed delimiter error. See the comment in the issue for
// more details.
#{
(
// Error: 5-7 expected pattern, found keyword `if`
// Hint: 5-7 keyword `if` is not allowed as an identifier; try `if_` instead
// Error: 9 expected comma
// Error: 13-17 unexpected keyword `else`
// Error: 20 expected comma
if x {} else {}
// Error: 5-6 unclosed delimiter
{ () = "inner"
) = "outer"
}
|