microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
minestarks-patch-1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

compiler/qsc_frontend/src/lower/tests.rs

2566lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use crate::compile::{self, compile, PackageStore, SourceMap};
5use expect_test::{expect, Expect};
6use indoc::indoc;
7use qsc_data_structures::{language_features::LanguageFeatures, target::TargetCapabilityFlags};
8
9fn check_hir(input: &str, expect: &Expect) {
10 let sources = SourceMap::new([("test".into(), input.into())], None);
11 let unit = compile(
12 &PackageStore::new(compile::core()),
13 &[],
14 sources,
15 TargetCapabilityFlags::all(),
16 LanguageFeatures::default(),
17 );
18 expect.assert_eq(&unit.package.to_string());
19}
20
21fn check_errors(input: &str, expect: &Expect) {
22 let sources = SourceMap::new([("test".into(), input.into())], None);
23 let unit = compile(
24 &PackageStore::new(compile::core()),
25 &[],
26 sources,
27 TargetCapabilityFlags::all(),
28 LanguageFeatures::default(),
29 );
30
31 let lower_errors: Vec<_> = unit
32 .errors
33 .into_iter()
34 .filter_map(try_into_lower_error)
35 .collect();
36
37 expect.assert_debug_eq(&lower_errors);
38}
39
40fn try_into_lower_error(error: compile::Error) -> Option<super::Error> {
41 if let compile::ErrorKind::Lower(error) = error.0 {
42 Some(error)
43 } else {
44 None
45 }
46}
47
48#[test]
49fn test_entrypoint_attr_allowed() {
50 check_errors(
51 indoc! {"
52 namespace input {
53 @EntryPoint()
54 operation Foo() : Unit {
55 body ... {}
56 }
57 }
58 "},
59 &expect![[r#"
60 []
61 "#]],
62 );
63}
64
65#[test]
66fn test_entrypoint_attr_wrong_args() {
67 check_errors(
68 indoc! {r#"
69 namespace input {
70 @EntryPoint("Bar")
71 operation Foo() : Unit {
72 body ... {}
73 }
74 }
75 "#},
76 &expect![[r#"
77 [
78 InvalidAttrArgs(
79 "()",
80 Span {
81 lo: 33,
82 hi: 40,
83 },
84 ),
85 ]
86 "#]],
87 );
88}
89
90#[test]
91fn test_target_profile_base_attr_allowed() {
92 check_errors(
93 indoc! {"
94 namespace input {
95 @Config(Base)
96 operation Foo() : Unit {
97 body ... {}
98 }
99 }
100 "},
101 &expect![[r#"
102 []
103 "#]],
104 );
105}
106
107#[test]
108fn test_target_profile_attr_wrong_args() {
109 check_errors(
110 indoc! {"
111 namespace input {
112 @Config(Bar)
113 operation Foo() : Unit {
114 body ... {}
115 }
116 }
117 "},
118 &expect![[r#"
119 [
120 InvalidAttrArgs(
121 "runtime capability",
122 Span {
123 lo: 29,
124 hi: 34,
125 },
126 ),
127 ]
128 "#]],
129 );
130}
131
132#[test]
133fn test_unknown_attr() {
134 check_errors(
135 indoc! {"
136 namespace input {
137 @Bar()
138 operation Foo() : Unit {
139 body ... {}
140 }
141 }
142 "},
143 &expect![[r#"
144 [
145 UnknownAttr(
146 "Bar",
147 Span {
148 lo: 23,
149 hi: 26,
150 },
151 ),
152 ]
153 "#]],
154 );
155}
156
157#[test]
158fn lift_local_function() {
159 check_hir(
160 indoc! {"
161 namespace A {
162 function Foo(x : Int) : Int {
163 function Bar(y : Int) : Int { y + 1 }
164 Bar(x + 2)
165 }
166 }
167 "},
168 &expect![[r#"
169 Package:
170 Item 0 [0-120] (Public):
171 Namespace (Ident 23 [10-11] "A"): Item 1
172 Item 1 [18-118] (Internal):
173 Parent: 0
174 Callable 0 [18-118] (function):
175 name: Ident 1 [27-30] "Foo"
176 input: Pat 2 [31-38] [Type Int]: Bind: Ident 3 [31-32] "x"
177 output: Int
178 functors: empty set
179 body: SpecDecl 4 [18-118]: Impl:
180 Block 5 [46-118] [Type Int]:
181 Stmt 6 [56-93]: Item: 2
182 Stmt 17 [102-112]: Expr: Expr 18 [102-112] [Type Int]: Call:
183 Expr 19 [102-105] [Type (Int -> Int)]: Var: Item 2
184 Expr 20 [106-111] [Type Int]: BinOp (Add):
185 Expr 21 [106-107] [Type Int]: Var: Local 3
186 Expr 22 [110-111] [Type Int]: Lit: Int(2)
187 adj: <none>
188 ctl: <none>
189 ctl-adj: <none>
190 Item 2 [56-93] (Internal):
191 Parent: 1
192 Callable 7 [56-93] (function):
193 name: Ident 8 [65-68] "Bar"
194 input: Pat 9 [69-76] [Type Int]: Bind: Ident 10 [69-70] "y"
195 output: Int
196 functors: empty set
197 body: SpecDecl 11 [56-93]: Impl:
198 Block 12 [84-93] [Type Int]:
199 Stmt 13 [86-91]: Expr: Expr 14 [86-91] [Type Int]: BinOp (Add):
200 Expr 15 [86-87] [Type Int]: Var: Local 10
201 Expr 16 [90-91] [Type Int]: Lit: Int(1)
202 adj: <none>
203 ctl: <none>
204 ctl-adj: <none>"#]],
205 );
206}
207
208#[test]
209fn lift_local_operation() {
210 check_hir(
211 indoc! {"
212 namespace A {
213 operation Foo() : Result {
214 operation Bar(q : Qubit) : Result { Zero }
215 use q = Qubit();
216 Bar(q)
217 }
218 }
219 "},
220 &expect![[r#"
221 Package:
222 Item 0 [0-143] (Public):
223 Namespace (Ident 22 [10-11] "A"): Item 1
224 Item 1 [18-141] (Internal):
225 Parent: 0
226 Callable 0 [18-141] (operation):
227 name: Ident 1 [28-31] "Foo"
228 input: Pat 2 [31-33] [Type Unit]: Unit
229 output: Result
230 functors: empty set
231 body: SpecDecl 3 [18-141]: Impl:
232 Block 4 [43-141] [Type Result]:
233 Stmt 5 [53-95]: Item: 2
234 Stmt 14 [104-120]: Qubit (Fresh)
235 Pat 15 [108-109] [Type Qubit]: Bind: Ident 16 [108-109] "q"
236 QubitInit 17 [112-119] [Type Qubit]: Single
237 Stmt 18 [129-135]: Expr: Expr 19 [129-135] [Type Result]: Call:
238 Expr 20 [129-132] [Type (Qubit => Result)]: Var: Item 2
239 Expr 21 [133-134] [Type Qubit]: Var: Local 16
240 adj: <none>
241 ctl: <none>
242 ctl-adj: <none>
243 Item 2 [53-95] (Internal):
244 Parent: 1
245 Callable 6 [53-95] (operation):
246 name: Ident 7 [63-66] "Bar"
247 input: Pat 8 [67-76] [Type Qubit]: Bind: Ident 9 [67-68] "q"
248 output: Result
249 functors: empty set
250 body: SpecDecl 10 [53-95]: Impl:
251 Block 11 [87-95] [Type Result]:
252 Stmt 12 [89-93]: Expr: Expr 13 [89-93] [Type Result]: Lit: Result(Zero)
253 adj: <none>
254 ctl: <none>
255 ctl-adj: <none>"#]],
256 );
257}
258
259#[test]
260fn lift_local_newtype() {
261 check_hir(
262 indoc! {"
263 namespace A {
264 function Foo() : Int {
265 newtype Bar = Int;
266 let x = Bar(5);
267 x!
268 }
269 }
270 "},
271 &expect![[r#"
272 Package:
273 Item 0 [0-110] (Public):
274 Namespace (Ident 16 [10-11] "A"): Item 1
275 Item 1 [18-108] (Internal):
276 Parent: 0
277 Callable 0 [18-108] (function):
278 name: Ident 1 [27-30] "Foo"
279 input: Pat 2 [30-32] [Type Unit]: Unit
280 output: Int
281 functors: empty set
282 body: SpecDecl 3 [18-108]: Impl:
283 Block 4 [39-108] [Type Int]:
284 Stmt 5 [49-67]: Item: 2
285 Stmt 7 [76-91]: Local (Immutable):
286 Pat 8 [80-81] [Type UDT<"Bar": Item 2>]: Bind: Ident 9 [80-81] "x"
287 Expr 10 [84-90] [Type UDT<"Bar": Item 2>]: Call:
288 Expr 11 [84-87] [Type (Int -> UDT<"Bar": Item 2>)]: Var: Item 2
289 Expr 12 [88-89] [Type Int]: Lit: Int(5)
290 Stmt 13 [100-102]: Expr: Expr 14 [100-102] [Type Int]: UnOp (Unwrap):
291 Expr 15 [100-101] [Type UDT<"Bar": Item 2>]: Var: Local 9
292 adj: <none>
293 ctl: <none>
294 ctl-adj: <none>
295 Item 2 [49-67] (Internal):
296 Parent: 1
297 Type (Ident 6 [57-60] "Bar"): UDT [49-67]:
298 TyDef [63-66]: Field:
299 type: Int"#]],
300 );
301}
302
303#[test]
304fn lift_newtype() {
305 check_hir(
306 indoc! {"
307 namespace A {
308 newtype Foo = Int;
309 operation Bar() : Unit {
310 let x = Foo(1);
311 }
312 }
313 "},
314 &expect![[r#"
315 Package:
316 Item 0 [0-97] (Public):
317 Namespace (Ident 12 [10-11] "A"): Item 1, Item 2
318 Item 1 [18-36] (Internal):
319 Parent: 0
320 Type (Ident 0 [26-29] "Foo"): UDT [18-36]:
321 TyDef [32-35]: Field:
322 type: Int
323 Item 2 [41-95] (Internal):
324 Parent: 0
325 Callable 1 [41-95] (operation):
326 name: Ident 2 [51-54] "Bar"
327 input: Pat 3 [54-56] [Type Unit]: Unit
328 output: Unit
329 functors: empty set
330 body: SpecDecl 4 [41-95]: Impl:
331 Block 5 [64-95] [Type Unit]:
332 Stmt 6 [74-89]: Local (Immutable):
333 Pat 7 [78-79] [Type UDT<"Foo": Item 1>]: Bind: Ident 8 [78-79] "x"
334 Expr 9 [82-88] [Type UDT<"Foo": Item 1>]: Call:
335 Expr 10 [82-85] [Type (Int -> UDT<"Foo": Item 1>)]: Var: Item 1
336 Expr 11 [86-87] [Type Int]: Lit: Int(1)
337 adj: <none>
338 ctl: <none>
339 ctl-adj: <none>"#]],
340 );
341}
342
343#[test]
344fn lift_newtype_tuple() {
345 check_hir(
346 indoc! {"
347 namespace A {
348 newtype Foo = (Int, Double);
349 operation Bar() : Unit {
350 let x = Foo(1, 2.3);
351 }
352 }
353 "},
354 &expect![[r#"
355 Package:
356 Item 0 [0-112] (Public):
357 Namespace (Ident 14 [10-11] "A"): Item 1, Item 2
358 Item 1 [18-46] (Internal):
359 Parent: 0
360 Type (Ident 0 [26-29] "Foo"): UDT [18-46]:
361 TyDef [32-45]: Field:
362 type: (Int, Double)
363 Item 2 [51-110] (Internal):
364 Parent: 0
365 Callable 1 [51-110] (operation):
366 name: Ident 2 [61-64] "Bar"
367 input: Pat 3 [64-66] [Type Unit]: Unit
368 output: Unit
369 functors: empty set
370 body: SpecDecl 4 [51-110]: Impl:
371 Block 5 [74-110] [Type Unit]:
372 Stmt 6 [84-104]: Local (Immutable):
373 Pat 7 [88-89] [Type UDT<"Foo": Item 1>]: Bind: Ident 8 [88-89] "x"
374 Expr 9 [92-103] [Type UDT<"Foo": Item 1>]: Call:
375 Expr 10 [92-95] [Type ((Int, Double) -> UDT<"Foo": Item 1>)]: Var: Item 1
376 Expr 11 [95-103] [Type (Int, Double)]: Tuple:
377 Expr 12 [96-97] [Type Int]: Lit: Int(1)
378 Expr 13 [99-102] [Type Double]: Lit: Double(2.3)
379 adj: <none>
380 ctl: <none>
381 ctl-adj: <none>"#]],
382 );
383}
384
385#[test]
386fn lift_newtype_tuple_fields() {
387 check_hir(
388 indoc! {"
389 namespace A {
390 newtype Foo = (a: Int, b: Double);
391 operation Bar() : Unit {
392 let x = Foo(1, 2.3);
393 let y = x::b;
394 }
395 }
396 "},
397 &expect![[r#"
398 Package:
399 Item 0 [0-140] (Public):
400 Namespace (Ident 19 [10-11] "A"): Item 1, Item 2
401 Item 1 [18-52] (Internal):
402 Parent: 0
403 Type (Ident 0 [26-29] "Foo"): UDT [18-52]:
404 TyDef [32-51]: Tuple:
405 TyDef [33-39]: Field:
406 name: a [33-34]
407 type: Int
408 TyDef [41-50]: Field:
409 name: b [41-42]
410 type: Double
411 Item 2 [57-138] (Internal):
412 Parent: 0
413 Callable 1 [57-138] (operation):
414 name: Ident 2 [67-70] "Bar"
415 input: Pat 3 [70-72] [Type Unit]: Unit
416 output: Unit
417 functors: empty set
418 body: SpecDecl 4 [57-138]: Impl:
419 Block 5 [80-138] [Type Unit]:
420 Stmt 6 [90-110]: Local (Immutable):
421 Pat 7 [94-95] [Type UDT<"Foo": Item 1>]: Bind: Ident 8 [94-95] "x"
422 Expr 9 [98-109] [Type UDT<"Foo": Item 1>]: Call:
423 Expr 10 [98-101] [Type ((Int, Double) -> UDT<"Foo": Item 1>)]: Var: Item 1
424 Expr 11 [101-109] [Type (Int, Double)]: Tuple:
425 Expr 12 [102-103] [Type Int]: Lit: Int(1)
426 Expr 13 [105-108] [Type Double]: Lit: Double(2.3)
427 Stmt 14 [119-132]: Local (Immutable):
428 Pat 15 [123-124] [Type Double]: Bind: Ident 16 [123-124] "y"
429 Expr 17 [127-131] [Type Double]: Field:
430 Expr 18 [127-128] [Type UDT<"Foo": Item 1>]: Var: Local 8
431 Path(FieldPath { indices: [1] })
432 adj: <none>
433 ctl: <none>
434 ctl-adj: <none>"#]],
435 );
436}
437
438#[test]
439fn lift_newtype_nested_tuple() {
440 check_hir(
441 indoc! {"
442 namespace A {
443 newtype Foo = (Int, (Double, Bool));
444 operation Bar() : Unit {
445 let x = Foo(1, (2.3, true));
446 }
447 }
448 "},
449 &expect![[r#"
450 Package:
451 Item 0 [0-128] (Public):
452 Namespace (Ident 16 [10-11] "A"): Item 1, Item 2
453 Item 1 [18-54] (Internal):
454 Parent: 0
455 Type (Ident 0 [26-29] "Foo"): UDT [18-54]:
456 TyDef [32-53]: Field:
457 type: (Int, (Double, Bool))
458 Item 2 [59-126] (Internal):
459 Parent: 0
460 Callable 1 [59-126] (operation):
461 name: Ident 2 [69-72] "Bar"
462 input: Pat 3 [72-74] [Type Unit]: Unit
463 output: Unit
464 functors: empty set
465 body: SpecDecl 4 [59-126]: Impl:
466 Block 5 [82-126] [Type Unit]:
467 Stmt 6 [92-120]: Local (Immutable):
468 Pat 7 [96-97] [Type UDT<"Foo": Item 1>]: Bind: Ident 8 [96-97] "x"
469 Expr 9 [100-119] [Type UDT<"Foo": Item 1>]: Call:
470 Expr 10 [100-103] [Type ((Int, (Double, Bool)) -> UDT<"Foo": Item 1>)]: Var: Item 1
471 Expr 11 [103-119] [Type (Int, (Double, Bool))]: Tuple:
472 Expr 12 [104-105] [Type Int]: Lit: Int(1)
473 Expr 13 [107-118] [Type (Double, Bool)]: Tuple:
474 Expr 14 [108-111] [Type Double]: Lit: Double(2.3)
475 Expr 15 [113-117] [Type Bool]: Lit: Bool(true)
476 adj: <none>
477 ctl: <none>
478 ctl-adj: <none>"#]],
479 );
480}
481
482#[test]
483fn lift_newtype_nested_tuple_fields() {
484 check_hir(
485 indoc! {"
486 namespace A {
487 newtype Foo = (a: Int, (b: Double, c: Bool));
488 operation Bar() : Unit {
489 let x = Foo(1, (2.3, true));
490 let y = x::c;
491 }
492 }
493 "},
494 &expect![[r#"
495 Package:
496 Item 0 [0-159] (Public):
497 Namespace (Ident 21 [10-11] "A"): Item 1, Item 2
498 Item 1 [18-63] (Internal):
499 Parent: 0
500 Type (Ident 0 [26-29] "Foo"): UDT [18-63]:
501 TyDef [32-62]: Tuple:
502 TyDef [33-39]: Field:
503 name: a [33-34]
504 type: Int
505 TyDef [41-61]: Tuple:
506 TyDef [42-51]: Field:
507 name: b [42-43]
508 type: Double
509 TyDef [53-60]: Field:
510 name: c [53-54]
511 type: Bool
512 Item 2 [68-157] (Internal):
513 Parent: 0
514 Callable 1 [68-157] (operation):
515 name: Ident 2 [78-81] "Bar"
516 input: Pat 3 [81-83] [Type Unit]: Unit
517 output: Unit
518 functors: empty set
519 body: SpecDecl 4 [68-157]: Impl:
520 Block 5 [91-157] [Type Unit]:
521 Stmt 6 [101-129]: Local (Immutable):
522 Pat 7 [105-106] [Type UDT<"Foo": Item 1>]: Bind: Ident 8 [105-106] "x"
523 Expr 9 [109-128] [Type UDT<"Foo": Item 1>]: Call:
524 Expr 10 [109-112] [Type ((Int, (Double, Bool)) -> UDT<"Foo": Item 1>)]: Var: Item 1
525 Expr 11 [112-128] [Type (Int, (Double, Bool))]: Tuple:
526 Expr 12 [113-114] [Type Int]: Lit: Int(1)
527 Expr 13 [116-127] [Type (Double, Bool)]: Tuple:
528 Expr 14 [117-120] [Type Double]: Lit: Double(2.3)
529 Expr 15 [122-126] [Type Bool]: Lit: Bool(true)
530 Stmt 16 [138-151]: Local (Immutable):
531 Pat 17 [142-143] [Type Bool]: Bind: Ident 18 [142-143] "y"
532 Expr 19 [146-150] [Type Bool]: Field:
533 Expr 20 [146-147] [Type UDT<"Foo": Item 1>]: Var: Local 8
534 Path(FieldPath { indices: [1, 1] })
535 adj: <none>
536 ctl: <none>
537 ctl-adj: <none>"#]],
538 );
539}
540
541#[test]
542fn lift_newtype_from_newtype() {
543 check_hir(
544 indoc! {"
545 namespace A {
546 newtype Foo = (a: Int, (b: Double, c: Bool));
547 newtype Bar = (x: Int, y: Foo);
548 operation Baz() : Unit {
549 let x = Bar(1, Foo(2, (3.4, false)));
550 let y = x::y::c;
551 }
552 }
553 "},
554 &expect![[r#"
555 Package:
556 Item 0 [0-207] (Public):
557 Namespace (Ident 27 [10-11] "A"): Item 1, Item 2, Item 3
558 Item 1 [18-63] (Internal):
559 Parent: 0
560 Type (Ident 0 [26-29] "Foo"): UDT [18-63]:
561 TyDef [32-62]: Tuple:
562 TyDef [33-39]: Field:
563 name: a [33-34]
564 type: Int
565 TyDef [41-61]: Tuple:
566 TyDef [42-51]: Field:
567 name: b [42-43]
568 type: Double
569 TyDef [53-60]: Field:
570 name: c [53-54]
571 type: Bool
572 Item 2 [68-99] (Internal):
573 Parent: 0
574 Type (Ident 1 [76-79] "Bar"): UDT [68-99]:
575 TyDef [82-98]: Tuple:
576 TyDef [83-89]: Field:
577 name: x [83-84]
578 type: Int
579 TyDef [91-97]: Field:
580 name: y [91-92]
581 type: UDT<"Foo": Item 1>
582 Item 3 [104-205] (Internal):
583 Parent: 0
584 Callable 2 [104-205] (operation):
585 name: Ident 3 [114-117] "Baz"
586 input: Pat 4 [117-119] [Type Unit]: Unit
587 output: Unit
588 functors: empty set
589 body: SpecDecl 5 [104-205]: Impl:
590 Block 6 [127-205] [Type Unit]:
591 Stmt 7 [137-174]: Local (Immutable):
592 Pat 8 [141-142] [Type UDT<"Bar": Item 2>]: Bind: Ident 9 [141-142] "x"
593 Expr 10 [145-173] [Type UDT<"Bar": Item 2>]: Call:
594 Expr 11 [145-148] [Type ((Int, UDT<"Foo": Item 1>) -> UDT<"Bar": Item 2>)]: Var: Item 2
595 Expr 12 [148-173] [Type (Int, UDT<"Foo": Item 1>)]: Tuple:
596 Expr 13 [149-150] [Type Int]: Lit: Int(1)
597 Expr 14 [152-172] [Type UDT<"Foo": Item 1>]: Call:
598 Expr 15 [152-155] [Type ((Int, (Double, Bool)) -> UDT<"Foo": Item 1>)]: Var: Item 1
599 Expr 16 [155-172] [Type (Int, (Double, Bool))]: Tuple:
600 Expr 17 [156-157] [Type Int]: Lit: Int(2)
601 Expr 18 [159-171] [Type (Double, Bool)]: Tuple:
602 Expr 19 [160-163] [Type Double]: Lit: Double(3.4)
603 Expr 20 [165-170] [Type Bool]: Lit: Bool(false)
604 Stmt 21 [183-199]: Local (Immutable):
605 Pat 22 [187-188] [Type Bool]: Bind: Ident 23 [187-188] "y"
606 Expr 24 [191-198] [Type Bool]: Field:
607 Expr 25 [191-195] [Type UDT<"Foo": Item 1>]: Field:
608 Expr 26 [191-192] [Type UDT<"Bar": Item 2>]: Var: Local 9
609 Path(FieldPath { indices: [1] })
610 Path(FieldPath { indices: [1, 1] })
611 adj: <none>
612 ctl: <none>
613 ctl-adj: <none>"#]],
614 );
615}
616
617#[test]
618fn lower_struct_decl() {
619 check_hir(
620 indoc! {"
621 namespace A {
622 struct Foo {
623 x: Int,
624 y: Double,
625 }
626 }
627 "},
628 &expect![[r#"
629 Package:
630 Item 0 [0-73] (Public):
631 Namespace (Ident 1 [10-11] "A"): Item 1
632 Item 1 [18-71] (Internal):
633 Parent: 0
634 Type (Ident 0 [25-28] "Foo"): UDT [18-71]:
635 TyDef [18-71]: Tuple:
636 TyDef [39-45]: Field:
637 name: x [39-40]
638 type: Int
639 TyDef [55-64]: Field:
640 name: y [55-56]
641 type: Double"#]],
642 );
643}
644
645#[test]
646fn lower_struct_constructor() {
647 check_hir(
648 indoc! {"
649 namespace A {
650 struct Foo {
651 x: Int,
652 y: Double,
653 }
654 operation Bar() : Unit {
655 let z = new Foo { x = 1, y = 2.3 };
656 }
657 }
658 "},
659 &expect![[r#"
660 Package:
661 Item 0 [0-152] (Public):
662 Namespace (Ident 14 [10-11] "A"): Item 1, Item 2
663 Item 1 [18-71] (Internal):
664 Parent: 0
665 Type (Ident 0 [25-28] "Foo"): UDT [18-71]:
666 TyDef [18-71]: Tuple:
667 TyDef [39-45]: Field:
668 name: x [39-40]
669 type: Int
670 TyDef [55-64]: Field:
671 name: y [55-56]
672 type: Double
673 Item 2 [76-150] (Internal):
674 Parent: 0
675 Callable 1 [76-150] (operation):
676 name: Ident 2 [86-89] "Bar"
677 input: Pat 3 [89-91] [Type Unit]: Unit
678 output: Unit
679 functors: empty set
680 body: SpecDecl 4 [76-150]: Impl:
681 Block 5 [99-150] [Type Unit]:
682 Stmt 6 [109-144]: Local (Immutable):
683 Pat 7 [113-114] [Type UDT<"Foo": Item 1>]: Bind: Ident 8 [113-114] "z"
684 Expr 9 [117-143] [Type UDT<"Foo": Item 1>]: Struct (Item 1):
685 FieldsAssign 10 [127-132]: (Path([0])) Expr 11 [131-132] [Type Int]: Lit: Int(1)
686 FieldsAssign 12 [134-141]: (Path([1])) Expr 13 [138-141] [Type Double]: Lit: Double(2.3)
687 adj: <none>
688 ctl: <none>
689 ctl-adj: <none>"#]],
690 );
691}
692
693#[test]
694fn lower_struct_copy_constructor() {
695 check_hir(
696 indoc! {"
697 namespace A {
698 struct Foo {
699 x: Int,
700 y: Double,
701 }
702 operation Bar() : Foo {
703 let z = new Foo { x = 1, y = 2.3 };
704 new Foo { ...z, x = 4 };
705 }
706 }
707 "},
708 &expect![[r#"
709 Package:
710 Item 0 [0-184] (Public):
711 Namespace (Ident 19 [10-11] "A"): Item 1, Item 2
712 Item 1 [18-71] (Internal):
713 Parent: 0
714 Type (Ident 0 [25-28] "Foo"): UDT [18-71]:
715 TyDef [18-71]: Tuple:
716 TyDef [39-45]: Field:
717 name: x [39-40]
718 type: Int
719 TyDef [55-64]: Field:
720 name: y [55-56]
721 type: Double
722 Item 2 [76-182] (Internal):
723 Parent: 0
724 Callable 1 [76-182] (operation):
725 name: Ident 2 [86-89] "Bar"
726 input: Pat 3 [89-91] [Type Unit]: Unit
727 output: UDT<"Foo": Item 1>
728 functors: empty set
729 body: SpecDecl 4 [76-182]: Impl:
730 Block 5 [98-182] [Type Unit]:
731 Stmt 6 [108-143]: Local (Immutable):
732 Pat 7 [112-113] [Type UDT<"Foo": Item 1>]: Bind: Ident 8 [112-113] "z"
733 Expr 9 [116-142] [Type UDT<"Foo": Item 1>]: Struct (Item 1):
734 FieldsAssign 10 [126-131]: (Path([0])) Expr 11 [130-131] [Type Int]: Lit: Int(1)
735 FieldsAssign 12 [133-140]: (Path([1])) Expr 13 [137-140] [Type Double]: Lit: Double(2.3)
736 Stmt 14 [152-176]: Semi: Expr 15 [152-175] [Type UDT<"Foo": Item 1>]: Struct (Item 1):
737 Copy: Expr 16 [165-166] [Type UDT<"Foo": Item 1>]: Var: Local 8
738 FieldsAssign 17 [168-173]: (Path([0])) Expr 18 [172-173] [Type Int]: Lit: Int(4)
739 adj: <none>
740 ctl: <none>
741 ctl-adj: <none>"#]],
742 );
743}
744
745#[test]
746fn lower_struct_copy_constructor_with_alternative_fields() {
747 check_hir(
748 indoc! {r#"
749 namespace A {
750 struct Foo {
751 x: Int,
752 y: Double,
753 z: String
754 }
755 operation Bar() : Foo {
756 let z = new Foo { x = 1, y = 2.3, z = "four" };
757 new Foo { ...z, z = "five", y = 6.7 };
758 }
759 }
760 "#},
761 &expect![[r#"
762 Package:
763 Item 0 [0-228] (Public):
764 Namespace (Ident 23 [10-11] "A"): Item 1, Item 2
765 Item 1 [18-89] (Internal):
766 Parent: 0
767 Type (Ident 0 [25-28] "Foo"): UDT [18-89]:
768 TyDef [18-89]: Tuple:
769 TyDef [39-45]: Field:
770 name: x [39-40]
771 type: Int
772 TyDef [55-64]: Field:
773 name: y [55-56]
774 type: Double
775 TyDef [74-83]: Field:
776 name: z [74-75]
777 type: String
778 Item 2 [94-226] (Internal):
779 Parent: 0
780 Callable 1 [94-226] (operation):
781 name: Ident 2 [104-107] "Bar"
782 input: Pat 3 [107-109] [Type Unit]: Unit
783 output: UDT<"Foo": Item 1>
784 functors: empty set
785 body: SpecDecl 4 [94-226]: Impl:
786 Block 5 [116-226] [Type Unit]:
787 Stmt 6 [126-173]: Local (Immutable):
788 Pat 7 [130-131] [Type UDT<"Foo": Item 1>]: Bind: Ident 8 [130-131] "z"
789 Expr 9 [134-172] [Type UDT<"Foo": Item 1>]: Struct (Item 1):
790 FieldsAssign 10 [144-149]: (Path([0])) Expr 11 [148-149] [Type Int]: Lit: Int(1)
791 FieldsAssign 12 [151-158]: (Path([1])) Expr 13 [155-158] [Type Double]: Lit: Double(2.3)
792 FieldsAssign 14 [160-170]: (Path([2])) Expr 15 [164-170] [Type String]: String:
793 Lit: "four"
794 Stmt 16 [182-220]: Semi: Expr 17 [182-219] [Type UDT<"Foo": Item 1>]: Struct (Item 1):
795 Copy: Expr 18 [195-196] [Type UDT<"Foo": Item 1>]: Var: Local 8
796 FieldsAssign 19 [198-208]: (Path([2])) Expr 20 [202-208] [Type String]: String:
797 Lit: "five"
798 FieldsAssign 21 [210-217]: (Path([1])) Expr 22 [214-217] [Type Double]: Lit: Double(6.7)
799 adj: <none>
800 ctl: <none>
801 ctl-adj: <none>"#]],
802 );
803}
804
805#[test]
806fn lower_fields_path() {
807 check_hir(
808 indoc! {r#"
809 namespace Foo {
810 struct A { b : B }
811 struct B { c : C }
812 struct C { i : Int }
813 operation Bar(a : A) : Unit {
814 let x = a.b.c.i;
815 }
816 }
817 "#},
818 &expect![[r#"
819 Package:
820 Item 0 [0-153] (Public):
821 Namespace (Ident 16 [10-13] "Foo"): Item 1, Item 2, Item 3, Item 4
822 Item 1 [20-38] (Internal):
823 Parent: 0
824 Type (Ident 0 [27-28] "A"): UDT [20-38]:
825 TyDef [20-38]: Tuple:
826 TyDef [31-36]: Field:
827 name: b [31-32]
828 type: UDT<"B": Item 2>
829 Item 2 [43-61] (Internal):
830 Parent: 0
831 Type (Ident 1 [50-51] "B"): UDT [43-61]:
832 TyDef [43-61]: Tuple:
833 TyDef [54-59]: Field:
834 name: c [54-55]
835 type: UDT<"C": Item 3>
836 Item 3 [66-86] (Internal):
837 Parent: 0
838 Type (Ident 2 [73-74] "C"): UDT [66-86]:
839 TyDef [66-86]: Tuple:
840 TyDef [77-84]: Field:
841 name: i [77-78]
842 type: Int
843 Item 4 [91-151] (Internal):
844 Parent: 0
845 Callable 3 [91-151] (operation):
846 name: Ident 4 [101-104] "Bar"
847 input: Pat 5 [105-110] [Type UDT<"A": Item 1>]: Bind: Ident 6 [105-106] "a"
848 output: Unit
849 functors: empty set
850 body: SpecDecl 7 [91-151]: Impl:
851 Block 8 [119-151] [Type Unit]:
852 Stmt 9 [129-145]: Local (Immutable):
853 Pat 10 [133-134] [Type Int]: Bind: Ident 11 [133-134] "x"
854 Expr 12 [137-144] [Type Int]: Field:
855 Expr 15 [137-142] [Type UDT<"C": Item 3>]: Field:
856 Expr 14 [137-140] [Type UDT<"B": Item 2>]: Field:
857 Expr 13 [137-138] [Type UDT<"A": Item 1>]: Var: Local 6
858 Path(FieldPath { indices: [0] })
859 Path(FieldPath { indices: [0] })
860 Path(FieldPath { indices: [0] })
861 adj: <none>
862 ctl: <none>
863 ctl-adj: <none>"#]],
864 );
865}
866
867#[test]
868fn lower_fields_path_with_expr() {
869 check_hir(
870 indoc! {r#"
871 namespace Foo {
872 struct A { b : B }
873 struct B { c : C }
874 struct C { i : Int }
875 operation Bar(a : A) : Unit {
876 let x = { a.b }.c.i;
877 }
878 }
879 "#},
880 &expect![[r#"
881 Package:
882 Item 0 [0-157] (Public):
883 Namespace (Ident 19 [10-13] "Foo"): Item 1, Item 2, Item 3, Item 4
884 Item 1 [20-38] (Internal):
885 Parent: 0
886 Type (Ident 0 [27-28] "A"): UDT [20-38]:
887 TyDef [20-38]: Tuple:
888 TyDef [31-36]: Field:
889 name: b [31-32]
890 type: UDT<"B": Item 2>
891 Item 2 [43-61] (Internal):
892 Parent: 0
893 Type (Ident 1 [50-51] "B"): UDT [43-61]:
894 TyDef [43-61]: Tuple:
895 TyDef [54-59]: Field:
896 name: c [54-55]
897 type: UDT<"C": Item 3>
898 Item 3 [66-86] (Internal):
899 Parent: 0
900 Type (Ident 2 [73-74] "C"): UDT [66-86]:
901 TyDef [66-86]: Tuple:
902 TyDef [77-84]: Field:
903 name: i [77-78]
904 type: Int
905 Item 4 [91-155] (Internal):
906 Parent: 0
907 Callable 3 [91-155] (operation):
908 name: Ident 4 [101-104] "Bar"
909 input: Pat 5 [105-110] [Type UDT<"A": Item 1>]: Bind: Ident 6 [105-106] "a"
910 output: Unit
911 functors: empty set
912 body: SpecDecl 7 [91-155]: Impl:
913 Block 8 [119-155] [Type Unit]:
914 Stmt 9 [129-149]: Local (Immutable):
915 Pat 10 [133-134] [Type Int]: Bind: Ident 11 [133-134] "x"
916 Expr 12 [137-148] [Type Int]: Field:
917 Expr 13 [137-146] [Type UDT<"C": Item 3>]: Field:
918 Expr 14 [137-144] [Type UDT<"B": Item 2>]: Expr Block: Block 15 [137-144] [Type UDT<"B": Item 2>]:
919 Stmt 16 [139-142]: Expr: Expr 17 [139-142] [Type UDT<"B": Item 2>]: Field:
920 Expr 18 [139-140] [Type UDT<"A": Item 1>]: Var: Local 6
921 Path(FieldPath { indices: [0] })
922 Path(FieldPath { indices: [0] })
923 Path(FieldPath { indices: [0] })
924 adj: <none>
925 ctl: <none>
926 ctl-adj: <none>"#]],
927 );
928}
929
930#[test]
931fn lambda_function_empty_closure() {
932 check_hir(
933 indoc! {"
934 namespace A {
935 function Foo() : Int {
936 let f = x -> x + 1;
937 f(1)
938 }
939 }
940 "},
941 &expect![[r#"
942 Package:
943 Item 0 [0-89] (Public):
944 Namespace (Ident 24 [10-11] "A"): Item 1
945 Item 1 [18-87] (Internal):
946 Parent: 0
947 Callable 0 [18-87] (function):
948 name: Ident 1 [27-30] "Foo"
949 input: Pat 2 [30-32] [Type Unit]: Unit
950 output: Int
951 functors: empty set
952 body: SpecDecl 3 [18-87]: Impl:
953 Block 4 [39-87] [Type Int]:
954 Stmt 5 [49-68]: Local (Immutable):
955 Pat 6 [53-54] [Type (Int -> Int)]: Bind: Ident 7 [53-54] "f"
956 Expr 8 [57-67] [Type (Int -> Int)]: Closure([], 2)
957 Stmt 20 [77-81]: Expr: Expr 21 [77-81] [Type Int]: Call:
958 Expr 22 [77-78] [Type (Int -> Int)]: Var: Local 7
959 Expr 23 [79-80] [Type Int]: Lit: Int(1)
960 adj: <none>
961 ctl: <none>
962 ctl-adj: <none>
963 Item 2 [57-67] (Internal):
964 Parent: 1
965 Callable 15 [57-67] (function):
966 name: Ident 16 [57-67] "<lambda>"
967 input: Pat 14 [57-67] [Type (Int,)]: Tuple:
968 Pat 9 [57-58] [Type Int]: Bind: Ident 10 [57-58] "x"
969 output: Int
970 functors: empty set
971 body: SpecDecl 17 [62-67]: Impl:
972 Block 18 [62-67] [Type Int]:
973 Stmt 19 [62-67]: Expr: Expr 11 [62-67] [Type Int]: BinOp (Add):
974 Expr 12 [62-63] [Type Int]: Var: Local 10
975 Expr 13 [66-67] [Type Int]: Lit: Int(1)
976 adj: <none>
977 ctl: <none>
978 ctl-adj: <none>"#]],
979 );
980}
981
982#[test]
983fn lambda_function_empty_closure_passed() {
984 check_hir(
985 indoc! {"
986 namespace A {
987 function Foo(f : Int -> Int) : Int { f(2) }
988 function Bar() : Int { Foo(x -> x + 1) }
989 }
990 "},
991 &expect![[r#"
992 Package:
993 Item 0 [0-108] (Public):
994 Namespace (Ident 30 [10-11] "A"): Item 1, Item 2
995 Item 1 [18-61] (Internal):
996 Parent: 0
997 Callable 0 [18-61] (function):
998 name: Ident 1 [27-30] "Foo"
999 input: Pat 2 [31-45] [Type (Int -> Int)]: Bind: Ident 3 [31-32] "f"
1000 output: Int
1001 functors: empty set
1002 body: SpecDecl 4 [18-61]: Impl:
1003 Block 5 [53-61] [Type Int]:
1004 Stmt 6 [55-59]: Expr: Expr 7 [55-59] [Type Int]: Call:
1005 Expr 8 [55-56] [Type (Int -> Int)]: Var: Local 3
1006 Expr 9 [57-58] [Type Int]: Lit: Int(2)
1007 adj: <none>
1008 ctl: <none>
1009 ctl-adj: <none>
1010 Item 2 [66-106] (Internal):
1011 Parent: 0
1012 Callable 10 [66-106] (function):
1013 name: Ident 11 [75-78] "Bar"
1014 input: Pat 12 [78-80] [Type Unit]: Unit
1015 output: Int
1016 functors: empty set
1017 body: SpecDecl 13 [66-106]: Impl:
1018 Block 14 [87-106] [Type Int]:
1019 Stmt 15 [89-104]: Expr: Expr 16 [89-104] [Type Int]: Call:
1020 Expr 17 [89-92] [Type ((Int -> Int) -> Int)]: Var: Item 1
1021 Expr 18 [93-103] [Type (Int -> Int)]: Closure([], 3)
1022 adj: <none>
1023 ctl: <none>
1024 ctl-adj: <none>
1025 Item 3 [93-103] (Internal):
1026 Parent: 2
1027 Callable 25 [93-103] (function):
1028 name: Ident 26 [93-103] "<lambda>"
1029 input: Pat 24 [93-103] [Type (Int,)]: Tuple:
1030 Pat 19 [93-94] [Type Int]: Bind: Ident 20 [93-94] "x"
1031 output: Int
1032 functors: empty set
1033 body: SpecDecl 27 [98-103]: Impl:
1034 Block 28 [98-103] [Type Int]:
1035 Stmt 29 [98-103]: Expr: Expr 21 [98-103] [Type Int]: BinOp (Add):
1036 Expr 22 [98-99] [Type Int]: Var: Local 20
1037 Expr 23 [102-103] [Type Int]: Lit: Int(1)
1038 adj: <none>
1039 ctl: <none>
1040 ctl-adj: <none>"#]],
1041 );
1042}
1043
1044#[test]
1045fn lambda_function_closure() {
1046 check_hir(
1047 indoc! {"
1048 namespace A {
1049 function Foo() : Int {
1050 let x = 5;
1051 let f = y -> x + y;
1052 f(2)
1053 }
1054 }
1055 "},
1056 &expect![[r#"
1057 Package:
1058 Item 0 [0-108] (Public):
1059 Namespace (Ident 30 [10-11] "A"): Item 1
1060 Item 1 [18-106] (Internal):
1061 Parent: 0
1062 Callable 0 [18-106] (function):
1063 name: Ident 1 [27-30] "Foo"
1064 input: Pat 2 [30-32] [Type Unit]: Unit
1065 output: Int
1066 functors: empty set
1067 body: SpecDecl 3 [18-106]: Impl:
1068 Block 4 [39-106] [Type Int]:
1069 Stmt 5 [49-59]: Local (Immutable):
1070 Pat 6 [53-54] [Type Int]: Bind: Ident 7 [53-54] "x"
1071 Expr 8 [57-58] [Type Int]: Lit: Int(5)
1072 Stmt 9 [68-87]: Local (Immutable):
1073 Pat 10 [72-73] [Type (Int -> Int)]: Bind: Ident 11 [72-73] "f"
1074 Expr 12 [76-86] [Type (Int -> Int)]: Closure([7], 2)
1075 Stmt 26 [96-100]: Expr: Expr 27 [96-100] [Type Int]: Call:
1076 Expr 28 [96-97] [Type (Int -> Int)]: Var: Local 11
1077 Expr 29 [98-99] [Type Int]: Lit: Int(2)
1078 adj: <none>
1079 ctl: <none>
1080 ctl-adj: <none>
1081 Item 2 [76-86] (Internal):
1082 Parent: 1
1083 Callable 21 [76-86] (function):
1084 name: Ident 22 [76-86] "<lambda>"
1085 input: Pat 19 [76-86] [Type (Int, Int)]: Tuple:
1086 Pat 20 [53-54] [Type Int]: Bind: Ident 18 [53-54] "x"
1087 Pat 13 [76-77] [Type Int]: Bind: Ident 14 [76-77] "y"
1088 output: Int
1089 functors: empty set
1090 body: SpecDecl 23 [81-86]: Impl:
1091 Block 24 [81-86] [Type Int]:
1092 Stmt 25 [81-86]: Expr: Expr 15 [81-86] [Type Int]: BinOp (Add):
1093 Expr 16 [81-82] [Type Int]: Var: Local 18
1094 Expr 17 [85-86] [Type Int]: Var: Local 14
1095 adj: <none>
1096 ctl: <none>
1097 ctl-adj: <none>"#]],
1098 );
1099}
1100
1101#[test]
1102fn lambda_function_closure_repeated_var() {
1103 check_hir(
1104 indoc! {"
1105 namespace A {
1106 function Foo() : Int {
1107 let x = 5;
1108 let f = y -> x + x + y;
1109 f(2)
1110 }
1111 }
1112 "},
1113 &expect![[r#"
1114 Package:
1115 Item 0 [0-112] (Public):
1116 Namespace (Ident 32 [10-11] "A"): Item 1
1117 Item 1 [18-110] (Internal):
1118 Parent: 0
1119 Callable 0 [18-110] (function):
1120 name: Ident 1 [27-30] "Foo"
1121 input: Pat 2 [30-32] [Type Unit]: Unit
1122 output: Int
1123 functors: empty set
1124 body: SpecDecl 3 [18-110]: Impl:
1125 Block 4 [39-110] [Type Int]:
1126 Stmt 5 [49-59]: Local (Immutable):
1127 Pat 6 [53-54] [Type Int]: Bind: Ident 7 [53-54] "x"
1128 Expr 8 [57-58] [Type Int]: Lit: Int(5)
1129 Stmt 9 [68-91]: Local (Immutable):
1130 Pat 10 [72-73] [Type (Int -> Int)]: Bind: Ident 11 [72-73] "f"
1131 Expr 12 [76-90] [Type (Int -> Int)]: Closure([7], 2)
1132 Stmt 28 [100-104]: Expr: Expr 29 [100-104] [Type Int]: Call:
1133 Expr 30 [100-101] [Type (Int -> Int)]: Var: Local 11
1134 Expr 31 [102-103] [Type Int]: Lit: Int(2)
1135 adj: <none>
1136 ctl: <none>
1137 ctl-adj: <none>
1138 Item 2 [76-90] (Internal):
1139 Parent: 1
1140 Callable 23 [76-90] (function):
1141 name: Ident 24 [76-90] "<lambda>"
1142 input: Pat 21 [76-90] [Type (Int, Int)]: Tuple:
1143 Pat 22 [53-54] [Type Int]: Bind: Ident 20 [53-54] "x"
1144 Pat 13 [76-77] [Type Int]: Bind: Ident 14 [76-77] "y"
1145 output: Int
1146 functors: empty set
1147 body: SpecDecl 25 [81-90]: Impl:
1148 Block 26 [81-90] [Type Int]:
1149 Stmt 27 [81-90]: Expr: Expr 15 [81-90] [Type Int]: BinOp (Add):
1150 Expr 16 [81-86] [Type Int]: BinOp (Add):
1151 Expr 17 [81-82] [Type Int]: Var: Local 20
1152 Expr 18 [85-86] [Type Int]: Var: Local 20
1153 Expr 19 [89-90] [Type Int]: Var: Local 14
1154 adj: <none>
1155 ctl: <none>
1156 ctl-adj: <none>"#]],
1157 );
1158}
1159
1160#[test]
1161fn lambda_function_closure_passed() {
1162 check_hir(
1163 indoc! {"
1164 namespace A {
1165 function Foo(f : Int -> Int) : Int { f(2) }
1166 function Bar() : Int {
1167 let x = 5;
1168 Foo(y -> x + y)
1169 }
1170 }
1171 "},
1172 &expect![[r#"
1173 Package:
1174 Item 0 [0-139] (Public):
1175 Namespace (Ident 36 [10-11] "A"): Item 1, Item 2
1176 Item 1 [18-61] (Internal):
1177 Parent: 0
1178 Callable 0 [18-61] (function):
1179 name: Ident 1 [27-30] "Foo"
1180 input: Pat 2 [31-45] [Type (Int -> Int)]: Bind: Ident 3 [31-32] "f"
1181 output: Int
1182 functors: empty set
1183 body: SpecDecl 4 [18-61]: Impl:
1184 Block 5 [53-61] [Type Int]:
1185 Stmt 6 [55-59]: Expr: Expr 7 [55-59] [Type Int]: Call:
1186 Expr 8 [55-56] [Type (Int -> Int)]: Var: Local 3
1187 Expr 9 [57-58] [Type Int]: Lit: Int(2)
1188 adj: <none>
1189 ctl: <none>
1190 ctl-adj: <none>
1191 Item 2 [66-137] (Internal):
1192 Parent: 0
1193 Callable 10 [66-137] (function):
1194 name: Ident 11 [75-78] "Bar"
1195 input: Pat 12 [78-80] [Type Unit]: Unit
1196 output: Int
1197 functors: empty set
1198 body: SpecDecl 13 [66-137]: Impl:
1199 Block 14 [87-137] [Type Int]:
1200 Stmt 15 [97-107]: Local (Immutable):
1201 Pat 16 [101-102] [Type Int]: Bind: Ident 17 [101-102] "x"
1202 Expr 18 [105-106] [Type Int]: Lit: Int(5)
1203 Stmt 19 [116-131]: Expr: Expr 20 [116-131] [Type Int]: Call:
1204 Expr 21 [116-119] [Type ((Int -> Int) -> Int)]: Var: Item 1
1205 Expr 22 [120-130] [Type (Int -> Int)]: Closure([17], 3)
1206 adj: <none>
1207 ctl: <none>
1208 ctl-adj: <none>
1209 Item 3 [120-130] (Internal):
1210 Parent: 2
1211 Callable 31 [120-130] (function):
1212 name: Ident 32 [120-130] "<lambda>"
1213 input: Pat 29 [120-130] [Type (Int, Int)]: Tuple:
1214 Pat 30 [101-102] [Type Int]: Bind: Ident 28 [101-102] "x"
1215 Pat 23 [120-121] [Type Int]: Bind: Ident 24 [120-121] "y"
1216 output: Int
1217 functors: empty set
1218 body: SpecDecl 33 [125-130]: Impl:
1219 Block 34 [125-130] [Type Int]:
1220 Stmt 35 [125-130]: Expr: Expr 25 [125-130] [Type Int]: BinOp (Add):
1221 Expr 26 [125-126] [Type Int]: Var: Local 28
1222 Expr 27 [129-130] [Type Int]: Var: Local 24
1223 adj: <none>
1224 ctl: <none>
1225 ctl-adj: <none>"#]],
1226 );
1227}
1228
1229#[test]
1230fn lambda_function_nested_closure() {
1231 check_hir(
1232 indoc! {"
1233 namespace A {
1234 function Foo(f : Int -> Int -> Int) : Int { f(2)(3) }
1235 function Bar() : Int {
1236 let a = 5;
1237 Foo(b -> {
1238 let c = 1;
1239 d -> a + b + c + d
1240 })
1241 }
1242 }
1243 "},
1244 &expect![[r#"
1245 Package:
1246 Item 0 [0-209] (Public):
1247 Namespace (Ident 64 [10-11] "A"): Item 1, Item 2
1248 Item 1 [18-71] (Internal):
1249 Parent: 0
1250 Callable 0 [18-71] (function):
1251 name: Ident 1 [27-30] "Foo"
1252 input: Pat 2 [31-52] [Type (Int -> (Int -> Int))]: Bind: Ident 3 [31-32] "f"
1253 output: Int
1254 functors: empty set
1255 body: SpecDecl 4 [18-71]: Impl:
1256 Block 5 [60-71] [Type Int]:
1257 Stmt 6 [62-69]: Expr: Expr 7 [62-69] [Type Int]: Call:
1258 Expr 8 [62-66] [Type (Int -> Int)]: Call:
1259 Expr 9 [62-63] [Type (Int -> (Int -> Int))]: Var: Local 3
1260 Expr 10 [64-65] [Type Int]: Lit: Int(2)
1261 Expr 11 [67-68] [Type Int]: Lit: Int(3)
1262 adj: <none>
1263 ctl: <none>
1264 ctl-adj: <none>
1265 Item 2 [76-207] (Internal):
1266 Parent: 0
1267 Callable 12 [76-207] (function):
1268 name: Ident 13 [85-88] "Bar"
1269 input: Pat 14 [88-90] [Type Unit]: Unit
1270 output: Int
1271 functors: empty set
1272 body: SpecDecl 15 [76-207]: Impl:
1273 Block 16 [97-207] [Type Int]:
1274 Stmt 17 [107-117]: Local (Immutable):
1275 Pat 18 [111-112] [Type Int]: Bind: Ident 19 [111-112] "a"
1276 Expr 20 [115-116] [Type Int]: Lit: Int(5)
1277 Stmt 21 [126-201]: Expr: Expr 22 [126-201] [Type Int]: Call:
1278 Expr 23 [126-129] [Type ((Int -> (Int -> Int)) -> Int)]: Var: Item 1
1279 Expr 24 [130-200] [Type (Int -> (Int -> Int))]: Closure([19], 4)
1280 adj: <none>
1281 ctl: <none>
1282 ctl-adj: <none>
1283 Item 3 [172-190] (Internal):
1284 Parent: 2
1285 Callable 51 [172-190] (function):
1286 name: Ident 52 [172-190] "<lambda>"
1287 input: Pat 47 [172-190] [Type (Int, Int, Int, Int)]: Tuple:
1288 Pat 48 [111-112] [Type Int]: Bind: Ident 44 [111-112] "a"
1289 Pat 49 [130-131] [Type Int]: Bind: Ident 45 [130-131] "b"
1290 Pat 50 [153-154] [Type Int]: Bind: Ident 46 [153-154] "c"
1291 Pat 35 [172-173] [Type Int]: Bind: Ident 36 [172-173] "d"
1292 output: Int
1293 functors: empty set
1294 body: SpecDecl 53 [177-190]: Impl:
1295 Block 54 [177-190] [Type Int]:
1296 Stmt 55 [177-190]: Expr: Expr 37 [177-190] [Type Int]: BinOp (Add):
1297 Expr 38 [177-186] [Type Int]: BinOp (Add):
1298 Expr 39 [177-182] [Type Int]: BinOp (Add):
1299 Expr 40 [177-178] [Type Int]: Var: Local 44
1300 Expr 41 [181-182] [Type Int]: Var: Local 45
1301 Expr 42 [185-186] [Type Int]: Var: Local 46
1302 Expr 43 [189-190] [Type Int]: Var: Local 36
1303 adj: <none>
1304 ctl: <none>
1305 ctl-adj: <none>
1306 Item 4 [130-200] (Internal):
1307 Parent: 2
1308 Callable 59 [130-200] (function):
1309 name: Ident 60 [130-200] "<lambda>"
1310 input: Pat 57 [130-200] [Type (Int, Int)]: Tuple:
1311 Pat 58 [111-112] [Type Int]: Bind: Ident 56 [111-112] "a"
1312 Pat 25 [130-131] [Type Int]: Bind: Ident 26 [130-131] "b"
1313 output: (Int -> Int)
1314 functors: empty set
1315 body: SpecDecl 61 [135-200]: Impl:
1316 Block 62 [135-200] [Type (Int -> Int)]:
1317 Stmt 63 [135-200]: Expr: Expr 27 [135-200] [Type (Int -> Int)]: Expr Block: Block 28 [135-200] [Type (Int -> Int)]:
1318 Stmt 29 [149-159]: Local (Immutable):
1319 Pat 30 [153-154] [Type Int]: Bind: Ident 31 [153-154] "c"
1320 Expr 32 [157-158] [Type Int]: Lit: Int(1)
1321 Stmt 33 [172-190]: Expr: Expr 34 [172-190] [Type (Int -> Int)]: Closure([56, 26, 31], 3)
1322 adj: <none>
1323 ctl: <none>
1324 ctl-adj: <none>"#]],
1325 );
1326}
1327
1328#[test]
1329fn lambda_operation_empty_closure() {
1330 check_hir(
1331 indoc! {"
1332 namespace A {
1333 operation Foo(op : Qubit => ()) : () {
1334 use q = Qubit();
1335 op(q)
1336 }
1337 operation Bar() : Result { Foo(q => ()) }
1338 }
1339 "},
1340 &expect![[r#"
1341 Package:
1342 Item 0 [0-149] (Public):
1343 Namespace (Ident 32 [10-11] "A"): Item 1, Item 2
1344 Item 1 [18-101] (Internal):
1345 Parent: 0
1346 Callable 0 [18-101] (operation):
1347 name: Ident 1 [28-31] "Foo"
1348 generics:
1349 0: functor (empty set)
1350 input: Pat 2 [32-48] [Type (Qubit => Unit is Param<0>)]: Bind: Ident 3 [32-34] "op"
1351 output: Unit
1352 functors: empty set
1353 body: SpecDecl 4 [18-101]: Impl:
1354 Block 5 [55-101] [Type Unit]:
1355 Stmt 6 [65-81]: Qubit (Fresh)
1356 Pat 7 [69-70] [Type Qubit]: Bind: Ident 8 [69-70] "q"
1357 QubitInit 9 [73-80] [Type Qubit]: Single
1358 Stmt 10 [90-95]: Expr: Expr 11 [90-95] [Type Unit]: Call:
1359 Expr 12 [90-92] [Type (Qubit => Unit)]: Var: Local 3
1360 Expr 13 [93-94] [Type Qubit]: Var: Local 8
1361 adj: <none>
1362 ctl: <none>
1363 ctl-adj: <none>
1364 Item 2 [106-147] (Internal):
1365 Parent: 0
1366 Callable 14 [106-147] (operation):
1367 name: Ident 15 [116-119] "Bar"
1368 input: Pat 16 [119-121] [Type Unit]: Unit
1369 output: Result
1370 functors: empty set
1371 body: SpecDecl 17 [106-147]: Impl:
1372 Block 18 [131-147] [Type Unit]:
1373 Stmt 19 [133-145]: Expr: Expr 20 [133-145] [Type Unit]: Call:
1374 Expr 21 [133-136] [Type ((Qubit => Unit) => Unit)]: Var:
1375 res: Item 1
1376 generics:
1377 empty set
1378 Expr 22 [137-144] [Type (Qubit => Unit)]: Closure([], 3)
1379 adj: <none>
1380 ctl: <none>
1381 ctl-adj: <none>
1382 Item 3 [137-144] (Internal):
1383 Parent: 2
1384 Callable 27 [137-144] (operation):
1385 name: Ident 28 [137-144] "<lambda>"
1386 input: Pat 26 [137-144] [Type (Qubit,)]: Tuple:
1387 Pat 23 [137-138] [Type Qubit]: Bind: Ident 24 [137-138] "q"
1388 output: Unit
1389 functors: empty set
1390 body: SpecDecl 29 [142-144]: Impl:
1391 Block 30 [142-144] [Type Unit]:
1392 Stmt 31 [142-144]: Expr: Expr 25 [142-144] [Type Unit]: Unit
1393 adj: <none>
1394 ctl: <none>
1395 ctl-adj: <none>"#]],
1396 );
1397}
1398
1399#[test]
1400fn lambda_operation_closure() {
1401 check_hir(
1402 indoc! {"
1403 namespace A {
1404 operation MResetZ(q : Qubit) : Result { body intrinsic; }
1405 operation Foo(op : () => Result) : Result { op() }
1406 operation Bar() : Result {
1407 use q = Qubit();
1408 Foo(() => MResetZ(q))
1409 }
1410 }
1411 "},
1412 &expect![[r#"
1413 Package:
1414 Item 0 [0-224] (Public):
1415 Namespace (Ident 40 [10-11] "A"): Item 1, Item 2, Item 3
1416 Item 1 [18-75] (Internal):
1417 Parent: 0
1418 Callable 0 [18-75] (operation):
1419 name: Ident 1 [28-35] "MResetZ"
1420 input: Pat 2 [36-45] [Type Qubit]: Bind: Ident 3 [36-37] "q"
1421 output: Result
1422 functors: empty set
1423 body: SpecDecl 4 [58-73]: Gen: Intrinsic
1424 adj: <none>
1425 ctl: <none>
1426 ctl-adj: <none>
1427 Item 2 [80-130] (Internal):
1428 Parent: 0
1429 Callable 5 [80-130] (operation):
1430 name: Ident 6 [90-93] "Foo"
1431 generics:
1432 0: functor (empty set)
1433 input: Pat 7 [94-111] [Type (Unit => Result is Param<0>)]: Bind: Ident 8 [94-96] "op"
1434 output: Result
1435 functors: empty set
1436 body: SpecDecl 9 [80-130]: Impl:
1437 Block 10 [122-130] [Type Result]:
1438 Stmt 11 [124-128]: Expr: Expr 12 [124-128] [Type Result]: Call:
1439 Expr 13 [124-126] [Type (Unit => Result)]: Var: Local 8
1440 Expr 14 [126-128] [Type Unit]: Unit
1441 adj: <none>
1442 ctl: <none>
1443 ctl-adj: <none>
1444 Item 3 [135-222] (Internal):
1445 Parent: 0
1446 Callable 15 [135-222] (operation):
1447 name: Ident 16 [145-148] "Bar"
1448 input: Pat 17 [148-150] [Type Unit]: Unit
1449 output: Result
1450 functors: empty set
1451 body: SpecDecl 18 [135-222]: Impl:
1452 Block 19 [160-222] [Type Result]:
1453 Stmt 20 [170-186]: Qubit (Fresh)
1454 Pat 21 [174-175] [Type Qubit]: Bind: Ident 22 [174-175] "q"
1455 QubitInit 23 [178-185] [Type Qubit]: Single
1456 Stmt 24 [195-216]: Expr: Expr 25 [195-216] [Type Result]: Call:
1457 Expr 26 [195-198] [Type ((Unit => Result) => Result)]: Var:
1458 res: Item 2
1459 generics:
1460 empty set
1461 Expr 27 [199-215] [Type (Unit => Result)]: Closure([22], 4)
1462 adj: <none>
1463 ctl: <none>
1464 ctl-adj: <none>
1465 Item 4 [199-215] (Internal):
1466 Parent: 3
1467 Callable 35 [199-215] (operation):
1468 name: Ident 36 [199-215] "<lambda>"
1469 input: Pat 33 [199-215] [Type (Qubit, Unit)]: Tuple:
1470 Pat 34 [174-175] [Type Qubit]: Bind: Ident 32 [174-175] "q"
1471 Pat 28 [199-201] [Type Unit]: Unit
1472 output: Result
1473 functors: empty set
1474 body: SpecDecl 37 [205-215]: Impl:
1475 Block 38 [205-215] [Type Result]:
1476 Stmt 39 [205-215]: Expr: Expr 29 [205-215] [Type Result]: Call:
1477 Expr 30 [205-212] [Type (Qubit => Result)]: Var: Item 1
1478 Expr 31 [213-214] [Type Qubit]: Var: Local 32
1479 adj: <none>
1480 ctl: <none>
1481 ctl-adj: <none>"#]],
1482 );
1483}
1484
1485#[test]
1486fn lambda_adj() {
1487 check_hir(
1488 indoc! {r#"
1489 namespace A {
1490 operation X(q : Qubit) : () is Adj {}
1491 operation Foo(op : Qubit => () is Adj) : () {}
1492 operation Bar() : () { Foo(q => X(q)); }
1493 }
1494 "#},
1495 &expect![[r#"
1496 Package:
1497 Item 0 [0-153] (Public):
1498 Namespace (Ident 32 [10-11] "A"): Item 1, Item 2, Item 3
1499 Item 1 [18-55] (Internal):
1500 Parent: 0
1501 Callable 0 [18-55] (operation):
1502 name: Ident 1 [28-29] "X"
1503 input: Pat 2 [30-39] [Type Qubit]: Bind: Ident 3 [30-31] "q"
1504 output: Unit
1505 functors: Adj
1506 body: SpecDecl 4 [18-55]: Impl:
1507 Block 5 [53-55]: <empty>
1508 adj: <none>
1509 ctl: <none>
1510 ctl-adj: <none>
1511 Item 2 [60-106] (Internal):
1512 Parent: 0
1513 Callable 6 [60-106] (operation):
1514 name: Ident 7 [70-73] "Foo"
1515 generics:
1516 0: functor (Adj)
1517 input: Pat 8 [74-97] [Type (Qubit => Unit is Param<0>)]: Bind: Ident 9 [74-76] "op"
1518 output: Unit
1519 functors: empty set
1520 body: SpecDecl 10 [60-106]: Impl:
1521 Block 11 [104-106]: <empty>
1522 adj: <none>
1523 ctl: <none>
1524 ctl-adj: <none>
1525 Item 3 [111-151] (Internal):
1526 Parent: 0
1527 Callable 12 [111-151] (operation):
1528 name: Ident 13 [121-124] "Bar"
1529 input: Pat 14 [124-126] [Type Unit]: Unit
1530 output: Unit
1531 functors: empty set
1532 body: SpecDecl 15 [111-151]: Impl:
1533 Block 16 [132-151] [Type Unit]:
1534 Stmt 17 [134-149]: Semi: Expr 18 [134-148] [Type Unit]: Call:
1535 Expr 19 [134-137] [Type ((Qubit => Unit is Adj) => Unit)]: Var:
1536 res: Item 2
1537 generics:
1538 Adj
1539 Expr 20 [138-147] [Type (Qubit => Unit is Adj)]: Closure([], 4)
1540 adj: <none>
1541 ctl: <none>
1542 ctl-adj: <none>
1543 Item 4 [138-147] (Internal):
1544 Parent: 3
1545 Callable 27 [138-147] (operation):
1546 name: Ident 28 [138-147] "<lambda>"
1547 input: Pat 26 [138-147] [Type (Qubit,)]: Tuple:
1548 Pat 21 [138-139] [Type Qubit]: Bind: Ident 22 [138-139] "q"
1549 output: Unit
1550 functors: Adj
1551 body: SpecDecl 29 [143-147]: Impl:
1552 Block 30 [143-147] [Type Unit]:
1553 Stmt 31 [143-147]: Expr: Expr 23 [143-147] [Type Unit]: Call:
1554 Expr 24 [143-144] [Type (Qubit => Unit is Adj)]: Var: Item 1
1555 Expr 25 [145-146] [Type Qubit]: Var: Local 22
1556 adj: <none>
1557 ctl: <none>
1558 ctl-adj: <none>"#]],
1559 );
1560}
1561
1562#[test]
1563fn partial_app_one_hole() {
1564 check_hir(
1565 indoc! {"
1566 namespace A {
1567 function Foo(x : Int, y : Int) : Int { x + y }
1568 function Bar() : () { let f = Foo(_, 2); }
1569 }
1570 "},
1571 &expect![[r#"
1572 Package:
1573 Item 0 [0-113] (Public):
1574 Namespace (Ident 45 [10-11] "A"): Item 1, Item 2
1575 Item 1 [18-64] (Internal):
1576 Parent: 0
1577 Callable 0 [18-64] (function):
1578 name: Ident 1 [27-30] "Foo"
1579 input: Pat 2 [30-48] [Type (Int, Int)]: Tuple:
1580 Pat 3 [31-38] [Type Int]: Bind: Ident 4 [31-32] "x"
1581 Pat 5 [40-47] [Type Int]: Bind: Ident 6 [40-41] "y"
1582 output: Int
1583 functors: empty set
1584 body: SpecDecl 7 [18-64]: Impl:
1585 Block 8 [55-64] [Type Int]:
1586 Stmt 9 [57-62]: Expr: Expr 10 [57-62] [Type Int]: BinOp (Add):
1587 Expr 11 [57-58] [Type Int]: Var: Local 4
1588 Expr 12 [61-62] [Type Int]: Var: Local 6
1589 adj: <none>
1590 ctl: <none>
1591 ctl-adj: <none>
1592 Item 2 [69-111] (Internal):
1593 Parent: 0
1594 Callable 13 [69-111] (function):
1595 name: Ident 14 [78-81] "Bar"
1596 input: Pat 15 [81-83] [Type Unit]: Unit
1597 output: Unit
1598 functors: empty set
1599 body: SpecDecl 16 [69-111]: Impl:
1600 Block 17 [89-111] [Type Unit]:
1601 Stmt 18 [91-109]: Local (Immutable):
1602 Pat 19 [95-96] [Type (Int -> Int)]: Bind: Ident 20 [95-96] "f"
1603 Expr 21 [99-108] [Type (Int -> Int)]: Expr Block: Block 42 [99-108] [Type (Int -> Int)]:
1604 Stmt 30 [106-107]: Local (Immutable):
1605 Pat 29 [106-107] [Type Int]: Bind: Ident 27 [106-107] "arg"
1606 Expr 26 [106-107] [Type Int]: Lit: Int(2)
1607 Stmt 43 [99-108]: Expr: Expr 44 [99-108] [Type (Int -> Int)]: Closure([27], 3)
1608 adj: <none>
1609 ctl: <none>
1610 ctl-adj: <none>
1611 Item 3 [99-108] (Internal):
1612 Parent: 2
1613 Callable 37 [99-108] (function):
1614 name: Ident 38 [99-108] "<lambda>"
1615 input: Pat 35 [99-108] [Type (Int, Int)]: Tuple:
1616 Pat 36 [106-107] [Type Int]: Bind: Ident 34 [106-107] "arg"
1617 Pat 24 [103-104] [Type Int]: Bind: Ident 23 [103-104] "hole"
1618 output: Int
1619 functors: empty set
1620 body: SpecDecl 39 [99-108]: Impl:
1621 Block 40 [99-108] [Type Int]:
1622 Stmt 41 [99-108]: Expr: Expr 33 [99-108] [Type Int]: Call:
1623 Expr 22 [99-102] [Type ((Int, Int) -> Int)]: Var: Item 1
1624 Expr 32 [102-108] [Type (Int, Int)]: Tuple:
1625 Expr 25 [103-104] [Type Int]: Var: Local 23
1626 Expr 28 [106-107] [Type Int]: Var: Local 34
1627 adj: <none>
1628 ctl: <none>
1629 ctl-adj: <none>"#]],
1630 );
1631}
1632
1633#[test]
1634fn partial_app_two_holes() {
1635 check_hir(
1636 indoc! {"
1637 namespace A {
1638 function Foo(x : Int, y : Int) : Int { x + y }
1639 function Bar() : () { let f = Foo(_, _); }
1640 }
1641 "},
1642 &expect![[r#"
1643 Package:
1644 Item 0 [0-113] (Public):
1645 Namespace (Ident 41 [10-11] "A"): Item 1, Item 2
1646 Item 1 [18-64] (Internal):
1647 Parent: 0
1648 Callable 0 [18-64] (function):
1649 name: Ident 1 [27-30] "Foo"
1650 input: Pat 2 [30-48] [Type (Int, Int)]: Tuple:
1651 Pat 3 [31-38] [Type Int]: Bind: Ident 4 [31-32] "x"
1652 Pat 5 [40-47] [Type Int]: Bind: Ident 6 [40-41] "y"
1653 output: Int
1654 functors: empty set
1655 body: SpecDecl 7 [18-64]: Impl:
1656 Block 8 [55-64] [Type Int]:
1657 Stmt 9 [57-62]: Expr: Expr 10 [57-62] [Type Int]: BinOp (Add):
1658 Expr 11 [57-58] [Type Int]: Var: Local 4
1659 Expr 12 [61-62] [Type Int]: Var: Local 6
1660 adj: <none>
1661 ctl: <none>
1662 ctl-adj: <none>
1663 Item 2 [69-111] (Internal):
1664 Parent: 0
1665 Callable 13 [69-111] (function):
1666 name: Ident 14 [78-81] "Bar"
1667 input: Pat 15 [81-83] [Type Unit]: Unit
1668 output: Unit
1669 functors: empty set
1670 body: SpecDecl 16 [69-111]: Impl:
1671 Block 17 [89-111] [Type Unit]:
1672 Stmt 18 [91-109]: Local (Immutable):
1673 Pat 19 [95-96] [Type ((Int, Int) -> Int)]: Bind: Ident 20 [95-96] "f"
1674 Expr 21 [99-108] [Type ((Int, Int) -> Int)]: Expr Block: Block 38 [99-108] [Type ((Int, Int) -> Int)]:
1675 Stmt 39 [99-108]: Expr: Expr 40 [99-108] [Type ((Int, Int) -> Int)]: Closure([], 3)
1676 adj: <none>
1677 ctl: <none>
1678 ctl-adj: <none>
1679 Item 3 [99-108] (Internal):
1680 Parent: 2
1681 Callable 33 [99-108] (function):
1682 name: Ident 34 [99-108] "<lambda>"
1683 input: Pat 32 [99-108] [Type ((Int, Int),)]: Tuple:
1684 Pat 30 [102-108] [Type (Int, Int)]: Tuple:
1685 Pat 24 [103-104] [Type Int]: Bind: Ident 23 [103-104] "hole"
1686 Pat 27 [106-107] [Type Int]: Bind: Ident 26 [106-107] "hole"
1687 output: Int
1688 functors: empty set
1689 body: SpecDecl 35 [99-108]: Impl:
1690 Block 36 [99-108] [Type Int]:
1691 Stmt 37 [99-108]: Expr: Expr 31 [99-108] [Type Int]: Call:
1692 Expr 22 [99-102] [Type ((Int, Int) -> Int)]: Var: Item 1
1693 Expr 29 [102-108] [Type (Int, Int)]: Tuple:
1694 Expr 25 [103-104] [Type Int]: Var: Local 23
1695 Expr 28 [106-107] [Type Int]: Var: Local 26
1696 adj: <none>
1697 ctl: <none>
1698 ctl-adj: <none>"#]],
1699 );
1700}
1701
1702#[test]
1703fn partial_app_nested_tuple() {
1704 check_hir(
1705 indoc! {"
1706 namespace A {
1707 function Foo(a : Int, (b : Bool, c : Double, d : String), e : Result) : () {}
1708 function Bar() : () { let f = Foo(_, (_, 1.0, _), _); }
1709 }
1710 "},
1711 &expect![[r#"
1712 Package:
1713 Item 0 [0-157] (Public):
1714 Namespace (Ident 60 [10-11] "A"): Item 1, Item 2
1715 Item 1 [18-95] (Internal):
1716 Parent: 0
1717 Callable 0 [18-95] (function):
1718 name: Ident 1 [27-30] "Foo"
1719 input: Pat 2 [30-87] [Type (Int, (Bool, Double, String), Result)]: Tuple:
1720 Pat 3 [31-38] [Type Int]: Bind: Ident 4 [31-32] "a"
1721 Pat 5 [40-74] [Type (Bool, Double, String)]: Tuple:
1722 Pat 6 [41-49] [Type Bool]: Bind: Ident 7 [41-42] "b"
1723 Pat 8 [51-61] [Type Double]: Bind: Ident 9 [51-52] "c"
1724 Pat 10 [63-73] [Type String]: Bind: Ident 11 [63-64] "d"
1725 Pat 12 [76-86] [Type Result]: Bind: Ident 13 [76-77] "e"
1726 output: Unit
1727 functors: empty set
1728 body: SpecDecl 14 [18-95]: Impl:
1729 Block 15 [93-95]: <empty>
1730 adj: <none>
1731 ctl: <none>
1732 ctl-adj: <none>
1733 Item 2 [100-155] (Internal):
1734 Parent: 0
1735 Callable 16 [100-155] (function):
1736 name: Ident 17 [109-112] "Bar"
1737 input: Pat 18 [112-114] [Type Unit]: Unit
1738 output: Unit
1739 functors: empty set
1740 body: SpecDecl 19 [100-155]: Impl:
1741 Block 20 [120-155] [Type Unit]:
1742 Stmt 21 [122-153]: Local (Immutable):
1743 Pat 22 [126-127] [Type ((Int, (Bool, String), Result) -> Unit)]: Bind: Ident 23 [126-127] "f"
1744 Expr 24 [130-152] [Type ((Int, (Bool, String), Result) -> Unit)]: Expr Block: Block 57 [130-152] [Type ((Int, (Bool, String), Result) -> Unit)]:
1745 Stmt 36 [141-144]: Local (Immutable):
1746 Pat 35 [141-144] [Type Double]: Bind: Ident 33 [141-144] "arg"
1747 Expr 32 [141-144] [Type Double]: Lit: Double(1)
1748 Stmt 58 [130-152]: Expr: Expr 59 [130-152] [Type ((Int, (Bool, String), Result) -> Unit)]: Closure([33], 3)
1749 adj: <none>
1750 ctl: <none>
1751 ctl-adj: <none>
1752 Item 3 [130-152] (Internal):
1753 Parent: 2
1754 Callable 52 [130-152] (function):
1755 name: Ident 53 [130-152] "<lambda>"
1756 input: Pat 50 [130-152] [Type (Double, (Int, (Bool, String), Result))]: Tuple:
1757 Pat 51 [141-144] [Type Double]: Bind: Ident 49 [141-144] "arg"
1758 Pat 47 [133-152] [Type (Int, (Bool, String), Result)]: Tuple:
1759 Pat 27 [134-135] [Type Int]: Bind: Ident 26 [134-135] "hole"
1760 Pat 42 [137-148] [Type (Bool, String)]: Tuple:
1761 Pat 30 [138-139] [Type Bool]: Bind: Ident 29 [138-139] "hole"
1762 Pat 39 [146-147] [Type String]: Bind: Ident 38 [146-147] "hole"
1763 Pat 44 [150-151] [Type Result]: Bind: Ident 43 [150-151] "hole"
1764 output: Unit
1765 functors: empty set
1766 body: SpecDecl 54 [130-152]: Impl:
1767 Block 55 [130-152] [Type Unit]:
1768 Stmt 56 [130-152]: Expr: Expr 48 [130-152] [Type Unit]: Call:
1769 Expr 25 [130-133] [Type ((Int, (Bool, Double, String), Result) -> Unit)]: Var: Item 1
1770 Expr 46 [133-152] [Type (Int, (Bool, Double, String), Result)]: Tuple:
1771 Expr 28 [134-135] [Type Int]: Var: Local 26
1772 Expr 41 [137-148] [Type (Bool, Double, String)]: Tuple:
1773 Expr 31 [138-139] [Type Bool]: Var: Local 29
1774 Expr 34 [141-144] [Type Double]: Var: Local 49
1775 Expr 40 [146-147] [Type String]: Var: Local 38
1776 Expr 45 [150-151] [Type Result]: Var: Local 43
1777 adj: <none>
1778 ctl: <none>
1779 ctl-adj: <none>"#]],
1780 );
1781}
1782
1783#[test]
1784fn partial_app_nested_tuple_singleton_unwrap() {
1785 check_hir(
1786 indoc! {"
1787 namespace A {
1788 function Foo(a : Int, (b : Bool, c : Double, d : String), e : Result) : () {}
1789 function Bar() : () { let f = Foo(_, (true, 1.0, _), _); }
1790 }
1791 "},
1792 &expect![[r#"
1793 Package:
1794 Item 0 [0-160] (Public):
1795 Namespace (Ident 64 [10-11] "A"): Item 1, Item 2
1796 Item 1 [18-95] (Internal):
1797 Parent: 0
1798 Callable 0 [18-95] (function):
1799 name: Ident 1 [27-30] "Foo"
1800 input: Pat 2 [30-87] [Type (Int, (Bool, Double, String), Result)]: Tuple:
1801 Pat 3 [31-38] [Type Int]: Bind: Ident 4 [31-32] "a"
1802 Pat 5 [40-74] [Type (Bool, Double, String)]: Tuple:
1803 Pat 6 [41-49] [Type Bool]: Bind: Ident 7 [41-42] "b"
1804 Pat 8 [51-61] [Type Double]: Bind: Ident 9 [51-52] "c"
1805 Pat 10 [63-73] [Type String]: Bind: Ident 11 [63-64] "d"
1806 Pat 12 [76-86] [Type Result]: Bind: Ident 13 [76-77] "e"
1807 output: Unit
1808 functors: empty set
1809 body: SpecDecl 14 [18-95]: Impl:
1810 Block 15 [93-95]: <empty>
1811 adj: <none>
1812 ctl: <none>
1813 ctl-adj: <none>
1814 Item 2 [100-158] (Internal):
1815 Parent: 0
1816 Callable 16 [100-158] (function):
1817 name: Ident 17 [109-112] "Bar"
1818 input: Pat 18 [112-114] [Type Unit]: Unit
1819 output: Unit
1820 functors: empty set
1821 body: SpecDecl 19 [100-158]: Impl:
1822 Block 20 [120-158] [Type Unit]:
1823 Stmt 21 [122-156]: Local (Immutable):
1824 Pat 22 [126-127] [Type ((Int, String, Result) -> Unit)]: Bind: Ident 23 [126-127] "f"
1825 Expr 24 [130-155] [Type ((Int, String, Result) -> Unit)]: Expr Block: Block 61 [130-155] [Type ((Int, String, Result) -> Unit)]:
1826 Stmt 33 [138-142]: Local (Immutable):
1827 Pat 32 [138-142] [Type Bool]: Bind: Ident 30 [138-142] "arg"
1828 Expr 29 [138-142] [Type Bool]: Lit: Bool(true)
1829 Stmt 39 [144-147]: Local (Immutable):
1830 Pat 38 [144-147] [Type Double]: Bind: Ident 36 [144-147] "arg"
1831 Expr 35 [144-147] [Type Double]: Lit: Double(1)
1832 Stmt 62 [130-155]: Expr: Expr 63 [130-155] [Type ((Int, String, Result) -> Unit)]: Closure([30, 36], 3)
1833 adj: <none>
1834 ctl: <none>
1835 ctl-adj: <none>
1836 Item 3 [130-155] (Internal):
1837 Parent: 2
1838 Callable 56 [130-155] (function):
1839 name: Ident 57 [130-155] "<lambda>"
1840 input: Pat 53 [130-155] [Type (Bool, Double, (Int, String, Result))]: Tuple:
1841 Pat 54 [138-142] [Type Bool]: Bind: Ident 51 [138-142] "arg"
1842 Pat 55 [144-147] [Type Double]: Bind: Ident 52 [144-147] "arg"
1843 Pat 49 [133-155] [Type (Int, String, Result)]: Tuple:
1844 Pat 27 [134-135] [Type Int]: Bind: Ident 26 [134-135] "hole"
1845 Pat 42 [149-150] [Type String]: Bind: Ident 41 [149-150] "hole"
1846 Pat 46 [153-154] [Type Result]: Bind: Ident 45 [153-154] "hole"
1847 output: Unit
1848 functors: empty set
1849 body: SpecDecl 58 [130-155]: Impl:
1850 Block 59 [130-155] [Type Unit]:
1851 Stmt 60 [130-155]: Expr: Expr 50 [130-155] [Type Unit]: Call:
1852 Expr 25 [130-133] [Type ((Int, (Bool, Double, String), Result) -> Unit)]: Var: Item 1
1853 Expr 48 [133-155] [Type (Int, (Bool, Double, String), Result)]: Tuple:
1854 Expr 28 [134-135] [Type Int]: Var: Local 26
1855 Expr 44 [137-151] [Type (Bool, Double, String)]: Tuple:
1856 Expr 31 [138-142] [Type Bool]: Var: Local 51
1857 Expr 37 [144-147] [Type Double]: Var: Local 52
1858 Expr 43 [149-150] [Type String]: Var: Local 41
1859 Expr 47 [153-154] [Type Result]: Var: Local 45
1860 adj: <none>
1861 ctl: <none>
1862 ctl-adj: <none>"#]],
1863 );
1864}
1865
1866#[test]
1867fn body_missing_should_fail() {
1868 check_errors(
1869 indoc! {"
1870 namespace test {
1871 operation A(q : Qubit) : Unit is Adj {
1872 adjoint ... {}
1873 }
1874 }
1875 "},
1876 &expect![[r#"
1877 [
1878 MissingBody(
1879 Span {
1880 lo: 21,
1881 hi: 88,
1882 },
1883 ),
1884 ]
1885 "#]],
1886 );
1887}
1888
1889#[test]
1890fn duplicate_specialization() {
1891 check_errors(
1892 indoc! {"
1893 namespace test {
1894 operation Foo() : Unit {
1895 body ... {}
1896 body ... {}
1897 }
1898 }
1899 "},
1900 &expect![[r#"
1901 [
1902 DuplicateSpec(
1903 Span {
1904 lo: 54,
1905 hi: 65,
1906 },
1907 ),
1908 DuplicateSpec(
1909 Span {
1910 lo: 74,
1911 hi: 85,
1912 },
1913 ),
1914 ]
1915 "#]],
1916 );
1917}
1918
1919#[test]
1920fn duplicate_specialization_with_gen() {
1921 check_errors(
1922 indoc! {"
1923 namespace test {
1924 operation Foo() : Unit {
1925 body ... {}
1926 body auto;
1927 body intrinsic;
1928 }
1929 }
1930 "},
1931 &expect![[r#"
1932 [
1933 DuplicateSpec(
1934 Span {
1935 lo: 54,
1936 hi: 65,
1937 },
1938 ),
1939 DuplicateSpec(
1940 Span {
1941 lo: 74,
1942 hi: 84,
1943 },
1944 ),
1945 DuplicateSpec(
1946 Span {
1947 lo: 93,
1948 hi: 108,
1949 },
1950 ),
1951 ]
1952 "#]],
1953 );
1954}
1955
1956#[test]
1957fn partial_app_unknown_callable() {
1958 check_hir(
1959 indoc! {"
1960 namespace A {
1961 function Foo() : () { let f = Unknown(true, _, _); }
1962 }
1963 "},
1964 &expect![[r#"
1965 Package:
1966 Item 0 [0-72] (Public):
1967 Namespace (Ident 14 [10-11] "A"): Item 1
1968 Item 1 [18-70] (Internal):
1969 Parent: 0
1970 Callable 0 [18-70] (function):
1971 name: Ident 1 [27-30] "Foo"
1972 input: Pat 2 [30-32] [Type Unit]: Unit
1973 output: Unit
1974 functors: empty set
1975 body: SpecDecl 3 [18-70]: Impl:
1976 Block 4 [38-70] [Type Unit]:
1977 Stmt 5 [40-68]: Local (Immutable):
1978 Pat 6 [44-45] [Type ?3]: Bind: Ident 7 [44-45] "f"
1979 Expr 8 [48-67] [Type ?3]: Call:
1980 Expr 9 [48-55] [Type ?]: Var: Err
1981 Expr 10 [55-67] [Type (Bool, ?1, ?2)]: Tuple:
1982 Expr 11 [56-60] [Type Bool]: Lit: Bool(true)
1983 Expr 12 [62-63] [Type ?1]: Hole
1984 Expr 13 [65-66] [Type ?2]: Hole
1985 adj: <none>
1986 ctl: <none>
1987 ctl-adj: <none>"#]],
1988 );
1989}
1990
1991#[test]
1992fn partial_app_too_many_args() {
1993 check_hir(
1994 indoc! {"
1995 namespace A {
1996 function Foo(x : Int) : Int { x }
1997 function Bar() : () { let f = Foo(1, _, _); }
1998 }
1999 "},
2000 &expect![[r#"
2001 Package:
2002 Item 0 [0-103] (Public):
2003 Namespace (Ident 22 [10-11] "A"): Item 1, Item 2
2004 Item 1 [18-51] (Internal):
2005 Parent: 0
2006 Callable 0 [18-51] (function):
2007 name: Ident 1 [27-30] "Foo"
2008 input: Pat 2 [31-38] [Type Int]: Bind: Ident 3 [31-32] "x"
2009 output: Int
2010 functors: empty set
2011 body: SpecDecl 4 [18-51]: Impl:
2012 Block 5 [46-51] [Type Int]:
2013 Stmt 6 [48-49]: Expr: Expr 7 [48-49] [Type Int]: Var: Local 3
2014 adj: <none>
2015 ctl: <none>
2016 ctl-adj: <none>
2017 Item 2 [56-101] (Internal):
2018 Parent: 0
2019 Callable 8 [56-101] (function):
2020 name: Ident 9 [65-68] "Bar"
2021 input: Pat 10 [68-70] [Type Unit]: Unit
2022 output: Unit
2023 functors: empty set
2024 body: SpecDecl 11 [56-101]: Impl:
2025 Block 12 [76-101] [Type Unit]:
2026 Stmt 13 [78-99]: Local (Immutable):
2027 Pat 14 [82-83] [Type Int]: Bind: Ident 15 [82-83] "f"
2028 Expr 16 [86-98] [Type Int]: Call:
2029 Expr 17 [86-89] [Type (Int -> Int)]: Var: Item 1
2030 Expr 18 [89-98] [Type (Int, ?1, ?2)]: Tuple:
2031 Expr 19 [90-91] [Type Int]: Lit: Int(1)
2032 Expr 20 [93-94] [Type ?1]: Hole
2033 Expr 21 [96-97] [Type ?2]: Hole
2034 adj: <none>
2035 ctl: <none>
2036 ctl-adj: <none>"#]],
2037 );
2038}
2039
2040#[test]
2041fn partial_app_bound_to_non_arrow_ty() {
2042 check_hir(
2043 indoc! {"
2044 namespace A {
2045 function Foo(x : Int, y : Int) : Int { x + y }
2046 function Bar() : () {
2047 let f : Int = Foo(1, _);
2048 }
2049 }
2050 "},
2051 &expect![[r#"
2052 Package:
2053 Item 0 [0-131] (Public):
2054 Namespace (Ident 45 [10-11] "A"): Item 1, Item 2
2055 Item 1 [18-64] (Internal):
2056 Parent: 0
2057 Callable 0 [18-64] (function):
2058 name: Ident 1 [27-30] "Foo"
2059 input: Pat 2 [30-48] [Type (Int, Int)]: Tuple:
2060 Pat 3 [31-38] [Type Int]: Bind: Ident 4 [31-32] "x"
2061 Pat 5 [40-47] [Type Int]: Bind: Ident 6 [40-41] "y"
2062 output: Int
2063 functors: empty set
2064 body: SpecDecl 7 [18-64]: Impl:
2065 Block 8 [55-64] [Type Int]:
2066 Stmt 9 [57-62]: Expr: Expr 10 [57-62] [Type Int]: BinOp (Add):
2067 Expr 11 [57-58] [Type Int]: Var: Local 4
2068 Expr 12 [61-62] [Type Int]: Var: Local 6
2069 adj: <none>
2070 ctl: <none>
2071 ctl-adj: <none>
2072 Item 2 [69-129] (Internal):
2073 Parent: 0
2074 Callable 13 [69-129] (function):
2075 name: Ident 14 [78-81] "Bar"
2076 input: Pat 15 [81-83] [Type Unit]: Unit
2077 output: Unit
2078 functors: empty set
2079 body: SpecDecl 16 [69-129]: Impl:
2080 Block 17 [89-129] [Type Unit]:
2081 Stmt 18 [99-123]: Local (Immutable):
2082 Pat 19 [103-110] [Type Int]: Bind: Ident 20 [103-104] "f"
2083 Expr 21 [113-122] [Type (Int -> Int)]: Expr Block: Block 42 [113-122] [Type (Int -> Int)]:
2084 Stmt 27 [117-118]: Local (Immutable):
2085 Pat 26 [117-118] [Type Int]: Bind: Ident 24 [117-118] "arg"
2086 Expr 23 [117-118] [Type Int]: Lit: Int(1)
2087 Stmt 43 [113-122]: Expr: Expr 44 [113-122] [Type (Int -> Int)]: Closure([24], 3)
2088 adj: <none>
2089 ctl: <none>
2090 ctl-adj: <none>
2091 Item 3 [113-122] (Internal):
2092 Parent: 2
2093 Callable 37 [113-122] (function):
2094 name: Ident 38 [113-122] "<lambda>"
2095 input: Pat 35 [113-122] [Type (Int, Int)]: Tuple:
2096 Pat 36 [117-118] [Type Int]: Bind: Ident 34 [117-118] "arg"
2097 Pat 30 [120-121] [Type Int]: Bind: Ident 29 [120-121] "hole"
2098 output: Int
2099 functors: empty set
2100 body: SpecDecl 39 [113-122]: Impl:
2101 Block 40 [113-122] [Type Int]:
2102 Stmt 41 [113-122]: Expr: Expr 33 [113-122] [Type Int]: Call:
2103 Expr 22 [113-116] [Type ((Int, Int) -> Int)]: Var: Item 1
2104 Expr 32 [116-122] [Type (Int, Int)]: Tuple:
2105 Expr 25 [117-118] [Type Int]: Var: Local 34
2106 Expr 31 [120-121] [Type Int]: Var: Local 29
2107 adj: <none>
2108 ctl: <none>
2109 ctl-adj: <none>"#]],
2110 );
2111}
2112
2113#[test]
2114fn partial_app_hole_as_callee() {
2115 check_hir(
2116 indoc! {"
2117 namespace A {
2118 @EntryPoint()
2119 operation Main() : Result[] {
2120 let f = _(_);
2121 let res = f(4);
2122 return [res];
2123 }
2124 }
2125 "},
2126 &expect![[r#"
2127 Package:
2128 Item 0 [0-141] (Public):
2129 Namespace (Ident 21 [10-11] "A"): Item 1
2130 Item 1 [18-139] (Internal):
2131 Parent: 0
2132 EntryPoint
2133 Callable 0 [36-139] (operation):
2134 name: Ident 1 [46-50] "Main"
2135 input: Pat 2 [50-52] [Type Unit]: Unit
2136 output: Result[]
2137 functors: empty set
2138 body: SpecDecl 3 [36-139]: Impl:
2139 Block 4 [64-139] [Type Result[]]:
2140 Stmt 5 [74-87]: Local (Immutable):
2141 Pat 6 [78-79] [Type ?3]: Bind: Ident 7 [78-79] "f"
2142 Expr 8 [82-86] [Type ?3]: Call:
2143 Expr 9 [82-83] [Type ?1]: Hole
2144 Expr 10 [84-85] [Type ?2]: Hole
2145 Stmt 11 [96-111]: Local (Immutable):
2146 Pat 12 [100-103] [Type Result]: Bind: Ident 13 [100-103] "res"
2147 Expr 14 [106-110] [Type Result]: Call:
2148 Expr 15 [106-107] [Type ?3]: Var: Local 7
2149 Expr 16 [108-109] [Type Int]: Lit: Int(4)
2150 Stmt 17 [120-133]: Semi: Expr 18 [120-132] [Type Unit]: Return: Expr 19 [127-132] [Type Result[]]: Array:
2151 Expr 20 [128-131] [Type Result]: Var: Local 13
2152 adj: <none>
2153 ctl: <none>
2154 ctl-adj: <none>"#]],
2155 );
2156}
2157
2158#[test]
2159fn invalid_elided() {
2160 check_errors(
2161 indoc! {r#"
2162 namespace input {
2163 operation Foo() : Unit {
2164 let ... = 3;
2165 }
2166 }
2167 "#},
2168 &expect![[r#"
2169 [
2170 InvalidElidedPat(
2171 Span {
2172 lo: 59,
2173 hi: 62,
2174 },
2175 ),
2176 ]
2177 "#]],
2178 );
2179}
2180
2181#[test]
2182fn invalid_spec_pat() {
2183 check_errors(
2184 indoc! {r#"
2185 namespace input {
2186 operation Foo() : Unit is Ctl {
2187 body bar {}
2188 controlled (foo, bar) {}
2189 }
2190 }
2191 "#},
2192 &expect![[r#"
2193 [
2194 InvalidSpecPat(
2195 Span {
2196 lo: 67,
2197 hi: 70,
2198 },
2199 ),
2200 InvalidSpecPat(
2201 Span {
2202 lo: 93,
2203 hi: 103,
2204 },
2205 ),
2206 ]
2207 "#]],
2208 );
2209}
2210
2211#[test]
2212fn test_measurement_attr_on_function_issues_error() {
2213 check_errors(
2214 indoc! {r#"
2215 namespace Test {
2216 @Measurement()
2217 function Foo(q: Qubit) : Result {
2218 body intrinsic;
2219 }
2220 }
2221 "#},
2222 &expect![[r#"
2223 [
2224 InvalidAttrOnFunction(
2225 "Measurement",
2226 Span {
2227 lo: 49,
2228 hi: 52,
2229 },
2230 ),
2231 ]
2232 "#]],
2233 );
2234}
2235
2236#[test]
2237fn test_reset_attr_on_function_issues_error() {
2238 check_errors(
2239 indoc! {r#"
2240 namespace Test {
2241 @Reset()
2242 function Foo(q: Qubit) : Unit {
2243 body intrinsic;
2244 }
2245 }
2246 "#},
2247 &expect![[r#"
2248 [
2249 InvalidAttrOnFunction(
2250 "Reset",
2251 Span {
2252 lo: 43,
2253 hi: 46,
2254 },
2255 ),
2256 ]
2257 "#]],
2258 );
2259}
2260
2261#[test]
2262fn item_docs() {
2263 check_hir(
2264 "/// This is a namespace.
2265 namespace A {
2266 /// This is a newtype.
2267 newtype Foo = ();
2268
2269 /// This is a function.
2270 function Bar() : () {}
2271
2272 /// This is an operation.
2273 operation Baz() : () {}
2274 }",
2275 &expect![[r#"
2276 Package:
2277 Item 0 [0-268] (Public):
2278 Doc:
2279 This is a namespace.
2280 Namespace (Ident 11 [43-44] "A"): Item 1, Item 2, Item 3
2281 Item 1 [59-111] (Internal):
2282 Parent: 0
2283 Doc:
2284 This is a newtype.
2285 Type (Ident 0 [102-105] "Foo"): UDT [59-111]:
2286 TyDef [108-110]: Field:
2287 type: Unit
2288 Item 2 [125-183] (Internal):
2289 Parent: 0
2290 Doc:
2291 This is a function.
2292 Callable 1 [161-183] (function):
2293 name: Ident 2 [170-173] "Bar"
2294 input: Pat 3 [173-175] [Type Unit]: Unit
2295 output: Unit
2296 functors: empty set
2297 body: SpecDecl 4 [161-183]: Impl:
2298 Block 5 [181-183]: <empty>
2299 adj: <none>
2300 ctl: <none>
2301 ctl-adj: <none>
2302 Item 3 [197-258] (Internal):
2303 Parent: 0
2304 Doc:
2305 This is an operation.
2306 Callable 6 [235-258] (operation):
2307 name: Ident 7 [245-248] "Baz"
2308 input: Pat 8 [248-250] [Type Unit]: Unit
2309 output: Unit
2310 functors: empty set
2311 body: SpecDecl 9 [235-258]: Impl:
2312 Block 10 [256-258]: <empty>
2313 adj: <none>
2314 ctl: <none>
2315 ctl-adj: <none>"#]],
2316 );
2317}
2318
2319#[test]
2320fn nested_params() {
2321 check_hir(
2322 "namespace Test { function Foo<'T>(f: 'T => ()) : () { } }",
2323 &expect![[r#"
2324 Package:
2325 Item 0 [0-57] (Public):
2326 Namespace (Ident 6 [10-14] "Test"): Item 1
2327 Item 1 [17-55] (Internal):
2328 Parent: 0
2329 Callable 0 [17-55] (function):
2330 name: Ident 1 [26-29] "Foo"
2331 generics:
2332 0: type 'T
2333 1: functor (empty set)
2334 input: Pat 2 [34-45] [Type (Param<"'T": 0> => Unit is Param<1>)]: Bind: Ident 3 [34-35] "f"
2335 output: Unit
2336 functors: empty set
2337 body: SpecDecl 4 [17-55]: Impl:
2338 Block 5 [52-55]: <empty>
2339 adj: <none>
2340 ctl: <none>
2341 ctl-adj: <none>"#]],
2342 );
2343}
2344
2345#[test]
2346fn lambda_with_invalid_free_variable() {
2347 check_hir(
2348 "namespace Test{ operation T(): Unit {body fakelocal{() => fakelocal;}}}",
2349 &expect![[r#"
2350 Package:
2351 Item 0 [0-71] (Public):
2352 Namespace (Ident 17 [10-14] "Test"): Item 1
2353 Item 1 [16-70] (Internal):
2354 Parent: 0
2355 Callable 0 [16-70] (operation):
2356 name: Ident 1 [26-27] "T"
2357 input: Pat 2 [27-29] [Type Unit]: Unit
2358 output: Unit
2359 functors: empty set
2360 body: SpecDecl 3 [37-69]: Impl:
2361 Block 4 [51-69] [Type Unit]:
2362 Stmt 5 [52-68]: Semi: Expr 6 [52-67] [Type (Unit => Unit)]: Closure([9], 2)
2363 adj: <none>
2364 ctl: <none>
2365 ctl-adj: <none>
2366 Item 2 [52-67] (Internal):
2367 Parent: 1
2368 Callable 12 [52-67] (operation):
2369 name: Ident 13 [52-67] "<lambda>"
2370 input: Pat 11 [52-67] [Type (Unit,)]: Tuple:
2371 Pat 7 [52-54] [Type Unit]: Unit
2372 output: Unit
2373 functors: empty set
2374 body: SpecDecl 14 [58-67]: Impl:
2375 Block 15 [58-67] [Type Unit]:
2376 Stmt 16 [58-67]: Expr: Expr 8 [58-67] [Type Unit]: Var: Local 10
2377 adj: <none>
2378 ctl: <none>
2379 ctl-adj: <none>"#]],
2380 );
2381}
2382
2383#[test]
2384fn duplicate_commas_in_tydef() {
2385 check_hir(
2386 indoc! {r#"
2387 namespace test {
2388 newtype Foo = (Int,,);
2389 }
2390 "#},
2391 &expect![[r#"
2392 Package:
2393 Item 0 [0-45] (Public):
2394 Namespace (Ident 1 [10-14] "test"): Item 1
2395 Item 1 [21-43] (Internal):
2396 Parent: 0
2397 Type (Ident 0 [29-32] "Foo"): UDT [21-43]:
2398 TyDef [35-42]: Tuple:
2399 TyDef [36-39]: Field:
2400 type: Int
2401 TyDef [40-40]: Field:
2402 type: ?"#]],
2403 );
2404}
2405
2406#[test]
2407fn duplicate_commas_in_generics() {
2408 check_hir(
2409 indoc! {r#"
2410 namespace test {
2411 function Foo<'T,,>(x : 'T) : Unit {}
2412 }
2413 "#},
2414 &expect![[r#"
2415 Package:
2416 Item 0 [0-59] (Public):
2417 Namespace (Ident 6 [10-14] "test"): Item 1
2418 Item 1 [21-57] (Internal):
2419 Parent: 0
2420 Callable 0 [21-57] (function):
2421 name: Ident 1 [30-33] "Foo"
2422 generics:
2423 0: type 'T
2424 1: type
2425 input: Pat 2 [40-46] [Type Param<"'T": 0>]: Bind: Ident 3 [40-41] "x"
2426 output: Unit
2427 functors: empty set
2428 body: SpecDecl 4 [21-57]: Impl:
2429 Block 5 [55-57]: <empty>
2430 adj: <none>
2431 ctl: <none>
2432 ctl-adj: <none>"#]],
2433 );
2434}
2435
2436#[test]
2437fn duplicate_commas_in_pat() {
2438 check_hir(
2439 indoc! {r#"
2440 namespace test {
2441 operation Foo() : Unit {
2442 let (x,,) = (1, 2);
2443 }
2444 }"#},
2445 &expect![[r#"
2446 Package:
2447 Item 0 [0-81] (Public):
2448 Namespace (Ident 13 [10-14] "test"): Item 1
2449 Item 1 [21-79] (Internal):
2450 Parent: 0
2451 Callable 0 [21-79] (operation):
2452 name: Ident 1 [31-34] "Foo"
2453 input: Pat 2 [34-36] [Type Unit]: Unit
2454 output: Unit
2455 functors: empty set
2456 body: SpecDecl 3 [21-79]: Impl:
2457 Block 4 [44-79] [Type Unit]:
2458 Stmt 5 [54-73]: Local (Immutable):
2459 Pat 6 [58-63] [Type (Int, ?)]: Tuple:
2460 Pat 7 [59-60] [Type Int]: Bind: Ident 8 [59-60] "x"
2461 Pat 9 [61-61] [Type ?]: Err
2462 Expr 10 [66-72] [Type (Int, Int)]: Tuple:
2463 Expr 11 [67-68] [Type Int]: Lit: Int(1)
2464 Expr 12 [70-71] [Type Int]: Lit: Int(2)
2465 adj: <none>
2466 ctl: <none>
2467 ctl-adj: <none>"#]],
2468 );
2469}
2470
2471#[test]
2472fn duplicate_commas_in_tuple() {
2473 check_hir(
2474 indoc! {r#"
2475 namespace test {
2476 operation Foo() : Unit {
2477 let x = (1,,3);
2478 }
2479 }"#},
2480 &expect![[r#"
2481 Package:
2482 Item 0 [0-77] (Public):
2483 Namespace (Ident 12 [10-14] "test"): Item 1
2484 Item 1 [21-75] (Internal):
2485 Parent: 0
2486 Callable 0 [21-75] (operation):
2487 name: Ident 1 [31-34] "Foo"
2488 input: Pat 2 [34-36] [Type Unit]: Unit
2489 output: Unit
2490 functors: empty set
2491 body: SpecDecl 3 [21-75]: Impl:
2492 Block 4 [44-75] [Type Unit]:
2493 Stmt 5 [54-69]: Local (Immutable):
2494 Pat 6 [58-59] [Type (Int, ?, Int)]: Bind: Ident 7 [58-59] "x"
2495 Expr 8 [62-68] [Type (Int, ?, Int)]: Tuple:
2496 Expr 9 [63-64] [Type Int]: Lit: Int(1)
2497 Expr 10 [65-65] [Type ?]: Err
2498 Expr 11 [66-67] [Type Int]: Lit: Int(3)
2499 adj: <none>
2500 ctl: <none>
2501 ctl-adj: <none>"#]],
2502 );
2503}
2504
2505#[test]
2506fn duplicate_commas_in_arg_tuple() {
2507 check_hir(
2508 indoc! {r#"
2509 namespace test {
2510 operation Foo(a : Int, b : Int, c : Int) : Int {
2511 Foo(a,,c)
2512 }
2513 }"#},
2514 &expect![[r#"
2515 Package:
2516 Item 0 [0-95] (Public):
2517 Namespace (Ident 18 [10-14] "test"): Item 1
2518 Item 1 [21-93] (Internal):
2519 Parent: 0
2520 Callable 0 [21-93] (operation):
2521 name: Ident 1 [31-34] "Foo"
2522 input: Pat 2 [34-61] [Type (Int, Int, Int)]: Tuple:
2523 Pat 3 [35-42] [Type Int]: Bind: Ident 4 [35-36] "a"
2524 Pat 5 [44-51] [Type Int]: Bind: Ident 6 [44-45] "b"
2525 Pat 7 [53-60] [Type Int]: Bind: Ident 8 [53-54] "c"
2526 output: Int
2527 functors: empty set
2528 body: SpecDecl 9 [21-93]: Impl:
2529 Block 10 [68-93] [Type Int]:
2530 Stmt 11 [78-87]: Expr: Expr 12 [78-87] [Type Int]: Call:
2531 Expr 13 [78-81] [Type ((Int, Int, Int) => Int)]: Var: Item 1
2532 Expr 14 [81-87] [Type (Int, ?, Int)]: Tuple:
2533 Expr 15 [82-83] [Type Int]: Var: Local 4
2534 Expr 16 [84-84] [Type ?]: Err
2535 Expr 17 [85-86] [Type Int]: Var: Local 8
2536 adj: <none>
2537 ctl: <none>
2538 ctl-adj: <none>"#]],
2539 );
2540}
2541
2542#[test]
2543fn ignore_item_in_attribute() {
2544 check_hir(
2545 "namespace Test {
2546 @Attr{function Bar() : Unit { Bar }}
2547 function Foo() : Unit {}
2548 }",
2549 &expect![[r#"
2550 Package:
2551 Item 0 [0-112] (Public):
2552 Namespace (Ident 5 [10-14] "Test"): Item 1
2553 Item 1 [29-102] (Internal):
2554 Parent: 0
2555 Callable 0 [78-102] (function):
2556 name: Ident 1 [87-90] "Foo"
2557 input: Pat 2 [90-92] [Type Unit]: Unit
2558 output: Unit
2559 functors: empty set
2560 body: SpecDecl 3 [78-102]: Impl:
2561 Block 4 [100-102]: <empty>
2562 adj: <none>
2563 ctl: <none>
2564 ctl-adj: <none>"#]],
2565 );
2566}
2567