microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.0.33

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_frontend/src/lower/tests.rs

2219lines · modecode

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