microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/copilot

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_frontend/src/lower/tests.rs

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