microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
iadavis/pipeline-issue-debugging

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/compiler/qsc_frontend/src/resolve/tests.rs

6116lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use super::{Error, Locals, Names, Res};
5use crate::{
6 compile,
7 resolve::{Importable, Local, Resolver, imports::iter_valid_items},
8};
9use expect_test::{Expect, expect};
10use indoc::indoc;
11use qsc_ast::ast::{Idents, ImportKind, Item, ItemKind, PathKind};
12use qsc_ast::{
13 assigner::Assigner as AstAssigner,
14 ast::{Ident, NodeId, Package, Path, TopLevelNode},
15 mut_visit::MutVisitor,
16 visit::{self, Visitor},
17};
18
19use qsc_data_structures::{
20 language_features::LanguageFeatures,
21 namespaces::{NamespaceId, NamespaceTreeRoot},
22 span::Span,
23 target::TargetCapabilityFlags,
24};
25use qsc_hir::{assigner::Assigner as HirAssigner, hir::PackageId};
26use rustc_hash::FxHashMap;
27use std::rc::Rc;
28use std::{fmt::Write, vec};
29
30#[derive(Debug)]
31enum Change {
32 Res(Res),
33 NamespaceId(NamespaceId),
34 Import(Option<Res>, Res),
35}
36
37impl From<Res> for Change {
38 fn from(res: Res) -> Self {
39 Self::Res(res)
40 }
41}
42
43impl From<NamespaceId> for Change {
44 fn from(ns_id: NamespaceId) -> Self {
45 Self::NamespaceId(ns_id)
46 }
47}
48
49struct Renamer<'a> {
50 names: &'a Names,
51 changes: Vec<(Span, Change)>,
52 namespaces: NamespaceTreeRoot,
53 aliases: FxHashMap<Vec<Rc<str>>, NamespaceId>,
54}
55
56impl<'a> Renamer<'a> {
57 fn new(names: &'a Names, namespaces: NamespaceTreeRoot) -> Self {
58 Self {
59 names,
60 changes: Vec::new(),
61 namespaces,
62 aliases: FxHashMap::default(),
63 }
64 }
65
66 fn rename(&self, input: &mut String) {
67 for (span, change) in self.changes.iter().rev() {
68 let name = match change {
69 Change::Import(res_source, res) => {
70 format!(
71 "{} as {}",
72 res_source
73 .as_ref()
74 .map(Self::format_res)
75 .unwrap_or_default(),
76 Self::format_res(res)
77 )
78 }
79 Change::Res(res) => Self::format_res(res),
80 Change::NamespaceId(ns_id) => format!("namespace{}", Into::<usize>::into(ns_id)),
81 };
82 input.replace_range((span.lo as usize)..(span.hi as usize), &name);
83 }
84 }
85
86 fn format_res(res: &Res) -> String {
87 match res {
88 Res::Item(item, _) => format!("package{}_item{}", item.package, item.item),
89 Res::Local(node) => format!("local{node}"),
90 Res::PrimTy(prim) => format!("{prim:?}"),
91 Res::UnitTy => "Unit".to_string(),
92 Res::Param { id, .. } => format!("param{id}"),
93 Res::Importable(item_kind) => match item_kind {
94 Importable::Callable(item_id, _) | Importable::Ty(item_id, _) => {
95 format!("item{}", item_id.item)
96 }
97 Importable::Namespace(namespace_id, _) => {
98 format!("namespace{}", usize::from(namespace_id))
99 }
100 },
101 }
102 }
103}
104
105impl Visitor<'_> for Renamer<'_> {
106 fn visit_path(&mut self, path: &Path) {
107 if let Some(res) = self.names.get(path.id) {
108 // The whole path node can be a resolved name
109 self.changes.push((path.span, res.clone().into()));
110 return;
111 }
112
113 let ns_id = self.find_namespace_id(path);
114 if let Some(ns_id) = ns_id {
115 // The whole path can be a namespace
116 self.changes.push((path.span, ns_id.into()));
117 return;
118 }
119
120 // Individual ident nodes can be resolved names
121 visit::walk_path(self, path);
122 }
123
124 fn visit_idents(&mut self, idents: &[Ident]) {
125 let ns_id = self.find_namespace_id(&idents);
126 if let Some(ns_id) = ns_id {
127 self.changes.push((idents.full_span(), ns_id.into()));
128 return;
129 }
130 visit::walk_idents(self, idents);
131 }
132
133 fn visit_ident(&mut self, ident: &Ident) {
134 if let Some(res) = self.names.get(ident.id) {
135 self.changes.push((ident.span, res.clone().into()));
136 }
137 }
138
139 fn visit_item(&mut self, item: &'_ Item) {
140 match &*item.kind {
141 ItemKind::Open(PathKind::Ok(namespace), Some(alias)) => {
142 if let Some(ns_id) = self.namespaces.get_namespace_id(namespace.str_iter()) {
143 self.aliases.insert(vec![alias.name.clone()], ns_id);
144 } else {
145 return;
146 }
147 }
148 ItemKind::ImportOrExport(decl) => {
149 for item in iter_valid_items(decl) {
150 let path_res = self.names.get(item.path.id);
151 let name_res = self.names.get(item.name().id);
152
153 match (path_res, name_res) {
154 (Some(source_res), Some(res)) => {
155 self.changes.push((
156 item.span,
157 Change::Import(Some(source_res.clone()), res.clone()),
158 ));
159 }
160 (None, Some(res)) => {
161 self.changes.push((
162 if let ImportKind::Direct { alias: Some(alias) } = &item.kind {
163 alias.span
164 } else {
165 Span {
166 lo: item.span.hi,
167 hi: item.span.hi,
168 }
169 },
170 Change::Import(None, res.clone()),
171 ));
172 }
173 (Some(source_res), None) => {
174 self.changes
175 .push((item.path.span, Change::Res(source_res.clone())));
176 }
177 (None, None) => {}
178 }
179 }
180 return;
181 }
182 _ => (),
183 }
184 visit::walk_item(self, item);
185 }
186}
187
188impl Renamer<'_> {
189 fn find_namespace_id(&mut self, idents: &impl Idents) -> Option<NamespaceId> {
190 self.namespaces
191 .get_namespace_id(idents.str_iter())
192 .or_else(|| {
193 self.aliases
194 .get(&idents.rc_str_iter().cloned().collect::<Vec<_>>())
195 .copied()
196 })
197 }
198}
199
200fn check(input: &str, expect: &Expect) {
201 expect.assert_eq(&resolve_names(input, TargetCapabilityFlags::all()));
202}
203
204fn check_with_capabilities(input: &str, capabilities: TargetCapabilityFlags, expect: &Expect) {
205 expect.assert_eq(&resolve_names(input, capabilities));
206}
207
208fn resolve_names(input: &str, capabilities: TargetCapabilityFlags) -> String {
209 let (package, names, _, errors, namespaces) =
210 compile(input, LanguageFeatures::default(), capabilities);
211 let mut renamer = Renamer::new(&names, namespaces);
212 renamer.visit_package(&package);
213 let mut output = input.to_string();
214 renamer.rename(&mut output);
215 if !errors.is_empty() {
216 output += "\n";
217 }
218 for error in &errors {
219 writeln!(output, "// {error:?}").expect("string should be writable");
220 }
221 output
222}
223
224fn compile(
225 input: &str,
226 language_features: LanguageFeatures,
227 capabilities: TargetCapabilityFlags,
228) -> (Package, Names, Locals, Vec<Error>, NamespaceTreeRoot) {
229 let (namespaces, parse_errors) = qsc_parse::namespaces(input, None, language_features);
230 assert!(parse_errors.is_empty(), "parse failed: {parse_errors:#?}");
231 let mut package = Package {
232 id: NodeId::default(),
233 nodes: namespaces
234 .into_iter()
235 .map(TopLevelNode::Namespace)
236 .collect::<Vec<_>>()
237 .into_boxed_slice(),
238 entry: None,
239 };
240
241 AstAssigner::new().visit_package(&mut package);
242
243 let mut cond_compile = compile::preprocess::Conditional::new(capabilities);
244 cond_compile.visit_package(&mut package);
245 let dropped_names = cond_compile.into_names();
246
247 let mut assigner = HirAssigner::new();
248 // insert the core namespace
249
250 // These tests do not happen in the context of a `PackageStore`,
251 // so we just make up a fake PackageId.
252 let package_id = PackageId::CORE.successor().successor();
253
254 let mut globals = super::GlobalTable::new();
255 let mut errors = globals.add_local_package(&mut assigner, &package, package_id);
256 let mut resolver = Resolver::new(package_id, globals, dropped_names);
257 resolver.resolve(&mut assigner, &package);
258
259 let (names, globals, locals, mut resolve_errors) = resolver.into_result();
260 errors.append(&mut resolve_errors);
261 (package, names, locals, errors, globals.namespaces)
262}
263
264#[test]
265fn global_callable() {
266 check(
267 indoc! {"
268 namespace Foo {
269 function A() : Unit {}
270
271 function B() : Unit {
272 A();
273 }
274 }
275 "},
276 &expect![[r#"
277 namespace namespace3 {
278 function package2_item1() : Unit {}
279
280 function package2_item2() : Unit {
281 package2_item1();
282 }
283 }
284 "#]],
285 );
286}
287
288#[test]
289fn global_callable_recursive() {
290 check(
291 indoc! {
292 "namespace Foo {
293 function A() : Unit {
294 A();
295 }
296 }
297 "},
298 &expect![[r#"
299 namespace namespace3 {
300 function package2_item1() : Unit {
301 package2_item1();
302 }
303 }
304 "#]],
305 );
306}
307
308#[test]
309fn global_callable_internal() {
310 check(
311 indoc! {"
312 namespace Foo {
313 internal function A() : Unit {}
314
315 function B() : Unit {
316 A();
317 }
318 }
319 "},
320 &expect![[r#"
321 namespace namespace3 {
322 internal function package2_item1() : Unit {}
323
324 function package2_item2() : Unit {
325 package2_item1();
326 }
327 }
328 "#]],
329 );
330}
331
332#[test]
333fn global_callable_duplicate_error() {
334 check(
335 indoc! {"
336 namespace Foo {
337 function A() : Unit {}
338 operation A() : Unit {}
339 }
340 "},
341 &expect![[r#"
342 namespace namespace3 {
343 function package2_item1() : Unit {}
344 operation package2_item2() : Unit {}
345 }
346
347 // Duplicate("A", "Foo", Span { lo: 57, hi: 58 })
348 "#]],
349 );
350}
351
352#[test]
353fn global_path() {
354 check(
355 indoc! {"
356 namespace Foo {
357 function A() : Unit {}
358 }
359
360 namespace Bar {
361 function B() : Unit {
362 Foo.A();
363 }
364 }
365 "},
366 &expect![[r#"
367 namespace namespace3 {
368 function package2_item1() : Unit {}
369 }
370
371 namespace namespace4 {
372 function package2_item3() : Unit {
373 package2_item1();
374 }
375 }
376 "#]],
377 );
378}
379
380#[test]
381fn open_namespace() {
382 check(
383 indoc! {"
384 namespace Foo {
385 function A() : Unit {}
386 }
387
388 namespace Bar {
389 open Foo;
390
391 function B() : Unit {
392 A();
393 }
394 }
395 "},
396 &expect![[r#"
397 namespace namespace3 {
398 function package2_item1() : Unit {}
399 }
400
401 namespace namespace4 {
402 open namespace3;
403
404 function package2_item3() : Unit {
405 package2_item1();
406 }
407 }
408 "#]],
409 );
410}
411
412#[test]
413fn open_alias() {
414 check(
415 indoc! {"
416 namespace Foo {
417 function A() : Unit {}
418 }
419
420 namespace Bar {
421 open Foo as F;
422
423 function B() : Unit {
424 F.A();
425 }
426 }
427 "},
428 &expect![[r#"
429 namespace namespace3 {
430 function package2_item1() : Unit {}
431 }
432
433 namespace namespace4 {
434 open namespace3 as F;
435
436 function package2_item3() : Unit {
437 package2_item1();
438 }
439 }
440 "#]],
441 );
442}
443
444#[test]
445fn prelude_callable() {
446 check(
447 indoc! {"
448 namespace Std.Core {
449 function A() : Unit {}
450 }
451
452 namespace Foo {
453 function B() : Unit {
454 A();
455 }
456 }
457 "},
458 &expect![[r#"
459 namespace namespace2 {
460 function package2_item1() : Unit {}
461 }
462
463 namespace namespace3 {
464 function package2_item3() : Unit {
465 package2_item1();
466 }
467 }
468 "#]],
469 );
470}
471
472#[test]
473fn parent_namespace_shadows_prelude() {
474 check(
475 indoc! {"
476 namespace Std.Core {
477 function A() : Unit {}
478 }
479
480 namespace Foo {
481 function A() : Unit {}
482
483 function B() : Unit {
484 A();
485 }
486 }
487 "},
488 &expect![[r#"
489 namespace namespace2 {
490 function package2_item1() : Unit {}
491 }
492
493 namespace namespace3 {
494 function package2_item3() : Unit {}
495
496 function package2_item4() : Unit {
497 package2_item3();
498 }
499 }
500 "#]],
501 );
502}
503
504#[test]
505fn open_shadows_prelude() {
506 check(
507 indoc! {"
508 namespace Std.Core {
509 function A() : Unit {}
510 }
511
512 namespace Foo {
513 function A() : Unit {}
514 }
515
516 namespace Bar {
517 open Foo;
518
519 function B() : Unit {
520 A();
521 }
522 }
523 "},
524 &expect![[r#"
525 namespace namespace2 {
526 function package2_item1() : Unit {}
527 }
528
529 namespace namespace3 {
530 function package2_item3() : Unit {}
531 }
532
533 namespace namespace4 {
534 open namespace3;
535
536 function package2_item5() : Unit {
537 package2_item3();
538 }
539 }
540 "#]],
541 );
542}
543
544#[test]
545fn ambiguous_prelude() {
546 check(
547 indoc! {"
548 namespace Std.Canon {
549 function A() : Unit {}
550 }
551
552 namespace Std.Measurement {
553 function A() : Unit {}
554 }
555
556 namespace Foo {
557 function B() : Unit {
558 A();
559 }
560 }
561 "},
562 &expect![[r#"
563 namespace namespace3 {
564 function package2_item1() : Unit {}
565 }
566
567 namespace namespace4 {
568 function package2_item3() : Unit {}
569 }
570
571 namespace namespace5 {
572 function package2_item5() : Unit {
573 A();
574 }
575 }
576
577 // AmbiguousPrelude { name: "A", candidate_a: "Std.Canon", candidate_b: "Std.Measurement", span: Span { lo: 160, hi: 161 } }
578 "#]],
579 );
580}
581
582#[test]
583fn local_var() {
584 check(
585 indoc! {"
586 namespace Foo {
587 function A() : Int {
588 let x = 0;
589 x
590 }
591 }
592 "},
593 &expect![[r#"
594 namespace namespace3 {
595 function package2_item1() : Int {
596 let local13 = 0;
597 local13
598 }
599 }
600 "#]],
601 );
602}
603
604#[test]
605fn shadow_local() {
606 check(
607 indoc! {"
608 namespace Foo {
609 function A() : Int {
610 let x = 0;
611 let y = {
612 let x = 1;
613 x
614 };
615 x + y
616 }
617 }
618 "},
619 &expect![[r#"
620 namespace namespace3 {
621 function package2_item1() : Int {
622 let local13 = 0;
623 let local17 = {
624 let local22 = 1;
625 local22
626 };
627 local13 + local17
628 }
629 }
630 "#]],
631 );
632}
633
634#[test]
635fn callable_param() {
636 check(
637 indoc! {"
638 namespace Foo {
639 function A(x : Int) : Int {
640 x
641 }
642 }
643 "},
644 &expect![[r#"
645 namespace namespace3 {
646 function package2_item1(local8 : Int) : Int {
647 local8
648 }
649 }
650 "#]],
651 );
652}
653
654#[test]
655fn spec_param() {
656 check(
657 indoc! {"
658 namespace Foo {
659 operation A(q : Qubit) : (Qubit[], Qubit) {
660 controlled (cs, ...) {
661 (cs, q)
662 }
663 }
664 }
665 "},
666 &expect![[r#"
667 namespace namespace3 {
668 operation package2_item1(local8 : Qubit) : (Qubit[], Qubit) {
669 controlled (local23, ...) {
670 (local23, local8)
671 }
672 }
673 }
674 "#]],
675 );
676}
677
678#[test]
679fn spec_param_shadow_disallowed() {
680 check(
681 indoc! {"
682 namespace Foo {
683 operation A(qs : Qubit[]) : Qubit[] {
684 controlled (qs, ...) {
685 qs
686 }
687 body ... {
688 qs
689 }
690 }
691 }
692 "},
693 &expect![[r#"
694 namespace namespace3 {
695 operation package2_item1(local8 : Qubit[]) : Qubit[] {
696 controlled (local20, ...) {
697 local20
698 }
699 body ... {
700 local8
701 }
702 }
703 }
704
705 // DuplicateBinding("qs", Span { lo: 78, hi: 80 })
706 "#]],
707 );
708}
709
710#[test]
711fn local_shadows_global() {
712 check(
713 indoc! {"
714 namespace Foo {
715 function x() : Unit {}
716
717 function y() : Int {
718 x();
719 let x = 1;
720 x
721 }
722 }
723 "},
724 &expect![[r#"
725 namespace namespace3 {
726 function package2_item1() : Unit {}
727
728 function package2_item2() : Int {
729 package2_item1();
730 let local27 = 1;
731 local27
732 }
733 }
734 "#]],
735 );
736}
737
738#[test]
739fn shadow_same_block() {
740 check(
741 indoc! {"
742 namespace Foo {
743 function A() : Int {
744 let x = 0;
745 let x = x + 1;
746 x
747 }
748 }
749 "},
750 &expect![[r#"
751 namespace namespace3 {
752 function package2_item1() : Int {
753 let local13 = 0;
754 let local17 = local13 + 1;
755 local17
756 }
757 }
758 "#]],
759 );
760}
761
762#[test]
763fn parent_namespace_shadows_open() {
764 check(
765 indoc! {"
766 namespace Foo {
767 function A() : Unit {}
768 }
769
770 namespace Bar {
771 open Foo;
772
773 function A() : Unit {}
774
775 function B() : Unit {
776 A();
777 }
778 }
779 "},
780 &expect![[r#"
781 namespace namespace3 {
782 function package2_item1() : Unit {}
783 }
784
785 namespace namespace4 {
786 open namespace3;
787
788 function package2_item3() : Unit {}
789
790 function package2_item4() : Unit {
791 package2_item3();
792 }
793 }
794 "#]],
795 );
796}
797
798#[test]
799fn open_alias_shadows_global() {
800 check(
801 indoc! {"
802 namespace Foo {
803 function A() : Unit {}
804 }
805
806 namespace Bar {
807 function A() : Unit {}
808 }
809
810 namespace Baz {
811 open Foo as Bar;
812
813 function B() : Unit {
814 Bar.A();
815 }
816 }
817 "},
818 &expect![[r#"
819 namespace namespace3 {
820 function package2_item1() : Unit {}
821 }
822
823 namespace namespace4 {
824 function package2_item3() : Unit {}
825 }
826
827 namespace namespace5 {
828 open namespace3 as Bar;
829
830 function package2_item5() : Unit {
831 package2_item1();
832 }
833 }
834 "#]],
835 );
836}
837
838#[test]
839fn shadowing_disallowed_within_parameters() {
840 check(
841 indoc! {"
842 namespace Test {
843 operation Foo(x: Int, y: Double, x: Bool) : Unit {}
844 }
845 "},
846 &expect![[r#"
847 namespace namespace3 {
848 operation package2_item1(local8: Int, local13: Double, local18: Bool) : Unit {}
849 }
850
851 // DuplicateBinding("x", Span { lo: 54, hi: 55 })
852 "#]],
853 );
854}
855
856#[test]
857fn shadowing_disallowed_within_local_binding() {
858 check(
859 indoc! {"
860 namespace Test {
861 operation Foo() : Unit {
862 let (first, second, first) = (1, 2, 3);
863 }
864 }
865 "},
866 &expect![[r#"
867 namespace namespace3 {
868 operation package2_item1() : Unit {
869 let (local14, local16, local18) = (1, 2, 3);
870 }
871 }
872
873 // DuplicateBinding("first", Span { lo: 74, hi: 79 })
874 "#]],
875 );
876}
877
878#[test]
879fn shadowing_disallowed_within_for_loop() {
880 check(
881 indoc! {"
882 namespace Test {
883 operation Foo() : Unit {
884 for (key, val, key) in [(1, 1, 1)] {}
885 }
886 }
887 "},
888 &expect![[r#"
889 namespace namespace3 {
890 operation package2_item1() : Unit {
891 for (local15, local17, local19) in [(1, 1, 1)] {}
892 }
893 }
894
895 // DuplicateBinding("key", Span { lo: 69, hi: 72 })
896 "#]],
897 );
898}
899
900#[test]
901fn shadowing_disallowed_within_lambda_param() {
902 check(
903 indoc! {"
904 namespace Test {
905 operation Foo() : Unit {
906 let f = (x, y, x) -> x + y + 1;
907 }
908 }
909 "},
910 &expect![[r#"
911 namespace namespace3 {
912 operation package2_item1() : Unit {
913 let local13 = (local17, local19, local21) -> local21 + local19 + 1;
914 }
915 }
916
917 // DuplicateBinding("x", Span { lo: 69, hi: 70 })
918 "#]],
919 );
920}
921
922#[test]
923fn merged_aliases() {
924 check(
925 indoc! {"
926 namespace Foo {
927 function A() : Unit {}
928 }
929
930 namespace Bar {
931 function B() : Unit {}
932 }
933
934 namespace Baz {
935 open Foo as Alias;
936 open Bar as Alias;
937
938 function C() : Unit {
939 Alias.A();
940 Alias.B();
941 }
942 }
943 "},
944 &expect![[r#"
945 namespace namespace3 {
946 function package2_item1() : Unit {}
947 }
948
949 namespace namespace4 {
950 function package2_item3() : Unit {}
951 }
952
953 namespace namespace5 {
954 open namespace3 as Alias;
955 open namespace4 as Alias;
956
957 function package2_item5() : Unit {
958 package2_item1();
959 package2_item3();
960 }
961 }
962 "#]],
963 );
964}
965
966#[test]
967fn ty_decl() {
968 check(
969 indoc! {"
970 namespace Foo {
971 newtype A = Unit;
972 function B(a : A) : Unit {}
973 }
974 "},
975 &expect![[r#"
976 namespace namespace3 {
977 newtype package2_item1 = Unit;
978 function package2_item2(local14 : package2_item1) : Unit {}
979 }
980 "#]],
981 );
982}
983
984#[test]
985fn struct_decl() {
986 check(
987 indoc! {"
988 namespace Foo {
989 struct A {}
990 function B(a : A) : Unit {}
991 }
992 "},
993 &expect![[r#"
994 namespace namespace3 {
995 struct package2_item1 {}
996 function package2_item2(local11 : package2_item1) : Unit {}
997 }
998 "#]],
999 );
1000}
1001
1002#[test]
1003fn ty_decl_duplicate_error() {
1004 check(
1005 indoc! {"
1006 namespace Foo {
1007 newtype A = Unit;
1008 newtype A = Bool;
1009 }
1010 "},
1011 &expect![[r#"
1012 namespace namespace3 {
1013 newtype package2_item1 = Unit;
1014 newtype package2_item2 = Bool;
1015 }
1016
1017 // Duplicate("A", "Foo", Span { lo: 50, hi: 51 })
1018 "#]],
1019 );
1020}
1021
1022#[test]
1023fn struct_decl_duplicate_error() {
1024 check(
1025 indoc! {"
1026 namespace Foo {
1027 struct A {}
1028 struct A { first : Bool }
1029 }
1030 "},
1031 &expect![[r#"
1032 namespace namespace3 {
1033 struct package2_item1 {}
1034 struct package2_item2 { first : Bool }
1035 }
1036
1037 // Duplicate("A", "Foo", Span { lo: 43, hi: 44 })
1038 "#]],
1039 );
1040}
1041
1042#[test]
1043fn ty_decl_duplicate_error_on_built_in_ty() {
1044 check(
1045 indoc! {"
1046 namespace Std.Core {
1047 newtype Pauli = Unit;
1048 }
1049 "},
1050 &expect![[r#"
1051 namespace namespace2 {
1052 newtype package2_item1 = Unit;
1053 }
1054
1055 // Duplicate("Pauli", "Std.Core", Span { lo: 33, hi: 38 })
1056 "#]],
1057 );
1058}
1059
1060#[test]
1061fn struct_decl_duplicate_error_on_built_in_ty() {
1062 check(
1063 indoc! {"
1064 namespace Std.Core {
1065 struct Pauli {}
1066 }
1067 "},
1068 &expect![[r#"
1069 namespace namespace2 {
1070 struct package2_item1 {}
1071 }
1072
1073 // Duplicate("Pauli", "Std.Core", Span { lo: 32, hi: 37 })
1074 "#]],
1075 );
1076}
1077
1078#[test]
1079fn ty_decl_in_ty_decl() {
1080 check(
1081 indoc! {"
1082 namespace Foo {
1083 newtype A = Unit;
1084 newtype B = A;
1085 }
1086 "},
1087 &expect![[r#"
1088 namespace namespace3 {
1089 newtype package2_item1 = Unit;
1090 newtype package2_item2 = package2_item1;
1091 }
1092 "#]],
1093 );
1094}
1095
1096#[test]
1097fn struct_decl_in_struct_decl() {
1098 check(
1099 indoc! {"
1100 namespace Foo {
1101 struct A {}
1102 struct B { a : A }
1103 }
1104 "},
1105 &expect![[r#"
1106 namespace namespace3 {
1107 struct package2_item1 {}
1108 struct package2_item2 { a : package2_item1 }
1109 }
1110 "#]],
1111 );
1112}
1113
1114#[test]
1115fn ty_decl_recursive() {
1116 check(
1117 indoc! {"
1118 namespace Foo {
1119 newtype A = A;
1120 }
1121 "},
1122 &expect![[r#"
1123 namespace namespace3 {
1124 newtype package2_item1 = package2_item1;
1125 }
1126 "#]],
1127 );
1128}
1129
1130#[test]
1131fn struct_decl_recursive() {
1132 check(
1133 indoc! {"
1134 namespace Foo {
1135 struct A { a : A }
1136 }
1137 "},
1138 &expect![[r#"
1139 namespace namespace3 {
1140 struct package2_item1 { a : package2_item1 }
1141 }
1142 "#]],
1143 );
1144}
1145
1146#[test]
1147fn ty_decl_cons() {
1148 check(
1149 indoc! {"
1150 namespace Foo {
1151 newtype A = Unit;
1152
1153 function B() : A {
1154 A()
1155 }
1156 }
1157 "},
1158 &expect![[r#"
1159 namespace namespace3 {
1160 newtype package2_item1 = Unit;
1161
1162 function package2_item2() : package2_item1 {
1163 package2_item1()
1164 }
1165 }
1166 "#]],
1167 );
1168}
1169
1170#[test]
1171fn struct_decl_call_cons() {
1172 check(
1173 indoc! {"
1174 namespace Foo {
1175 struct A {}
1176
1177 function B() : A {
1178 A()
1179 }
1180 }
1181 "},
1182 &expect![[r#"
1183 namespace namespace3 {
1184 struct package2_item1 {}
1185
1186 function package2_item2() : package2_item1 {
1187 package2_item1()
1188 }
1189 }
1190 "#]],
1191 );
1192}
1193
1194#[test]
1195fn struct_decl_cons() {
1196 check(
1197 indoc! {"
1198 namespace Foo {
1199 struct A {}
1200
1201 function B() : A {
1202 new A {}
1203 }
1204 }
1205 "},
1206 &expect![[r#"
1207 namespace namespace3 {
1208 struct package2_item1 {}
1209
1210 function package2_item2() : package2_item1 {
1211 new package2_item1 {}
1212 }
1213 }
1214 "#]],
1215 );
1216}
1217
1218#[test]
1219fn struct_decl_cons_with_fields() {
1220 check(
1221 indoc! {"
1222 namespace Foo {
1223 struct A {}
1224 struct B {}
1225 struct C { a : A, b : B }
1226
1227 function D() : C {
1228 new C { a = new A {}, b = new B {} }
1229 }
1230 }
1231 "},
1232 &expect![[r#"
1233 namespace namespace3 {
1234 struct package2_item1 {}
1235 struct package2_item2 {}
1236 struct package2_item3 { a : package2_item1, b : package2_item2 }
1237
1238 function package2_item4() : package2_item3 {
1239 new package2_item3 { a = new package2_item1 {}, b = new package2_item2 {} }
1240 }
1241 }
1242 "#]],
1243 );
1244}
1245
1246#[test]
1247fn struct_field_accessor() {
1248 check(
1249 indoc! {"
1250 namespace Foo {
1251 struct A { b : B }
1252 struct B { c : C}
1253 struct C { i : Int }
1254
1255 function D() : Unit {
1256 let a = new A { b = new B { c = new C { i = 4 } } };
1257 let i = a.b.c.i;
1258 }
1259 }
1260 "},
1261 &expect![[r#"
1262 namespace namespace3 {
1263 struct package2_item1 { b : package2_item2 }
1264 struct package2_item2 { c : package2_item3}
1265 struct package2_item3 { i : Int }
1266
1267 function package2_item4() : Unit {
1268 let local37 = new package2_item1 { b = new package2_item2 { c = new package2_item3 { i = 4 } } };
1269 let local56 = local37.b.c.i;
1270 }
1271 }
1272 "#]],
1273 );
1274}
1275
1276#[test]
1277fn struct_field_accessor_with_expr() {
1278 check(
1279 indoc! {"
1280 namespace Foo {
1281 struct A { b : B }
1282 struct B { c : C}
1283 struct C { i : Int }
1284
1285 function D() : Unit {
1286 let a = new A { b = new B { c = new C { i = 4 } } };
1287 let i = { a }.b.c.i;
1288 }
1289 }
1290 "},
1291 &expect![[r#"
1292 namespace namespace3 {
1293 struct package2_item1 { b : package2_item2 }
1294 struct package2_item2 { c : package2_item3}
1295 struct package2_item3 { i : Int }
1296
1297 function package2_item4() : Unit {
1298 let local37 = new package2_item1 { b = new package2_item2 { c = new package2_item3 { i = 4 } } };
1299 let local56 = { local37 }.b.c.i;
1300 }
1301 }
1302 "#]],
1303 );
1304}
1305
1306#[test]
1307fn unknown_term() {
1308 check(
1309 indoc! {"
1310 namespace Foo {
1311 function A() : Unit {
1312 B();
1313 }
1314 }
1315 "},
1316 &expect![[r#"
1317 namespace namespace3 {
1318 function package2_item1() : Unit {
1319 B();
1320 }
1321 }
1322
1323 // NotFound("B", Span { lo: 50, hi: 51 })
1324 "#]],
1325 );
1326}
1327
1328#[test]
1329fn unknown_ty() {
1330 check(
1331 indoc! {"
1332 namespace Foo {
1333 function A(b : B) : Unit {}
1334 }
1335 "},
1336 &expect![[r#"
1337 namespace namespace3 {
1338 function package2_item1(local8 : B) : Unit {}
1339 }
1340
1341 // NotFound("B", Span { lo: 35, hi: 36 })
1342 "#]],
1343 );
1344}
1345
1346#[test]
1347fn open_ambiguous_terms() {
1348 check(
1349 indoc! {"
1350 namespace Foo {
1351 function A() : Unit {}
1352 }
1353
1354 namespace Bar {
1355 function A() : Unit {}
1356 }
1357
1358 namespace Baz {
1359 open Foo;
1360 open Bar;
1361
1362 function C() : Unit {
1363 A();
1364 }
1365 }
1366 "},
1367 &expect![[r#"
1368 namespace namespace3 {
1369 function package2_item1() : Unit {}
1370 }
1371
1372 namespace namespace4 {
1373 function package2_item3() : Unit {}
1374 }
1375
1376 namespace namespace5 {
1377 open namespace3;
1378 open namespace4;
1379
1380 function package2_item5() : Unit {
1381 A();
1382 }
1383 }
1384
1385 // Ambiguous { name: "A", first_open: "Foo", second_open: "Bar", name_span: Span { lo: 171, hi: 172 }, first_open_span: Span { lo: 117, hi: 120 }, second_open_span: Span { lo: 131, hi: 134 } }
1386 "#]],
1387 );
1388}
1389
1390#[test]
1391fn open_ambiguous_tys() {
1392 check(
1393 indoc! {"
1394 namespace Foo {
1395 newtype A = Unit;
1396 }
1397
1398 namespace Bar {
1399 newtype A = Unit;
1400 }
1401
1402 namespace Baz {
1403 open Foo;
1404 open Bar;
1405
1406 function C(a : A) : Unit {}
1407 }
1408 "},
1409 &expect![[r#"
1410 namespace namespace3 {
1411 newtype package2_item1 = Unit;
1412 }
1413
1414 namespace namespace4 {
1415 newtype package2_item3 = Unit;
1416 }
1417
1418 namespace namespace5 {
1419 open namespace3;
1420 open namespace4;
1421
1422 function package2_item5(local30 : A) : Unit {}
1423 }
1424
1425 // Ambiguous { name: "A", first_open: "Foo", second_open: "Bar", name_span: Span { lo: 146, hi: 147 }, first_open_span: Span { lo: 107, hi: 110 }, second_open_span: Span { lo: 121, hi: 124 } }
1426 "#]],
1427 );
1428}
1429
1430#[test]
1431fn merged_aliases_ambiguous_terms() {
1432 check(
1433 indoc! {"
1434 namespace Foo {
1435 function A() : Unit {}
1436 }
1437
1438 namespace Bar {
1439 function A() : Unit {}
1440 }
1441
1442 namespace Baz {
1443 open Foo as Alias;
1444 open Bar as Alias;
1445
1446 function C() : Unit {
1447 Alias.A();
1448 }
1449 }
1450 "},
1451 &expect![[r#"
1452 namespace namespace3 {
1453 function package2_item1() : Unit {}
1454 }
1455
1456 namespace namespace4 {
1457 function package2_item3() : Unit {}
1458 }
1459
1460 namespace namespace5 {
1461 open namespace3 as Alias;
1462 open namespace4 as Alias;
1463
1464 function package2_item5() : Unit {
1465 namespace4.A();
1466 }
1467 }
1468
1469 // Ambiguous { name: "A", first_open: "Foo", second_open: "Bar", name_span: Span { lo: 195, hi: 196 }, first_open_span: Span { lo: 117, hi: 120 }, second_open_span: Span { lo: 140, hi: 143 } }
1470 "#]],
1471 );
1472}
1473
1474#[test]
1475fn merged_aliases_ambiguous_tys() {
1476 check(
1477 indoc! {"
1478 namespace Foo {
1479 newtype A = Unit;
1480 }
1481
1482 namespace Bar {
1483 newtype A = Unit;
1484 }
1485
1486 namespace Baz {
1487 open Foo as Alias;
1488 open Bar as Alias;
1489
1490 function C(a : Alias.A) : Unit {}
1491 }
1492 "},
1493 &expect![[r#"
1494 namespace namespace3 {
1495 newtype package2_item1 = Unit;
1496 }
1497
1498 namespace namespace4 {
1499 newtype package2_item3 = Unit;
1500 }
1501
1502 namespace namespace5 {
1503 open namespace3 as Alias;
1504 open namespace4 as Alias;
1505
1506 function package2_item5(local32 : namespace4.A) : Unit {}
1507 }
1508
1509 // Ambiguous { name: "A", first_open: "Foo", second_open: "Bar", name_span: Span { lo: 170, hi: 171 }, first_open_span: Span { lo: 107, hi: 110 }, second_open_span: Span { lo: 130, hi: 133 } }
1510 "#]],
1511 );
1512}
1513
1514#[test]
1515fn lambda_param() {
1516 check(
1517 indoc! {"
1518 namespace Foo {
1519 function A() : Unit {
1520 let f = x -> x + 1;
1521 }
1522 }
1523 "},
1524 &expect![[r#"
1525 namespace namespace3 {
1526 function package2_item1() : Unit {
1527 let local13 = local16 -> local16 + 1;
1528 }
1529 }
1530 "#]],
1531 );
1532}
1533
1534#[test]
1535fn lambda_shadows_local() {
1536 check(
1537 indoc! {"
1538 namespace Foo {
1539 function A() : Int {
1540 let x = 1;
1541 let f = x -> x + 1;
1542 x
1543 }
1544 }
1545 "},
1546 &expect![[r#"
1547 namespace namespace3 {
1548 function package2_item1() : Int {
1549 let local13 = 1;
1550 let local17 = local20 -> local20 + 1;
1551 local13
1552 }
1553 }
1554 "#]],
1555 );
1556}
1557
1558#[test]
1559fn for_loop_range() {
1560 check(
1561 indoc! {"
1562 namespace Foo {
1563 function A() : Unit {
1564 for i in 0..9 {
1565 let _ = i;
1566 }
1567 }
1568 }
1569 "},
1570 &expect![[r#"
1571 namespace namespace3 {
1572 function package2_item1() : Unit {
1573 for local14 in 0..9 {
1574 let _ = local14;
1575 }
1576 }
1577 }
1578 "#]],
1579 );
1580}
1581
1582#[test]
1583fn for_loop_var() {
1584 check(
1585 indoc! {"
1586 namespace Foo {
1587 function A(xs : Int[]) : Unit {
1588 for x in xs {
1589 let _ = x;
1590 }
1591 }
1592 }
1593 "},
1594 &expect![[r#"
1595 namespace namespace3 {
1596 function package2_item1(local8 : Int[]) : Unit {
1597 for local20 in local8 {
1598 let _ = local20;
1599 }
1600 }
1601 }
1602 "#]],
1603 );
1604}
1605
1606#[test]
1607fn for_loop_explicit_type() {
1608 check(
1609 indoc! {"
1610 namespace Foo {
1611 function A() : Unit {
1612 for i : Int in 0..9 {
1613 let _ = i;
1614 }
1615 }
1616 }
1617 "},
1618 &expect![[r#"
1619 namespace namespace3 {
1620 function package2_item1() : Unit {
1621 for local14 : Int in 0..9 {
1622 let _ = local14;
1623 }
1624 }
1625 }
1626 "#]],
1627 );
1628}
1629
1630#[test]
1631fn repeat_until() {
1632 check(
1633 indoc! {"
1634 namespace Foo {
1635 operation A() : Unit {
1636 mutable cond = false;
1637 repeat {
1638 set cond = true;
1639 } until cond;
1640 }
1641 }
1642 "},
1643 &expect![[r#"
1644 namespace namespace3 {
1645 operation package2_item1() : Unit {
1646 mutable local13 = false;
1647 repeat {
1648 set local13 = true;
1649 } until local13;
1650 }
1651 }
1652 "#]],
1653 );
1654}
1655
1656#[test]
1657fn repeat_until_fixup() {
1658 check(
1659 indoc! {"
1660 namespace Foo {
1661 operation A() : Unit {
1662 mutable cond = false;
1663 repeat {
1664 set cond = false;
1665 } until cond
1666 fixup {
1667 set cond = true;
1668 }
1669 }
1670 }
1671 "},
1672 &expect![[r#"
1673 namespace namespace3 {
1674 operation package2_item1() : Unit {
1675 mutable local13 = false;
1676 repeat {
1677 set local13 = false;
1678 } until local13
1679 fixup {
1680 set local13 = true;
1681 }
1682 }
1683 }
1684 "#]],
1685 );
1686}
1687
1688#[test]
1689fn repeat_until_fixup_scoping() {
1690 check(
1691 indoc! {"
1692 namespace Foo {
1693 operation A() : Unit {
1694 repeat {
1695 mutable cond = false;
1696 }
1697 until cond
1698 fixup {
1699 set cond = true;
1700 }
1701 }
1702 }"},
1703 &expect![[r#"
1704 namespace namespace3 {
1705 operation package2_item1() : Unit {
1706 repeat {
1707 mutable local16 = false;
1708 }
1709 until cond
1710 fixup {
1711 set cond = true;
1712 }
1713 }
1714 }
1715 // NotFound("cond", Span { lo: 118, hi: 122 })
1716 // NotFound("cond", Span { lo: 155, hi: 159 })
1717 "#]],
1718 );
1719}
1720
1721#[test]
1722fn use_qubit() {
1723 check(
1724 indoc! {"
1725 namespace Foo {
1726 operation X(q : Qubit) : Unit {
1727 body intrinsic;
1728 }
1729 operation A() : Unit {
1730 use q = Qubit();
1731 X(q);
1732 }
1733 }
1734 "},
1735 &expect![[r#"
1736 namespace namespace3 {
1737 operation package2_item1(local8 : Qubit) : Unit {
1738 body intrinsic;
1739 }
1740 operation package2_item2() : Unit {
1741 use local26 = Qubit();
1742 package2_item1(local26);
1743 }
1744 }
1745 "#]],
1746 );
1747}
1748
1749#[test]
1750fn use_qubit_block() {
1751 check(
1752 indoc! {"
1753 namespace Foo {
1754 operation X(q : Qubit) : Unit {
1755 body intrinsic;
1756 }
1757 operation A() : Unit {
1758 use q = Qubit() {
1759 X(q);
1760 }
1761 }
1762 }
1763 "},
1764 &expect![[r#"
1765 namespace namespace3 {
1766 operation package2_item1(local8 : Qubit) : Unit {
1767 body intrinsic;
1768 }
1769 operation package2_item2() : Unit {
1770 use local26 = Qubit() {
1771 package2_item1(local26);
1772 }
1773 }
1774 }
1775 "#]],
1776 );
1777}
1778
1779#[test]
1780fn use_qubit_block_qubit_restricted_to_block_scope() {
1781 check(
1782 indoc! {"
1783 namespace Foo {
1784 operation X(q : Qubit) : Unit {
1785 body intrinsic;
1786 }
1787 operation A() : Unit {
1788 use q = Qubit() {
1789 X(q);
1790 }
1791 X(q);
1792 }
1793 }
1794 "},
1795 &expect![[r#"
1796 namespace namespace3 {
1797 operation package2_item1(local8 : Qubit) : Unit {
1798 body intrinsic;
1799 }
1800 operation package2_item2() : Unit {
1801 use local26 = Qubit() {
1802 package2_item1(local26);
1803 }
1804 package2_item1(q);
1805 }
1806 }
1807
1808 // NotFound("q", Span { lo: 173, hi: 174 })
1809 "#]],
1810 );
1811}
1812
1813#[test]
1814fn local_function() {
1815 check(
1816 indoc! {"
1817 namespace A {
1818 function Foo() : Int {
1819 function Bar() : Int { 2 }
1820 Bar() + 1
1821 }
1822 }
1823 "},
1824 &expect![[r#"
1825 namespace namespace3 {
1826 function package2_item1() : Int {
1827 function package2_item2() : Int { 2 }
1828 package2_item2() + 1
1829 }
1830 }
1831 "#]],
1832 );
1833}
1834
1835#[test]
1836fn local_function_use_before_declare() {
1837 check(
1838 indoc! {"
1839 namespace A {
1840 function Foo() : () {
1841 Bar();
1842 function Bar() : () {}
1843 }
1844 }
1845 "},
1846 &expect![[r#"
1847 namespace namespace3 {
1848 function package2_item1() : () {
1849 package2_item2();
1850 function package2_item2() : () {}
1851 }
1852 }
1853 "#]],
1854 );
1855}
1856
1857#[test]
1858fn local_function_is_really_local() {
1859 check(
1860 indoc! {"
1861 namespace A {
1862 function Foo() : () {
1863 function Bar() : () {}
1864 Bar();
1865 }
1866
1867 function Baz() : () { Bar(); }
1868 }
1869 "},
1870 &expect![[r#"
1871 namespace namespace3 {
1872 function package2_item1() : () {
1873 function package2_item3() : () {}
1874 package2_item3();
1875 }
1876
1877 function package2_item2() : () { Bar(); }
1878 }
1879
1880 // NotFound("Bar", Span { lo: 119, hi: 122 })
1881 "#]],
1882 );
1883}
1884
1885#[test]
1886fn local_function_is_not_closure() {
1887 check(
1888 indoc! {"
1889 namespace A {
1890 function Foo() : () {
1891 let x = 2;
1892 function Bar() : Int { x }
1893 }
1894 }
1895 "},
1896 &expect![[r#"
1897 namespace namespace3 {
1898 function package2_item1() : () {
1899 let local11 = 2;
1900 function package2_item2() : Int { x }
1901 }
1902 }
1903
1904 // NotFound("x", Span { lo: 90, hi: 91 })
1905 "#]],
1906 );
1907}
1908
1909#[test]
1910fn local_type() {
1911 check(
1912 indoc! {"
1913 namespace A {
1914 function Foo() : () {
1915 newtype Bar = Int;
1916 let x = Bar(5);
1917 }
1918 }
1919 "},
1920 &expect![[r#"
1921 namespace namespace3 {
1922 function package2_item1() : () {
1923 newtype package2_item2 = Int;
1924 let local18 = package2_item2(5);
1925 }
1926 }
1927 "#]],
1928 );
1929}
1930
1931#[test]
1932fn local_open() {
1933 check(
1934 indoc! {"
1935 namespace A { function Foo() : () { open B; Bar(); } }
1936 namespace B { function Bar() : () {} }
1937 "},
1938 &expect![[r#"
1939 namespace namespace3 { function package2_item1() : () { open namespace4; package2_item3(); } }
1940 namespace namespace4 { function package2_item3() : () {} }
1941 "#]],
1942 );
1943}
1944
1945#[test]
1946fn local_open_shadows_parent_item() {
1947 check(
1948 indoc! {"
1949 namespace A {
1950 function Bar() : () {}
1951 function Foo() : () { open B; Bar(); }
1952 }
1953
1954 namespace B { function Bar() : () {} }
1955 "},
1956 &expect![[r#"
1957 namespace namespace3 {
1958 function package2_item1() : () {}
1959 function package2_item2() : () { open namespace4; package2_item4(); }
1960 }
1961
1962 namespace namespace4 { function package2_item4() : () {} }
1963 "#]],
1964 );
1965}
1966
1967#[test]
1968fn local_open_shadows_parent_open() {
1969 check(
1970 indoc! {"
1971 namespace A {
1972 open B;
1973 function Foo() : () { open C; Bar(); }
1974 }
1975
1976 namespace B { function Bar() : () {} }
1977 namespace C { function Bar() : () {} }
1978 "},
1979 &expect![[r#"
1980 namespace namespace3 {
1981 open namespace4;
1982 function package2_item1() : () { open namespace5; package2_item5(); }
1983 }
1984
1985 namespace namespace4 { function package2_item3() : () {} }
1986 namespace namespace5 { function package2_item5() : () {} }
1987 "#]],
1988 );
1989}
1990
1991#[test]
1992fn update_array_index_var() {
1993 check(
1994 indoc! {"
1995 namespace A {
1996 function Foo() : () {
1997 let xs = [2];
1998 let i = 0;
1999 let ys = xs w/ i <- 3;
2000 }
2001 }
2002 "},
2003 &expect![[r#"
2004 namespace namespace3 {
2005 function package2_item1() : () {
2006 let local11 = [2];
2007 let local16 = 0;
2008 let local20 = local11 w/ local16 <- 3;
2009 }
2010 }
2011 "#]],
2012 );
2013}
2014
2015#[test]
2016fn update_array_index_expr() {
2017 check(
2018 indoc! {"
2019 namespace A {
2020 function Foo() : () {
2021 let xs = [2];
2022 let i = 0;
2023 let ys = xs w/ i + 1 <- 3;
2024 }
2025 }
2026 "},
2027 &expect![[r#"
2028 namespace namespace3 {
2029 function package2_item1() : () {
2030 let local11 = [2];
2031 let local16 = 0;
2032 let local20 = local11 w/ local16 + 1 <- 3;
2033 }
2034 }
2035 "#]],
2036 );
2037}
2038
2039#[test]
2040fn update_udt_known_field_name() {
2041 check(
2042 indoc! {"
2043 namespace A {
2044 newtype Pair = (First : Int, Second : Int);
2045
2046 function Foo() : () {
2047 let p = Pair(1, 2);
2048 let q = p w/ First <- 3;
2049 }
2050 }
2051 "},
2052 &expect![[r#"
2053 namespace namespace3 {
2054 newtype package2_item1 = (First : Int, Second : Int);
2055
2056 function package2_item2() : () {
2057 let local24 = package2_item1(1, 2);
2058 let local34 = local24 w/ First <- 3;
2059 }
2060 }
2061 "#]],
2062 );
2063}
2064
2065#[test]
2066fn update_udt_known_field_name_expr() {
2067 check(
2068 indoc! {"
2069 namespace A {
2070 newtype Pair = (First : Int, Second : Int);
2071
2072 function Foo() : () {
2073 let p = Pair(1, 2);
2074 let q = p w/ First + 1 <- 3;
2075 }
2076 }
2077 "},
2078 &expect![[r#"
2079 namespace namespace3 {
2080 newtype package2_item1 = (First : Int, Second : Int);
2081
2082 function package2_item2() : () {
2083 let local24 = package2_item1(1, 2);
2084 let local34 = local24 w/ First + 1 <- 3;
2085 }
2086 }
2087
2088 // NotFound("First", Span { lo: 138, hi: 143 })
2089 "#]],
2090 );
2091}
2092
2093#[test]
2094fn update_udt_unknown_field_name() {
2095 check(
2096 indoc! {"
2097 namespace A {
2098 newtype Pair = (First : Int, Second : Int);
2099
2100 function Foo() : () {
2101 let p = Pair(1, 2);
2102 let q = p w/ Third <- 3;
2103 }
2104 }
2105 "},
2106 &expect![[r#"
2107 namespace namespace3 {
2108 newtype package2_item1 = (First : Int, Second : Int);
2109
2110 function package2_item2() : () {
2111 let local24 = package2_item1(1, 2);
2112 let local34 = local24 w/ Third <- 3;
2113 }
2114 }
2115 "#]],
2116 );
2117}
2118
2119#[test]
2120fn update_udt_unknown_field_name_known_global() {
2121 check(
2122 indoc! {"
2123 namespace A {
2124 newtype Pair = (First : Int, Second : Int);
2125
2126 function Third() : () {}
2127
2128 function Foo() : () {
2129 let p = Pair(1, 2);
2130 let q = p w/ Third <- 3;
2131 }
2132 }
2133 "},
2134 &expect![[r#"
2135 namespace namespace3 {
2136 newtype package2_item1 = (First : Int, Second : Int);
2137
2138 function package2_item2() : () {}
2139
2140 function package2_item3() : () {
2141 let local30 = package2_item1(1, 2);
2142 let local40 = local30 w/ Third <- 3;
2143 }
2144 }
2145 "#]],
2146 );
2147}
2148
2149#[test]
2150fn unknown_namespace() {
2151 check(
2152 indoc! {"
2153 namespace A {
2154 import Std.Fake.*;
2155 }
2156 "},
2157 &expect![[r#"
2158 namespace namespace3 {
2159 import Std.Fake.*;
2160 }
2161
2162 // NotFound("Std.Fake", Span { lo: 25, hi: 33 })
2163 "#]],
2164 );
2165}
2166
2167#[test]
2168fn empty_namespace_works() {
2169 check(
2170 indoc! {"
2171 namespace A {
2172 open B;
2173 function foo(): Unit{}
2174 }
2175 namespace B {}
2176 "},
2177 &expect![[r#"
2178 namespace namespace3 {
2179 open namespace4;
2180 function package2_item1(): Unit{}
2181 }
2182 namespace namespace4 {}
2183 "#]],
2184 );
2185}
2186
2187#[test]
2188fn cyclic_namespace_dependency_supported() {
2189 check(
2190 indoc! {"
2191 namespace A {
2192 open B;
2193 operation Foo() : Unit {
2194 Bar();
2195 }
2196 }
2197 namespace B {
2198 open A;
2199 operation Bar() : Unit {
2200 Foo();
2201 }
2202 }
2203 "},
2204 &expect![[r#"
2205 namespace namespace3 {
2206 open namespace4;
2207 operation package2_item1() : Unit {
2208 package2_item3();
2209 }
2210 }
2211 namespace namespace4 {
2212 open namespace3;
2213 operation package2_item3() : Unit {
2214 package2_item1();
2215 }
2216 }
2217 "#]],
2218 );
2219}
2220
2221#[test]
2222fn bind_items_in_repeat() {
2223 check(
2224 indoc! {"
2225 namespace A {
2226 operation B() : Unit {
2227 repeat {
2228 function C() : Unit {}
2229 } until false
2230 fixup {
2231 function D() : Unit {}
2232 }
2233 }
2234 }
2235 "},
2236 &expect![[r#"
2237 namespace namespace3 {
2238 operation package2_item1() : Unit {
2239 repeat {
2240 function package2_item2() : Unit {}
2241 } until false
2242 fixup {
2243 function package2_item3() : Unit {}
2244 }
2245 }
2246 }
2247 "#]],
2248 );
2249}
2250
2251#[test]
2252fn bind_items_in_qubit_use_block() {
2253 check(
2254 indoc! {"
2255 namespace A {
2256 operation B() : Unit {
2257 use q = Qubit() {
2258 function C() : Unit {}
2259 }
2260 }
2261 }
2262 "},
2263 &expect![[r#"
2264 namespace namespace3 {
2265 operation package2_item1() : Unit {
2266 use local13 = Qubit() {
2267 function package2_item2() : Unit {}
2268 }
2269 }
2270 }
2271 "#]],
2272 );
2273}
2274
2275#[test]
2276fn use_bound_item_in_another_bound_item() {
2277 check(
2278 indoc! {"
2279 namespace A {
2280 function B() : Unit {
2281 function C() : Unit {
2282 D();
2283 }
2284 function D() : Unit {}
2285 }
2286 }
2287 "},
2288 &expect![[r#"
2289 namespace namespace3 {
2290 function package2_item1() : Unit {
2291 function package2_item2() : Unit {
2292 package2_item3();
2293 }
2294 function package2_item3() : Unit {}
2295 }
2296 }
2297 "#]],
2298 );
2299}
2300
2301#[test]
2302fn use_unbound_generic() {
2303 check(
2304 indoc! {"
2305 namespace A {
2306 function B<'T>(x: 'U) : 'U {
2307 x
2308 }
2309 }
2310 "},
2311 &expect![[r#"
2312 namespace namespace3 {
2313 function package2_item1<param0>(local9: 'U) : 'U {
2314 local9
2315 }
2316 }
2317
2318 // NotFound("'U", Span { lo: 36, hi: 38 })
2319 // NotFound("'U", Span { lo: 42, hi: 44 })
2320 "#]],
2321 );
2322}
2323
2324#[test]
2325fn resolve_local_generic() {
2326 check(
2327 indoc! {"
2328 namespace A {
2329 function B<'T>(x: 'T) : 'T {
2330 x
2331 }
2332 }
2333 "},
2334 &expect![[r#"
2335 namespace namespace3 {
2336 function package2_item1<param0>(local9: param0) : param0 {
2337 local9
2338 }
2339 }
2340 "#]],
2341 );
2342}
2343
2344#[test]
2345fn incorrect_single_qubit_allocation_syntax_gets_extra_help() {
2346 check(
2347 indoc! {"
2348 namespace A {
2349 operation B() : Unit {
2350 let q = Qubit();
2351 }
2352 }
2353 "},
2354 &expect![[r#"
2355 namespace namespace3 {
2356 operation package2_item1() : Unit {
2357 let local13 = Qubit();
2358 }
2359 }
2360
2361 // NotFoundQubit(Span { lo: 57, hi: 62 })
2362 "#]],
2363 );
2364}
2365
2366#[test]
2367fn incorrect_qubit_array_allocation_syntax_gets_extra_help() {
2368 check(
2369 indoc! {"
2370 namespace A {
2371 operation B() : Unit {
2372 let q = Qubit[5];
2373 }
2374 }
2375 "},
2376 &expect![[r#"
2377 namespace namespace3 {
2378 operation package2_item1() : Unit {
2379 let local13 = Qubit[5];
2380 }
2381 }
2382
2383 // NotFoundQubit(Span { lo: 57, hi: 62 })
2384 "#]],
2385 );
2386}
2387
2388#[test]
2389fn dropped_base_callable_from_unrestricted() {
2390 check_with_capabilities(
2391 indoc! {"
2392 namespace A {
2393 @Config(Base)
2394 function Dropped() : Unit {}
2395
2396 function B() : Unit {
2397 Dropped();
2398 }
2399 }
2400 "},
2401 TargetCapabilityFlags::all(),
2402 &expect![[r#"
2403 namespace namespace3 {
2404 @Config(Base)
2405 function Dropped() : Unit {}
2406
2407 function package2_item1() : Unit {
2408 Dropped();
2409 }
2410 }
2411
2412 // NotAvailable("Dropped", "A.Dropped", Span { lo: 100, hi: 107 })
2413 "#]],
2414 );
2415}
2416
2417#[test]
2418fn dropped_base_callable_from_adaptive() {
2419 check_with_capabilities(
2420 indoc! {"
2421 namespace A {
2422 @Config(Base)
2423 function Dropped() : Unit {}
2424
2425 function B() : Unit {
2426 Dropped();
2427 }
2428 }
2429 "},
2430 TargetCapabilityFlags::Adaptive,
2431 &expect![[r#"
2432 namespace namespace3 {
2433 @Config(Base)
2434 function Dropped() : Unit {}
2435
2436 function package2_item1() : Unit {
2437 Dropped();
2438 }
2439 }
2440
2441 // NotAvailable("Dropped", "A.Dropped", Span { lo: 100, hi: 107 })
2442 "#]],
2443 );
2444}
2445
2446#[test]
2447fn dropped_not_base_callable_from_base() {
2448 check_with_capabilities(
2449 indoc! {"
2450 namespace A {
2451 @Config(not Base)
2452 function Dropped() : Unit {}
2453
2454 function B() : Unit {
2455 Dropped();
2456 }
2457 }
2458 "},
2459 TargetCapabilityFlags::empty(),
2460 &expect![[r#"
2461 namespace namespace3 {
2462 @Config(not Base)
2463 function Dropped() : Unit {}
2464
2465 function package2_item1() : Unit {
2466 Dropped();
2467 }
2468 }
2469
2470 // NotAvailable("Dropped", "A.Dropped", Span { lo: 104, hi: 111 })
2471 "#]],
2472 );
2473}
2474
2475#[test]
2476fn resolved_not_base_callable_from_adaptive() {
2477 check_with_capabilities(
2478 indoc! {"
2479 namespace A {
2480 @Config(not Base)
2481 function Dropped() : Unit {}
2482
2483 function B() : Unit {
2484 Dropped();
2485 }
2486 }
2487 "},
2488 TargetCapabilityFlags::Adaptive,
2489 &expect![[r#"
2490 namespace namespace3 {
2491 @Config(not Base)
2492 function package2_item1() : Unit {}
2493
2494 function package2_item2() : Unit {
2495 package2_item1();
2496 }
2497 }
2498 "#]],
2499 );
2500}
2501
2502#[test]
2503fn dropped_base_and_not_base_callable_from_base() {
2504 check_with_capabilities(
2505 indoc! {"
2506 namespace A {
2507 @Config(Base)
2508 @Config(not Base)
2509 function Dropped() : Unit {}
2510
2511 function B() : Unit {
2512 Dropped();
2513 }
2514 }
2515 "},
2516 TargetCapabilityFlags::empty(),
2517 &expect![[r#"
2518 namespace namespace3 {
2519 @Config(Base)
2520 @Config(not Base)
2521 function Dropped() : Unit {}
2522
2523 function package2_item1() : Unit {
2524 Dropped();
2525 }
2526 }
2527
2528 // NotAvailable("Dropped", "A.Dropped", Span { lo: 122, hi: 129 })
2529 "#]],
2530 );
2531}
2532
2533#[test]
2534fn resolved_not_unrestricted_callable_from_base() {
2535 check_with_capabilities(
2536 indoc! {"
2537 namespace A {
2538 @Config(not Unrestricted)
2539 function Dropped() : Unit {}
2540
2541 function B() : Unit {
2542 Dropped();
2543 }
2544 }
2545 "},
2546 TargetCapabilityFlags::empty(),
2547 &expect![[r#"
2548 namespace namespace3 {
2549 @Config(not Unrestricted)
2550 function package2_item1() : Unit {}
2551
2552 function package2_item2() : Unit {
2553 package2_item1();
2554 }
2555 }
2556 "#]],
2557 );
2558}
2559
2560#[test]
2561fn resolved_not_unrestricted_callable_from_adaptive() {
2562 check_with_capabilities(
2563 indoc! {"
2564 namespace A {
2565 @Config(not Unrestricted)
2566 function Dropped() : Unit {}
2567
2568 function B() : Unit {
2569 Dropped();
2570 }
2571 }
2572 "},
2573 TargetCapabilityFlags::Adaptive,
2574 &expect![[r#"
2575 namespace namespace3 {
2576 @Config(not Unrestricted)
2577 function package2_item1() : Unit {}
2578
2579 function package2_item2() : Unit {
2580 package2_item1();
2581 }
2582 }
2583 "#]],
2584 );
2585}
2586
2587#[test]
2588fn dropped_not_unrestricted_callable_from_unrestricted() {
2589 check_with_capabilities(
2590 indoc! {"
2591 namespace A {
2592 @Config(not Unrestricted)
2593 function Dropped() : Unit {}
2594
2595 function B() : Unit {
2596 Dropped();
2597 }
2598 }
2599 "},
2600 TargetCapabilityFlags::all(),
2601 &expect![[r#"
2602 namespace namespace3 {
2603 @Config(not Unrestricted)
2604 function Dropped() : Unit {}
2605
2606 function package2_item1() : Unit {
2607 Dropped();
2608 }
2609 }
2610
2611 // NotAvailable("Dropped", "A.Dropped", Span { lo: 112, hi: 119 })
2612 "#]],
2613 );
2614}
2615
2616#[test]
2617fn resolved_adaptive_callable_from_adaptive() {
2618 check_with_capabilities(
2619 indoc! {"
2620 namespace A {
2621 @Config(Adaptive)
2622 function Dropped() : Unit {}
2623
2624 function B() : Unit {
2625 Dropped();
2626 }
2627 }
2628 "},
2629 TargetCapabilityFlags::Adaptive,
2630 &expect![[r#"
2631 namespace namespace3 {
2632 @Config(Adaptive)
2633 function package2_item1() : Unit {}
2634
2635 function package2_item2() : Unit {
2636 package2_item1();
2637 }
2638 }
2639 "#]],
2640 );
2641}
2642
2643#[test]
2644fn resolved_adaptive_callable_from_unrestricted() {
2645 check_with_capabilities(
2646 indoc! {"
2647 namespace A {
2648 @Config(Adaptive)
2649 function Dropped() : Unit {}
2650
2651 function B() : Unit {
2652 Dropped();
2653 }
2654 }
2655 "},
2656 TargetCapabilityFlags::all(),
2657 &expect![[r#"
2658 namespace namespace3 {
2659 @Config(Adaptive)
2660 function package2_item1() : Unit {}
2661
2662 function package2_item2() : Unit {
2663 package2_item1();
2664 }
2665 }
2666 "#]],
2667 );
2668}
2669
2670#[test]
2671fn dropped_not_higher_level_callable_from_unrestricted() {
2672 check_with_capabilities(
2673 indoc! {"
2674 namespace A {
2675 @Config(not HigherLevelConstructs)
2676 function Dropped() : Unit {}
2677
2678 function B() : Unit {
2679 Dropped();
2680 }
2681 }
2682 "},
2683 TargetCapabilityFlags::all(),
2684 &expect![[r#"
2685 namespace namespace3 {
2686 @Config(not HigherLevelConstructs)
2687 function Dropped() : Unit {}
2688
2689 function package2_item1() : Unit {
2690 Dropped();
2691 }
2692 }
2693
2694 // NotAvailable("Dropped", "A.Dropped", Span { lo: 121, hi: 128 })
2695 "#]],
2696 );
2697}
2698
2699#[test]
2700fn resolved_not_higher_level_callable_from_adaptive() {
2701 check_with_capabilities(
2702 indoc! {"
2703 namespace A {
2704 @Config(not HigherLevelConstructs)
2705 function Dropped() : Unit {}
2706
2707 function B() : Unit {
2708 Dropped();
2709 }
2710 }
2711 "},
2712 TargetCapabilityFlags::Adaptive,
2713 &expect![[r#"
2714 namespace namespace3 {
2715 @Config(not HigherLevelConstructs)
2716 function package2_item1() : Unit {}
2717
2718 function package2_item2() : Unit {
2719 package2_item1();
2720 }
2721 }
2722 "#]],
2723 );
2724}
2725
2726#[test]
2727fn resolved_not_higher_level_callable_from_base() {
2728 check_with_capabilities(
2729 indoc! {"
2730 namespace A {
2731 @Config(not HigherLevelConstructs)
2732 function Dropped() : Unit {}
2733
2734 function B() : Unit {
2735 Dropped();
2736 }
2737 }
2738 "},
2739 TargetCapabilityFlags::empty(),
2740 &expect![[r#"
2741 namespace namespace3 {
2742 @Config(not HigherLevelConstructs)
2743 function package2_item1() : Unit {}
2744
2745 function package2_item2() : Unit {
2746 package2_item1();
2747 }
2748 }
2749 "#]],
2750 );
2751}
2752
2753#[test]
2754fn dropped_not_higher_level_and_adaptive_callable_from_base() {
2755 check_with_capabilities(
2756 indoc! {"
2757 namespace A {
2758 @Config(Adaptive)
2759 @Config(not HigherLevelConstructs)
2760 function Dropped() : Unit {}
2761
2762 function B() : Unit {
2763 Dropped();
2764 }
2765 }
2766 "},
2767 TargetCapabilityFlags::empty(),
2768 &expect![[r#"
2769 namespace namespace3 {
2770 @Config(Adaptive)
2771 @Config(not HigherLevelConstructs)
2772 function Dropped() : Unit {}
2773
2774 function package2_item1() : Unit {
2775 Dropped();
2776 }
2777 }
2778
2779 // NotAvailable("Dropped", "A.Dropped", Span { lo: 143, hi: 150 })
2780 "#]],
2781 );
2782}
2783
2784#[test]
2785fn dropped_not_higher_level_and_adaptive_callable_from_unrestricted() {
2786 check_with_capabilities(
2787 indoc! {"
2788 namespace A {
2789 @Config(Adaptive)
2790 @Config(not HigherLevelConstructs)
2791 function Dropped() : Unit {}
2792
2793 function B() : Unit {
2794 Dropped();
2795 }
2796 }
2797 "},
2798 TargetCapabilityFlags::all(),
2799 &expect![[r#"
2800 namespace namespace3 {
2801 @Config(Adaptive)
2802 @Config(not HigherLevelConstructs)
2803 function Dropped() : Unit {}
2804
2805 function package2_item1() : Unit {
2806 Dropped();
2807 }
2808 }
2809
2810 // NotAvailable("Dropped", "A.Dropped", Span { lo: 143, hi: 150 })
2811 "#]],
2812 );
2813}
2814
2815#[test]
2816fn resolved_not_higher_level_and_adaptive_callable_from_adaptive() {
2817 check_with_capabilities(
2818 indoc! {"
2819 namespace A {
2820 @Config(Adaptive)
2821 @Config(not HigherLevelConstructs)
2822 function Dropped() : Unit {}
2823
2824 function B() : Unit {
2825 Dropped();
2826 }
2827 }
2828 "},
2829 TargetCapabilityFlags::Adaptive,
2830 &expect![[r#"
2831 namespace namespace3 {
2832 @Config(Adaptive)
2833 @Config(not HigherLevelConstructs)
2834 function package2_item1() : Unit {}
2835
2836 function package2_item2() : Unit {
2837 package2_item1();
2838 }
2839 }
2840 "#]],
2841 );
2842}
2843
2844#[test]
2845fn dropped_floating_point_from_adaptive() {
2846 check_with_capabilities(
2847 indoc! {"
2848 namespace A {
2849 @Config(FloatingPointComputations)
2850 function Dropped() : Double {}
2851
2852 function B() : Unit {
2853 Dropped();
2854 }
2855 }
2856 "},
2857 TargetCapabilityFlags::Adaptive,
2858 &expect![[r#"
2859 namespace namespace3 {
2860 @Config(FloatingPointComputations)
2861 function Dropped() : Double {}
2862
2863 function package2_item1() : Unit {
2864 Dropped();
2865 }
2866 }
2867
2868 // NotAvailable("Dropped", "A.Dropped", Span { lo: 123, hi: 130 })
2869 "#]],
2870 );
2871}
2872
2873#[test]
2874fn resolved_adaptive_and_integer_from_adaptive_and_integer() {
2875 check_with_capabilities(
2876 indoc! {"
2877 namespace A {
2878 @Config(Adaptive)
2879 @Config(IntegerComputations)
2880 function Dropped() : Double {}
2881
2882 function B() : Unit {
2883 Dropped();
2884 }
2885 }
2886 "},
2887 TargetCapabilityFlags::Adaptive | TargetCapabilityFlags::IntegerComputations,
2888 &expect![[r#"
2889 namespace namespace3 {
2890 @Config(Adaptive)
2891 @Config(IntegerComputations)
2892 function package2_item1() : Double {}
2893
2894 function package2_item2() : Unit {
2895 package2_item1();
2896 }
2897 }
2898 "#]],
2899 );
2900}
2901
2902#[test]
2903fn multiple_definition_dropped_is_not_found() {
2904 check(
2905 indoc! {"
2906 namespace A {
2907 @Config(Adaptive)
2908 operation B() : Unit {}
2909 @Config(Base)
2910 operation B() : Unit {}
2911 @Config(Base)
2912 operation C() : Unit {}
2913 @Config(Adaptive)
2914 operation C() : Unit {}
2915 }
2916 namespace D {
2917 operation E() : Unit {
2918 B();
2919 C();
2920 }
2921 operation F() : Unit {
2922 open A;
2923 B();
2924 C();
2925 }
2926 }
2927 "},
2928 &expect![[r#"
2929 namespace namespace3 {
2930 @Config(Adaptive)
2931 operation package2_item1() : Unit {}
2932 @Config(Base)
2933 operation B() : Unit {}
2934 @Config(Base)
2935 operation C() : Unit {}
2936 @Config(Adaptive)
2937 operation package2_item2() : Unit {}
2938 }
2939 namespace namespace4 {
2940 operation package2_item4() : Unit {
2941 B();
2942 C();
2943 }
2944 operation package2_item5() : Unit {
2945 open namespace3;
2946 package2_item1();
2947 package2_item2();
2948 }
2949 }
2950
2951 // NotFound("B", Span { lo: 257, hi: 258 })
2952 // NotFound("C", Span { lo: 270, hi: 271 })
2953 "#]],
2954 );
2955}
2956
2957#[test]
2958fn disallow_duplicate_intrinsic() {
2959 check(
2960 indoc! {"
2961 namespace A {
2962 operation B() : Unit {
2963 body intrinsic;
2964 }
2965 }
2966 namespace B {
2967 operation B() : Unit {
2968 body intrinsic;
2969 }
2970 }
2971 "},
2972 &expect![[r#"
2973 namespace namespace3 {
2974 operation package2_item1() : Unit {
2975 body intrinsic;
2976 }
2977 }
2978 namespace namespace4 {
2979 operation package2_item3() : Unit {
2980 body intrinsic;
2981 }
2982 }
2983
2984 // DuplicateIntrinsic("B", Span { lo: 101, hi: 102 })
2985 "#]],
2986 );
2987}
2988
2989#[test]
2990fn disallow_duplicate_intrinsic_and_non_intrinsic_collision() {
2991 check(
2992 indoc! {"
2993 namespace A {
2994 internal operation C() : Unit {
2995 body intrinsic;
2996 }
2997 }
2998 namespace B {
2999 operation C() : Unit {}
3000 }
3001 namespace B {
3002 operation C() : Unit {
3003 body intrinsic;
3004 }
3005 }
3006 "},
3007 &expect![[r#"
3008 namespace namespace3 {
3009 internal operation package2_item1() : Unit {
3010 body intrinsic;
3011 }
3012 }
3013 namespace namespace4 {
3014 operation package2_item3() : Unit {}
3015 }
3016 namespace namespace4 {
3017 operation package2_item5() : Unit {
3018 body intrinsic;
3019 }
3020 }
3021
3022 // Duplicate("C", "B", Span { lo: 154, hi: 155 })
3023 // DuplicateIntrinsic("C", Span { lo: 154, hi: 155 })
3024 "#]],
3025 );
3026}
3027
3028#[test]
3029fn disallow_duplicate_intrinsic_and_simulatableintrinsic() {
3030 check(
3031 indoc! {"
3032 namespace A {
3033 operation C() : Unit {
3034 body intrinsic;
3035 }
3036 }
3037 namespace B {
3038 @SimulatableIntrinsic()
3039 operation C() : Unit {}
3040 }
3041 "},
3042 &expect![[r#"
3043 namespace namespace3 {
3044 operation package2_item1() : Unit {
3045 body intrinsic;
3046 }
3047 }
3048 namespace namespace4 {
3049 @SimulatableIntrinsic()
3050 operation package2_item3() : Unit {}
3051 }
3052
3053 // DuplicateIntrinsic("C", Span { lo: 129, hi: 130 })
3054 "#]],
3055 );
3056}
3057
3058#[allow(clippy::cast_possible_truncation)]
3059fn check_locals(input: &str, expect: &Expect) {
3060 let parts = input.split('↘').collect::<Vec<_>>();
3061 assert_eq!(
3062 parts.len(),
3063 2,
3064 "input must contain exactly one cursor marker"
3065 );
3066 let cursor_offset = parts[0].len() as u32;
3067 let source = parts.join("");
3068
3069 let (_, _, locals, _, _) = compile(
3070 &source,
3071 LanguageFeatures::default(),
3072 TargetCapabilityFlags::all(),
3073 );
3074
3075 let locals = locals.get_all_at_offset(cursor_offset);
3076 let actual = locals.iter().fold(String::new(), |mut output, l| {
3077 let _ = match l {
3078 Local::Item(item_id, name) => writeln!(output, "{name} ({item_id})"),
3079 Local::TyParam(param_id, name) => writeln!(output, "{name} (ty_param {param_id})"),
3080 Local::Var(node_id, name) => writeln!(output, "{name} (var {node_id})"),
3081 Local::NamespaceImport(namespace_id, Some(alias)) => {
3082 writeln!(output, "{alias} (namespace {})", usize::from(namespace_id))
3083 }
3084 Local::NamespaceImport(namespace_id, None) => {
3085 writeln!(output, "namespace {}", usize::from(namespace_id))
3086 }
3087 };
3088 output
3089 });
3090
3091 expect.assert_eq(&actual);
3092}
3093
3094#[test]
3095fn get_locals_vars() {
3096 check_locals(
3097 indoc! {"
3098 namespace Foo {
3099 function A() : Int {
3100 let x = 0;
3101
3102 let y = 0;
3103 }
3104 }
3105 "},
3106 &expect![[r#"
3107 x (var 13)
3108 namespace 3
3109 "#]],
3110 );
3111}
3112
3113#[test]
3114fn get_locals_vars_shadowing_same_scope() {
3115 check_locals(
3116 indoc! {r#"
3117 namespace Foo {
3118 function A() : Int {
3119 let x = 0;
3120 let x = "foo";
3121
3122 }
3123 }
3124 "#},
3125 &expect![[r#"
3126 x (var 17)
3127 namespace 3
3128 "#]],
3129 );
3130}
3131
3132#[test]
3133fn get_locals_vars_parent_scope() {
3134 check_locals(
3135 indoc! {r#"
3136 namespace Foo {
3137 function A() : Int {
3138 let x = 0;
3139 {
3140 let y = 0;
3141
3142 }
3143 }
3144 }
3145 "#},
3146 &expect![[r#"
3147 y (var 20)
3148 x (var 13)
3149 namespace 3
3150 "#]],
3151 );
3152}
3153
3154#[test]
3155fn get_locals_params() {
3156 check_locals(
3157 indoc! {r#"
3158 namespace Foo {
3159 function A(x : Int) : Int {
3160
3161 }
3162 }
3163 "#},
3164 &expect![[r#"
3165 x (var 8)
3166 namespace 3
3167 "#]],
3168 );
3169}
3170
3171#[test]
3172fn get_locals_spec_params() {
3173 check_locals(
3174 indoc! {"
3175 namespace Foo {
3176 operation A(q : Qubit) : (Qubit[], Qubit) {
3177 controlled (cs, ...) {
3178
3179 }
3180 }
3181 }
3182 "},
3183 &expect![[r#"
3184 cs (var 23)
3185 q (var 8)
3186 namespace 3
3187 "#]],
3188 );
3189}
3190
3191#[test]
3192fn get_locals_before_binding() {
3193 check_locals(
3194 indoc! {"
3195 namespace Foo {
3196 function A() : Unit {
3197 let y = 0;
3198 let x = { ↘ };
3199 }
3200 }
3201 "},
3202 &expect![[r#"
3203 y (var 13)
3204 namespace 3
3205 "#]],
3206 );
3207}
3208
3209#[test]
3210fn get_locals_lambda_params() {
3211 check_locals(
3212 indoc! {"
3213 namespace Foo {
3214 function A() : Unit {
3215 let y = 0;
3216 let f = x -> { ↘ };
3217 }
3218 }
3219 "},
3220 &expect![[r#"
3221 x (var 20)
3222 y (var 13)
3223 namespace 3
3224 "#]],
3225 );
3226}
3227
3228#[test]
3229fn get_locals_for_loop() {
3230 check_locals(
3231 indoc! {"
3232 namespace Foo {
3233 function A() : Unit {
3234 for x in 0..1 {
3235
3236 }
3237 }
3238 }
3239 "},
3240 &expect![[r#"
3241 x (var 14)
3242 namespace 3
3243 "#]],
3244 );
3245}
3246
3247#[test]
3248fn get_locals_for_loop_before_binding() {
3249 check_locals(
3250 indoc! {"
3251 namespace Foo {
3252 function A() : Unit {
3253 for x in 0..{ ↘ } {
3254 }
3255 }
3256 }
3257 "},
3258 &expect![[r#"
3259 namespace 3
3260 "#]],
3261 );
3262}
3263
3264#[test]
3265fn get_locals_items() {
3266 check_locals(
3267 indoc! {"
3268 namespace Foo {
3269 function A() : Unit {
3270
3271 function B() : Unit {}
3272 newtype Bar = String;
3273 }
3274 }
3275 "},
3276 &expect![[r#"
3277 Bar (Item 3 (Package 2))
3278 B (Item 2 (Package 2))
3279 namespace 3
3280 "#]],
3281 );
3282}
3283
3284#[test]
3285fn get_locals_local_item_hide_parent_scope_variables() {
3286 check_locals(
3287 indoc! {"
3288 namespace Foo {
3289 function A() : Unit {
3290 let x = 3;
3291 function B() : Unit {
3292 let y = 3;
3293
3294 }
3295 }
3296 }
3297 "},
3298 &expect![[r#"
3299 y (var 26)
3300 B (Item 2 (Package 2))
3301 namespace 3
3302 "#]],
3303 );
3304}
3305
3306#[test]
3307fn get_locals_shadow_parent_scope() {
3308 check_locals(
3309 indoc! {r#"
3310 namespace Foo {
3311 function A() : Unit {
3312 let x = "foo";
3313 {
3314 let x = 0;
3315
3316 }
3317 }
3318 }
3319 "#},
3320 &expect![[r#"
3321 x (var 20)
3322 namespace 3
3323 "#]],
3324 );
3325}
3326
3327#[test]
3328fn get_locals_type_params() {
3329 check_locals(
3330 indoc! {"
3331 namespace Foo {
3332 function A<'T>(t: 'T) : Unit {
3333 {
3334
3335 }
3336 }
3337 }
3338 "},
3339 &expect![[r#"
3340 t (var 9)
3341 'T (ty_param 0)
3342 namespace 3
3343 "#]],
3344 );
3345}
3346
3347#[test]
3348fn get_locals_block_scope_boundary() {
3349 check_locals(
3350 indoc! {"
3351 namespace Foo {
3352 function A() : Int {
3353 {
3354 let x = 0;
3355 }↘
3356 }
3357 }
3358 "},
3359 &expect![[r#"
3360 namespace 3
3361 "#]],
3362 );
3363}
3364
3365#[test]
3366fn get_locals_block_scope_boundary_begin() {
3367 check_locals(
3368 indoc! {"
3369 namespace Foo {
3370 function A() : Int {
3371 ↘{
3372 function Bar(): Unit {}
3373 }
3374 }
3375 }
3376 "},
3377 &expect![[r#"
3378 namespace 3
3379 "#]],
3380 );
3381}
3382
3383#[test]
3384fn get_locals_item_imports() {
3385 check_locals(
3386 indoc! {"
3387 namespace Bar {
3388 function A() : Unit {}
3389 }
3390 namespace Foo {
3391 import Bar.A;
3392 import Bar.A as B;
3393 function C() : Int {
3394
3395 }
3396 }
3397 "},
3398 &expect![[r#"
3399 A (Item 1 (Package 2))
3400 B (Item 1 (Package 2))
3401 namespace 4
3402 "#]],
3403 );
3404}
3405
3406#[test]
3407fn get_locals_namespace_imports() {
3408 check_locals(
3409 indoc! {"
3410 namespace Bar {}
3411 namespace Foo {
3412 import Bar;
3413 import Bar as Baz;
3414 import Bar.*;
3415 function A() : Int {
3416
3417 }
3418 }
3419 "},
3420 &expect![[r#"
3421 namespace 4
3422 namespace 3
3423 Bar (namespace 3)
3424 Baz (namespace 3)
3425 "#]],
3426 );
3427}
3428
3429#[test]
3430fn use_after_scope() {
3431 check(
3432 indoc! {"
3433 namespace Foo {
3434 function A() : Unit {
3435 {
3436 let x = 42;
3437 }
3438 x; // x should not be accessible here
3439 }
3440 }
3441 "},
3442 &expect![[r#"
3443 namespace namespace3 {
3444 function package2_item1() : Unit {
3445 {
3446 let local16 = 42;
3447 }
3448 x; // x should not be accessible here
3449 }
3450 }
3451
3452 // NotFound("x", Span { lo: 94, hi: 95 })
3453 "#]],
3454 );
3455}
3456
3457#[test]
3458fn nested_function_definition() {
3459 check(
3460 indoc! {"
3461 namespace Foo {
3462 function A() : Unit {
3463 function B() : Unit {
3464 function C() : Unit {}
3465 C();
3466 }
3467 B();
3468 }
3469 }
3470 "},
3471 &expect![[r#"
3472 namespace namespace3 {
3473 function package2_item1() : Unit {
3474 function package2_item2() : Unit {
3475 function package2_item3() : Unit {}
3476 package2_item3();
3477 }
3478 package2_item2();
3479 }
3480 }
3481 "#]],
3482 );
3483}
3484
3485#[test]
3486fn variable_in_nested_blocks() {
3487 check(
3488 indoc! {"
3489 namespace Foo {
3490 function A() : Unit {
3491 {
3492 let x = 10;
3493 {
3494 let y = x + 5;
3495 y; // Should be accessible
3496 }
3497 y; // Should not be accessible
3498 }
3499 }
3500 }
3501 "},
3502 &expect![[r#"
3503 namespace namespace3 {
3504 function package2_item1() : Unit {
3505 {
3506 let local16 = 10;
3507 {
3508 let local23 = local16 + 5;
3509 local23; // Should be accessible
3510 }
3511 y; // Should not be accessible
3512 }
3513 }
3514 }
3515
3516 // NotFound("y", Span { lo: 190, hi: 191 })
3517 "#]],
3518 );
3519}
3520
3521#[test]
3522fn function_call_with_namespace_alias() {
3523 check(
3524 indoc! {"
3525 namespace Foo {
3526 function A() : Unit {}
3527 }
3528 namespace Bar {
3529 open Foo as F;
3530 function B() : Unit {
3531 F.A();
3532 }
3533 }
3534 "},
3535 &expect![[r#"
3536 namespace namespace3 {
3537 function package2_item1() : Unit {}
3538 }
3539 namespace namespace4 {
3540 open namespace3 as F;
3541 function package2_item3() : Unit {
3542 package2_item1();
3543 }
3544 }
3545 "#]],
3546 );
3547}
3548
3549#[test]
3550fn type_alias_in_function_scope() {
3551 check(
3552 indoc! {"
3553 namespace Foo {
3554 function A() : Unit {
3555 newtype MyInt = Int;
3556 let x : MyInt = MyInt(5);
3557 }
3558 function B() : Unit {
3559 let z: MyInt = MyInt(5); // this should be a different type (and unresolved)
3560 }
3561 }
3562 "},
3563 &expect![[r#"
3564 namespace namespace3 {
3565 function package2_item1() : Unit {
3566 newtype package2_item3 = Int;
3567 let local20 : package2_item3 = package2_item3(5);
3568 }
3569 function package2_item2() : Unit {
3570 let local40: MyInt = MyInt(5); // this should be a different type (and unresolved)
3571 }
3572 }
3573
3574 // NotFound("MyInt", Span { lo: 152, hi: 157 })
3575 // NotFound("MyInt", Span { lo: 160, hi: 165 })
3576 "#]],
3577 );
3578}
3579
3580#[test]
3581fn lambda_inside_lambda() {
3582 check(
3583 indoc! {"
3584 namespace Foo {
3585 function A() : Unit {
3586 let f = () -> {
3587 let g = (x) -> x + 1;
3588 g(10);
3589 };
3590 f();
3591 }
3592 }
3593 "},
3594 &expect![[r#"
3595 namespace namespace3 {
3596 function package2_item1() : Unit {
3597 let local13 = () -> {
3598 let local20 = (local24) -> local24 + 1;
3599 local20(10);
3600 };
3601 local13();
3602 }
3603 }
3604 "#]],
3605 );
3606}
3607
3608#[test]
3609fn nested_namespaces_with_same_function_name() {
3610 check(
3611 indoc! {"
3612 namespace Foo {
3613 function A() : Unit {}
3614 }
3615 namespace Bar {
3616 function A() : Unit {}
3617 function B() : Unit {
3618 Foo.A();
3619 A(); // Should call Bar.A without needing to qualify
3620 }
3621 }
3622 "},
3623 &expect![[r#"
3624 namespace namespace3 {
3625 function package2_item1() : Unit {}
3626 }
3627 namespace namespace4 {
3628 function package2_item3() : Unit {}
3629 function package2_item4() : Unit {
3630 package2_item1();
3631 package2_item3(); // Should call Bar.A without needing to qualify
3632 }
3633 }
3634 "#]],
3635 );
3636}
3637
3638#[test]
3639fn newtype_with_invalid_field_type() {
3640 check(
3641 indoc! {"
3642 namespace Foo {
3643 newtype Complex = (Re: Real, Im: Imaginary); // Imaginary is not a valid type
3644 }
3645 "},
3646 &expect![[r#"
3647 namespace namespace3 {
3648 newtype package2_item1 = (Re: Real, Im: Imaginary); // Imaginary is not a valid type
3649 }
3650
3651 // NotFound("Real", Span { lo: 43, hi: 47 })
3652 // NotFound("Imaginary", Span { lo: 53, hi: 62 })
3653 "#]],
3654 );
3655}
3656
3657#[test]
3658fn newtype_with_tuple_destructuring() {
3659 check(
3660 indoc! {"
3661 namespace Foo {
3662 newtype Pair = (First: Int, Second: Int);
3663 function Destructure(pair: Pair) : Int {
3664 let (first, second) = pair;
3665 first + second
3666 }
3667 }
3668 "},
3669 &expect![[r#"
3670 namespace namespace3 {
3671 newtype package2_item1 = (First: Int, Second: Int);
3672 function package2_item2(local21: package2_item1) : Int {
3673 let (local32, local34) = local21;
3674 local32 + local34
3675 }
3676 }
3677 "#]],
3678 );
3679}
3680
3681#[test]
3682fn items_resolve_according_to_implicit_hierarchy() {
3683 check(
3684 indoc! {"
3685namespace Foo {
3686 @EntryPoint()
3687 function Main(): Int {
3688 Foo()
3689 }
3690
3691 function Foo() : Int {
3692 Bar.Baz.Quux()
3693 }
3694}
3695
3696namespace Foo.Bar.Baz {
3697 function Quux() : Int { 6 }
3698}
3699"},
3700 &expect![[r#"
3701 namespace namespace3 {
3702 @EntryPoint()
3703 function package2_item1(): Int {
3704 package2_item2()
3705 }
3706
3707 function package2_item2() : Int {
3708 package2_item4()
3709 }
3710 }
3711
3712 namespace namespace5 {
3713 function package2_item4() : Int { 6 }
3714 }
3715 "#]],
3716 );
3717}
3718
3719#[test]
3720fn basic_hierarchical_namespace() {
3721 check(
3722 indoc! {"
3723 namespace Foo.Bar.Baz {
3724 operation Quux() : Unit {}
3725 }
3726 namespace A {
3727 open Foo;
3728 operation Main() : Unit {
3729 Bar.Baz.Quux();
3730 }
3731 }
3732 namespace B {
3733 open Foo.Bar;
3734 operation Main() : Unit {
3735 Baz.Quux();
3736 }
3737 }"},
3738 &expect![[r#"
3739 namespace namespace5 {
3740 operation package2_item1() : Unit {}
3741 }
3742 namespace namespace6 {
3743 open namespace3;
3744 operation package2_item3() : Unit {
3745 package2_item1();
3746 }
3747 }
3748 namespace namespace7 {
3749 open namespace4;
3750 operation package2_item5() : Unit {
3751 package2_item1();
3752 }
3753 }"#]],
3754 );
3755}
3756
3757#[test]
3758fn test_katas_shadowing_use_case() {
3759 check(
3760 indoc! {"namespace Kata {
3761 operation ApplyX() : Unit {
3762 // Do nothing.
3763 }
3764}
3765
3766namespace Kata.Verification {
3767 operation CheckSolution() : Bool {
3768 let _ = Kata.ApplyX();
3769 let _ = ApplyX();
3770 }
3771
3772 operation ApplyX() : Unit {}
3773}
3774" },
3775 &expect![[r#"
3776 namespace namespace3 {
3777 operation package2_item1() : Unit {
3778 // Do nothing.
3779 }
3780 }
3781
3782 namespace namespace4 {
3783 operation package2_item3() : Bool {
3784 let _ = package2_item1();
3785 let _ = package2_item4();
3786 }
3787
3788 operation package2_item4() : Unit {}
3789 }
3790 "#]],
3791 );
3792}
3793
3794#[test]
3795fn open_can_access_parent_scope() {
3796 check(
3797 indoc! {r#"
3798namespace Foo.Bar {
3799 operation Hello() : Unit {
3800
3801 }
3802}
3803
3804namespace Foo {
3805 open Bar;
3806 @EntryPoint()
3807 operation Main() : Unit {
3808 Hello();
3809 }
3810}"#},
3811 &expect![[r#"
3812 namespace namespace4 {
3813 operation package2_item1() : Unit {
3814
3815 }
3816 }
3817
3818 namespace namespace3 {
3819 open Bar;
3820 @EntryPoint()
3821 operation package2_item3() : Unit {
3822 package2_item1();
3823 }
3824 }"#]],
3825 );
3826}
3827
3828#[test]
3829fn test_export_statement() {
3830 check(
3831 indoc! {"
3832 namespace Foo {
3833 operation ApplyX() : Unit {
3834 }
3835 export ApplyX;
3836 }
3837 "},
3838 &expect![[r#"
3839 namespace namespace3 {
3840 operation package2_item1() : Unit {
3841 }
3842 export item1 as package2_item2;
3843 }
3844 "#]],
3845 );
3846}
3847
3848#[test]
3849fn test_complicated_nested_export_statement() {
3850 check(
3851 indoc! {"
3852 namespace Foo {
3853 export Foo.Bar.Baz.Quux.HelloWorld;
3854 }
3855
3856 namespace Foo.Bar.Baz.Quux {
3857 function HelloWorld() : Unit {}
3858 }
3859
3860 namespace Foo.Bar {
3861 export Baz.Quux.HelloWorld;
3862 }
3863
3864 namespace Foo.Bar.Baz {
3865 export Quux.HelloWorld;
3866 }
3867
3868 namespace Foo.Bar.Graule {
3869 // HelloWorld should be available from all namespaces
3870 operation Main() : Unit {
3871 Foo.Bar.Baz.Quux.HelloWorld();
3872 Foo.Bar.Baz.HelloWorld();
3873 Foo.Bar.HelloWorld();
3874 Foo.HelloWorld();
3875 open Foo;
3876 HelloWorld();
3877 }
3878 // and we should be able to re-export it
3879 export Foo.HelloWorld;
3880 }
3881 "},
3882 &expect![[r#"
3883 namespace namespace3 {
3884 export item3 as package2_item1;
3885 }
3886
3887 namespace namespace6 {
3888 function package2_item3() : Unit {}
3889 }
3890
3891 namespace namespace4 {
3892 export item3 as package2_item5;
3893 }
3894
3895 namespace namespace5 {
3896 export item3 as package2_item7;
3897 }
3898
3899 namespace namespace7 {
3900 // HelloWorld should be available from all namespaces
3901 operation package2_item9() : Unit {
3902 package2_item3();
3903 package2_item3();
3904 package2_item3();
3905 package2_item3();
3906 open namespace3;
3907 package2_item3();
3908 }
3909 // and we should be able to re-export it
3910 export item3 as package2_item10;
3911 }
3912 "#]],
3913 );
3914}
3915
3916#[test]
3917fn exports_aware_of_opens() {
3918 check(
3919 indoc! {r#"
3920 namespace Foo {
3921 operation F() : Unit {}
3922 }
3923
3924 namespace Main {
3925 open Foo;
3926 export F;
3927 }
3928 "#},
3929 &expect![[r#"
3930 namespace namespace3 {
3931 operation package2_item1() : Unit {}
3932 }
3933
3934 namespace namespace4 {
3935 open namespace3;
3936 export item1 as package2_item3;
3937 }
3938 "#]],
3939 );
3940}
3941
3942#[test]
3943fn export_symbol_and_call_it() {
3944 check(
3945 indoc! {"
3946 namespace Foo {
3947 export Foo.Bar.Baz.Quux.Function;
3948 }
3949
3950 namespace Foo.Bar.Baz.Quux {
3951 function Function() : Unit {}
3952 }
3953
3954 namespace Main {
3955 open Foo;
3956 operation Main() : Unit {
3957 Foo.Function();
3958 Function();
3959 }
3960 }
3961 "},
3962 &expect![[r#"
3963 namespace namespace3 {
3964 export item3 as package2_item1;
3965 }
3966
3967 namespace namespace6 {
3968 function package2_item3() : Unit {}
3969 }
3970
3971 namespace namespace7 {
3972 open namespace3;
3973 operation package2_item5() : Unit {
3974 package2_item3();
3975 package2_item3();
3976 }
3977 }
3978 "#]],
3979 );
3980}
3981
3982#[test]
3983fn multiple_exports() {
3984 check(
3985 indoc! {"
3986 namespace Foo {
3987 operation ApplyX() : Unit {}
3988 operation ApplyY() : Unit {}
3989 }
3990 namespace Main {
3991 import Foo.ApplyX as X, Foo.ApplyY as Y;
3992 operation Main() : Unit {
3993 X();
3994 Y();
3995 }
3996 }
3997 "},
3998 &expect![[r#"
3999 namespace namespace3 {
4000 operation package2_item1() : Unit {}
4001 operation package2_item2() : Unit {}
4002 }
4003 namespace namespace4 {
4004 import item1 as package2_item4, item2 as package2_item5;
4005 operation package2_item6() : Unit {
4006 package2_item1();
4007 package2_item2();
4008 }
4009 }
4010 "#]],
4011 );
4012}
4013
4014#[test]
4015fn no_exports() {
4016 check(
4017 indoc! {"
4018 namespace Foo {
4019 operation ApplyX() : Unit {}
4020 }
4021 namespace Main {
4022 open Foo;
4023 operation Main() : Unit {
4024 ApplyX();
4025 }
4026 }
4027 "},
4028 &expect![[r#"
4029 namespace namespace3 {
4030 operation package2_item1() : Unit {}
4031 }
4032 namespace namespace4 {
4033 open namespace3;
4034 operation package2_item3() : Unit {
4035 package2_item1();
4036 }
4037 }
4038 "#]],
4039 );
4040}
4041
4042#[test]
4043fn export_non_existent_symbol() {
4044 check(
4045 indoc! {"
4046 namespace Foo {
4047 export NonExistent;
4048 }
4049 "},
4050 &expect![[r#"
4051 namespace namespace3 {
4052 export NonExistent as package2_item1;
4053 }
4054
4055 // NotFound("NonExistent", Span { lo: 27, hi: 38 })
4056 "#]],
4057 );
4058}
4059
4060#[test]
4061fn export_symbol_from_nested_namespace() {
4062 check(
4063 indoc! {"
4064 namespace Foo.Bar {
4065 operation ApplyX() : Unit {}
4066 }
4067
4068 namespace Foo {
4069 export Bar.ApplyX;
4070 }
4071
4072 namespace Main {
4073 open Foo;
4074 operation Main() : Unit {
4075 Bar.ApplyX();
4076 ApplyX();
4077 }
4078 }
4079 "},
4080 &expect![[r#"
4081 namespace namespace4 {
4082 operation package2_item1() : Unit {}
4083 }
4084
4085 namespace namespace3 {
4086 export item1 as package2_item3;
4087 }
4088
4089 namespace namespace5 {
4090 open namespace3;
4091 operation package2_item5() : Unit {
4092 package2_item1();
4093 package2_item1();
4094 }
4095 }
4096 "#]],
4097 );
4098}
4099
4100#[test]
4101fn disallow_exporting_local_vars() {
4102 check(
4103 indoc! {"
4104 namespace Foo {
4105 operation Main() : Unit {
4106 let x = 5;
4107 }
4108 export x;
4109 }
4110 "},
4111 &expect![[r#"
4112 namespace namespace3 {
4113 operation package2_item1() : Unit {
4114 let local13 = 5;
4115 }
4116 export x as package2_item2;
4117 }
4118
4119 // NotFound("x", Span { lo: 82, hi: 83 })
4120 "#]],
4121 );
4122}
4123
4124#[test]
4125fn disallow_export_non_item() {
4126 check(
4127 indoc! {"
4128 namespace Bar {}
4129
4130 namespace Foo {
4131 operation Main() : Unit {
4132 }
4133 export Unit;
4134 }
4135 "},
4136 &expect![[r#"
4137 namespace namespace3 {}
4138
4139 namespace namespace4 {
4140 operation package2_item2() : Unit {
4141 }
4142 export Unit as package2_item3;
4143 }
4144
4145 // NotFound("Unit", Span { lo: 81, hi: 85 })
4146 "#]],
4147 );
4148}
4149
4150#[test]
4151fn export_udt() {
4152 check(
4153 indoc! {"
4154 namespace Foo {
4155 newtype Pair = (First: Int, Second: Int);
4156 export Pair;
4157 }
4158 namespace Main {
4159 open Foo;
4160 operation Main() : Unit {
4161 Pair(1, 2);
4162 }
4163 }
4164 "},
4165 &expect![[r#"
4166 namespace namespace3 {
4167 newtype package2_item1 = (First: Int, Second: Int);
4168 export item1 as package2_item2;
4169 }
4170 namespace namespace4 {
4171 open namespace3;
4172 operation package2_item4() : Unit {
4173 package2_item1(1, 2);
4174 }
4175 }
4176 "#]],
4177 );
4178}
4179
4180#[test]
4181fn export_with_alias() {
4182 check(
4183 indoc! {"
4184 namespace Foo {
4185 operation ApplyX() : Unit {}
4186 export ApplyX as SomeAlias;
4187 }
4188 namespace Main {
4189 open Foo;
4190 operation Main() : Unit {
4191 SomeAlias();
4192 }
4193 }
4194 "},
4195 &expect![[r#"
4196 namespace namespace3 {
4197 operation package2_item1() : Unit {}
4198 export item1 as package2_item2;
4199 }
4200 namespace namespace4 {
4201 open namespace3;
4202 operation package2_item4() : Unit {
4203 package2_item1();
4204 }
4205 }
4206 "#]],
4207 );
4208}
4209
4210#[test]
4211fn multiple_exports_with_aliases() {
4212 check(
4213 indoc! {"
4214 namespace Foo {
4215 operation ApplyX() : Unit {}
4216 operation ApplyY() : Unit {}
4217 export ApplyX as SomeAlias, ApplyY as AnotherAlias;
4218 }
4219 namespace Main {
4220 open Foo;
4221 operation Main() : Unit {
4222 SomeAlias();
4223 AnotherAlias();
4224 }
4225 }
4226 "},
4227 &expect![[r#"
4228 namespace namespace3 {
4229 operation package2_item1() : Unit {}
4230 operation package2_item2() : Unit {}
4231 export item1 as package2_item3, item2 as package2_item4;
4232 }
4233 namespace namespace4 {
4234 open namespace3;
4235 operation package2_item6() : Unit {
4236 package2_item1();
4237 package2_item2();
4238 }
4239 }
4240 "#]],
4241 );
4242}
4243
4244#[test]
4245fn aliased_exports_call_with_qualified_paths() {
4246 check(
4247 indoc! {"
4248 namespace Foo {
4249 operation ApplyX() : Unit {}
4250 operation ApplyY() : Unit {}
4251 export ApplyX as SomeAlias, ApplyY as AnotherAlias;
4252 }
4253 namespace Main {
4254 open Foo;
4255 operation Main() : Unit {
4256 Foo.SomeAlias();
4257 Foo.AnotherAlias();
4258 }
4259 }
4260 "},
4261 &expect![[r#"
4262 namespace namespace3 {
4263 operation package2_item1() : Unit {}
4264 operation package2_item2() : Unit {}
4265 export item1 as package2_item3, item2 as package2_item4;
4266 }
4267 namespace namespace4 {
4268 open namespace3;
4269 operation package2_item6() : Unit {
4270 package2_item1();
4271 package2_item2();
4272 }
4273 }
4274 "#]],
4275 );
4276}
4277
4278#[test]
4279fn reexport_from_full_path_with_alias() {
4280 check(
4281 indoc! {"
4282 namespace Foo {
4283 operation ApplyX() : Unit {}
4284 export ApplyX as SomeAlias;
4285 }
4286 namespace Main {
4287 open Foo;
4288 export Foo.SomeAlias as AnotherAlias;
4289 }
4290 "},
4291 &expect![[r#"
4292 namespace namespace3 {
4293 operation package2_item1() : Unit {}
4294 export item1 as package2_item2;
4295 }
4296 namespace namespace4 {
4297 open namespace3;
4298 export item1 as package2_item4;
4299 }
4300 "#]],
4301 );
4302}
4303
4304#[test]
4305fn disallow_repeated_exports() {
4306 check(
4307 indoc! {"
4308 namespace Foo {
4309 operation ApplyX() : Unit {}
4310 export ApplyX;
4311 export ApplyX;
4312 }
4313 "},
4314 &expect![[r#"
4315 namespace namespace3 {
4316 operation package2_item1() : Unit {}
4317 export item1 as package2_item2;
4318 export item1 as package2_item3;
4319 }
4320
4321 // DuplicateExport { name: "ApplyX", span: Span { lo: 79, hi: 85 }, existing_span: Span { lo: 60, hi: 66 } }
4322 "#]],
4323 );
4324}
4325
4326#[test]
4327fn disallow_repeated_exports_inline() {
4328 check(
4329 indoc! {"
4330 namespace Foo {
4331 operation ApplyX() : Unit {}
4332 export ApplyX, ApplyX;
4333 }
4334 "},
4335 &expect![[r#"
4336 namespace namespace3 {
4337 operation package2_item1() : Unit {}
4338 export item1 as package2_item2, item1 as package2_item3;
4339 }
4340
4341 // DuplicateExport { name: "ApplyX", span: Span { lo: 68, hi: 74 }, existing_span: Span { lo: 60, hi: 66 } }
4342 "#]],
4343 );
4344}
4345
4346#[test]
4347fn order_of_exports_does_not_matter() {
4348 check(
4349 indoc! {"
4350 namespace Bar {
4351 export Foo.ApplyX;
4352 export ApplyY;
4353 operation ApplyY() : Unit {}
4354 }
4355 namespace Foo {
4356 operation ApplyX() : Unit {}
4357 }
4358
4359 "},
4360 &expect![[r#"
4361 namespace namespace3 {
4362 export item5 as package2_item1;
4363 export item3 as package2_item2;
4364 operation package2_item3() : Unit {}
4365 }
4366 namespace namespace4 {
4367 operation package2_item5() : Unit {}
4368 }
4369
4370 "#]],
4371 );
4372}
4373
4374#[test]
4375fn export_udt_and_construct_it() {
4376 check(
4377 indoc! {"
4378 namespace Foo {
4379 newtype Pair = (First: Int, Second: Int);
4380 export Pair;
4381 }
4382 namespace Main {
4383 open Foo;
4384 operation Main() : Unit {
4385 let z: Pair = Pair(1, 2);
4386 }
4387 }
4388 "},
4389 &expect![[r#"
4390 namespace namespace3 {
4391 newtype package2_item1 = (First: Int, Second: Int);
4392 export item1 as package2_item2;
4393 }
4394 namespace namespace4 {
4395 open namespace3;
4396 operation package2_item4() : Unit {
4397 let local34: package2_item1 = package2_item1(1, 2);
4398 }
4399 }
4400 "#]],
4401 );
4402}
4403#[test]
4404fn import_single_item() {
4405 check(
4406 indoc! {"
4407 namespace Foo {
4408 function Bar() : Unit {}
4409 }
4410 namespace Main {
4411 import Foo.Bar;
4412 operation Main() : Unit {
4413 Bar();
4414 }
4415 }
4416 "},
4417 &expect![[r#"
4418 namespace namespace3 {
4419 function package2_item1() : Unit {}
4420 }
4421 namespace namespace4 {
4422 import item1 as package2_item3;
4423 operation package2_item4() : Unit {
4424 package2_item1();
4425 }
4426 }
4427 "#]],
4428 );
4429}
4430
4431#[test]
4432fn import_namespace() {
4433 check(
4434 indoc! {"
4435 namespace Foo.Bar {
4436 function Baz() : Unit {}
4437 }
4438 namespace Main {
4439 import Foo.Bar;
4440 operation Main() : Unit {
4441 Bar.Baz();
4442 }
4443 }
4444 "},
4445 &expect![[r#"
4446 namespace namespace4 {
4447 function package2_item1() : Unit {}
4448 }
4449 namespace namespace5 {
4450 import namespace4 as package2_item3;
4451 operation package2_item4() : Unit {
4452 package2_item1();
4453 }
4454 }
4455 "#]],
4456 );
4457}
4458
4459#[test]
4460fn import_non_existent_item() {
4461 check(
4462 indoc! {"
4463 namespace Foo {
4464 }
4465 namespace Main {
4466 import Foo.Bar;
4467 operation Main() : Unit {
4468 Bar();
4469 }
4470 }
4471 "},
4472 &expect![[r#"
4473 namespace namespace3 {
4474 }
4475 namespace namespace4 {
4476 import Foo.Bar as package2_item2;
4477 operation package2_item3() : Unit {
4478 Bar();
4479 }
4480 }
4481
4482 // NotFound("Foo.Bar", Span { lo: 46, hi: 53 })
4483 // NotFound("Bar", Span { lo: 93, hi: 96 })
4484 "#]],
4485 );
4486}
4487
4488#[test]
4489fn import_name_collision_with_callable() {
4490 check(
4491 indoc! {"
4492 namespace Foo {
4493 function Bar() : Unit {}
4494 }
4495 namespace Main {
4496 function Bar() : Unit {}
4497 import Foo.Bar;
4498 operation Main() : Unit {
4499 Bar();
4500 }
4501 }
4502 "},
4503 &expect![[r#"
4504 namespace namespace3 {
4505 function package2_item1() : Unit {}
4506 }
4507 namespace namespace4 {
4508 function package2_item3() : Unit {}
4509 import item1 as package2_item4;
4510 operation package2_item5() : Unit {
4511 package2_item3();
4512 }
4513 }
4514
4515 // Duplicate("Bar", "Main", Span { lo: 108, hi: 111 })
4516 "#]],
4517 );
4518}
4519
4520#[test]
4521fn import_with_alias() {
4522 check(
4523 indoc! {"
4524 namespace Foo {
4525 function Bar() : Unit {}
4526 }
4527 namespace Main {
4528 import Foo.Bar as Baz;
4529 operation Main() : Unit {
4530 Baz();
4531 }
4532 }
4533 "},
4534 &expect![[r#"
4535 namespace namespace3 {
4536 function package2_item1() : Unit {}
4537 }
4538 namespace namespace4 {
4539 import item1 as package2_item3;
4540 operation package2_item4() : Unit {
4541 package2_item1();
4542 }
4543 }
4544 "#]],
4545 );
4546}
4547
4548#[test]
4549fn import_non_item() {
4550 check(
4551 indoc! {"
4552 namespace Main {
4553 import Unit;
4554 operation Main() : Unit {
4555 }
4556 }
4557 "},
4558 &expect![[r#"
4559 namespace namespace3 {
4560 import Unit as package2_item1;
4561 operation package2_item2() : Unit {
4562 }
4563 }
4564
4565 // NotFound("Unit", Span { lo: 28, hi: 32 })
4566 "#]],
4567 );
4568}
4569
4570#[test]
4571fn import_namespace_nested() {
4572 check(
4573 indoc! {"
4574 namespace Foo.Bar.Baz {
4575 operation Quux() : Unit {}
4576 }
4577 namespace Main {
4578 import Foo.Bar;
4579 operation Main() : Unit {
4580 Bar.Baz.Quux();
4581 }
4582 }
4583 "},
4584 &expect![[r#"
4585 namespace namespace5 {
4586 operation package2_item1() : Unit {}
4587 }
4588 namespace namespace6 {
4589 import namespace4 as package2_item3;
4590 operation package2_item4() : Unit {
4591 package2_item1();
4592 }
4593 }
4594 "#]],
4595 );
4596}
4597
4598#[test]
4599fn import_single_namespace() {
4600 check(
4601 indoc! {"
4602 namespace Foo {
4603 operation Bar() : Unit {}
4604 }
4605 namespace Main {
4606 import Foo;
4607
4608 operation Main() : Unit {
4609 Foo.Bar();
4610 }
4611 }
4612 "},
4613 &expect![[r#"
4614 namespace namespace3 {
4615 operation package2_item1() : Unit {}
4616 }
4617 namespace namespace4 {
4618 import namespace3 as package2_item3;
4619
4620 operation package2_item4() : Unit {
4621 package2_item1();
4622 }
4623 }
4624 "#]],
4625 );
4626}
4627
4628#[test]
4629fn import_shadowing_function() {
4630 check(
4631 indoc! {"
4632 namespace Foo {
4633 operation Bar() : Unit {}
4634 }
4635 namespace Main {
4636 operation Bar() : Unit {}
4637 operation Main() : Unit {
4638 import Foo.Bar;
4639 Bar();
4640 }
4641 }
4642 "},
4643 &expect![[r#"
4644 namespace namespace3 {
4645 operation package2_item1() : Unit {}
4646 }
4647 namespace namespace4 {
4648 operation package2_item3() : Unit {}
4649 operation package2_item4() : Unit {
4650 import item1 as package2_item5;
4651 package2_item1();
4652 }
4653 }
4654 "#]],
4655 );
4656}
4657
4658#[test]
4659fn import_non_existent_namespace() {
4660 check(
4661 indoc! {"
4662 namespace Main {
4663 operation Main() : Unit {
4664 import NonExistent;
4665 }
4666 }
4667 "},
4668 &expect![[r#"
4669 namespace namespace3 {
4670 operation package2_item1() : Unit {
4671 import NonExistent as package2_item2;
4672 }
4673 }
4674
4675 // NotFound("NonExistent", Span { lo: 62, hi: 73 })
4676 "#]],
4677 );
4678}
4679
4680#[test]
4681fn import_self() {
4682 check(
4683 indoc! {"
4684 namespace Main {
4685 operation Foo() : Unit {
4686 import Foo;
4687 }
4688 }
4689 "},
4690 &expect![[r#"
4691 namespace namespace3 {
4692 operation package2_item1() : Unit {
4693 import item1 as package2_item2;
4694 }
4695 }
4696 "#]],
4697 );
4698}
4699
4700// imports with the same name create collisions
4701// in the global scope
4702#[test]
4703fn import_duplicate_symbol_in_global_scope() {
4704 check(
4705 indoc! { r#"
4706 namespace Main {
4707 import Foo.Bar.Baz, Foo.Bar.Baz;
4708 }
4709
4710 namespace Foo.Bar {
4711 operation Baz() : Unit {}
4712 }
4713 "# },
4714 &expect![[r#"
4715 namespace namespace3 {
4716 import item4 as package2_item1, item4 as package2_item2;
4717 }
4718
4719 namespace namespace5 {
4720 operation package2_item4() : Unit {}
4721 }
4722
4723 // Duplicate("Baz", "Main", Span { lo: 49, hi: 52 })
4724 "#]],
4725 );
4726}
4727
4728// just like callable and UDT declarations, shadowing is
4729// allowed in local scopes for jupyter cell re-runnability
4730#[test]
4731fn import_duplicate_symbol_in_local_scope() {
4732 check(
4733 indoc! { r#"
4734 namespace Main {
4735 operation Main() : Unit {
4736 import Foo.Bar.Baz, Foo.Bar.Baz;
4737 }
4738 }
4739
4740 namespace Foo.Bar {
4741 operation Baz() : Unit {}
4742 }
4743 "# },
4744 &expect![[r#"
4745 namespace namespace3 {
4746 operation package2_item1() : Unit {
4747 import item3 as package2_item4, item3 as package2_item5;
4748 }
4749 }
4750
4751 namespace namespace5 {
4752 operation package2_item3() : Unit {}
4753 }
4754 "#]],
4755 );
4756}
4757
4758// naming collisions in global scope
4759#[test]
4760fn import_duplicate_symbol_different_source_global_scope() {
4761 check(
4762 indoc! { r#"
4763 namespace Main {
4764 import Foo.Bar.Baz, Foo.Bar;
4765 import Bar.Baz;
4766 }
4767
4768 namespace Foo.Bar {
4769 operation Baz() : Unit {}
4770 }
4771 "# },
4772 &expect![[r#"
4773 namespace namespace3 {
4774 import item5 as package2_item1, namespace5 as package2_item2;
4775 import item5 as package2_item3;
4776 }
4777
4778 namespace namespace5 {
4779 operation package2_item5() : Unit {}
4780 }
4781
4782 // Duplicate("Baz", "Main", Span { lo: 65, hi: 68 })
4783 "#]],
4784 );
4785}
4786
4787// this should be allowed for jupyter cell re-runnability
4788#[test]
4789fn import_duplicate_symbol_different_source_local_scope() {
4790 check(
4791 indoc! { r#"
4792 namespace Main {
4793 operation Main() : Unit {
4794 import Foo.Bar.Baz, Foo.Bar;
4795 import Bar.Baz;
4796 Baz();
4797 }
4798 }
4799
4800 namespace Foo.Bar {
4801 operation Baz() : Unit {}
4802 }
4803 "# },
4804 &expect![[r#"
4805 namespace namespace3 {
4806 operation package2_item1() : Unit {
4807 import item3 as package2_item4, namespace5 as package2_item5;
4808 import item3 as package2_item6;
4809 package2_item3();
4810 }
4811 }
4812
4813 namespace namespace5 {
4814 operation package2_item3() : Unit {}
4815 }
4816 "#]],
4817 );
4818}
4819
4820// this should be allowed for jupyter cell re-runnability
4821#[test]
4822fn disallow_importing_different_items_with_same_name() {
4823 check(
4824 indoc! { r#"
4825 namespace Main {
4826 import Foo.Bar.Baz, Foo.Bar.Baz2 as Baz;
4827 }
4828
4829 namespace Foo.Bar {
4830 operation Baz() : Unit {}
4831 operation Baz2() : Unit {}
4832 }
4833 "# },
4834 &expect![[r#"
4835 namespace namespace3 {
4836 import item4 as package2_item1, item5 as package2_item2;
4837 }
4838
4839 namespace namespace5 {
4840 operation package2_item4() : Unit {}
4841 operation package2_item5() : Unit {}
4842 }
4843
4844 // Duplicate("Baz", "Main", Span { lo: 57, hi: 60 })
4845 "#]],
4846 );
4847}
4848
4849#[test]
4850fn import_takes_precedence_over_local_decl() {
4851 check(
4852 indoc! { r#"
4853 namespace Main {
4854 operation Baz() : Unit {
4855 import Foo.Bar.Baz;
4856 Baz();
4857 }
4858 }
4859
4860 namespace Foo.Bar {
4861 operation Baz() : Unit {}
4862 }
4863 "# },
4864 &expect![[r#"
4865 namespace namespace3 {
4866 operation package2_item1() : Unit {
4867 import item3 as package2_item4;
4868 package2_item3();
4869 }
4870 }
4871
4872 namespace namespace5 {
4873 operation package2_item3() : Unit {}
4874 }
4875 "#]],
4876 );
4877}
4878
4879#[test]
4880fn import_then_export() {
4881 check(
4882 indoc! {"
4883 namespace Foo {
4884 operation Bar() : Unit {}
4885 }
4886 namespace Main {
4887 import Foo.Bar;
4888 export Bar;
4889 }
4890 "},
4891 &expect![[r#"
4892 namespace namespace3 {
4893 operation package2_item1() : Unit {}
4894 }
4895 namespace namespace4 {
4896 import item1 as package2_item3;
4897 export item1 as package2_item4;
4898 }
4899 "#]],
4900 );
4901}
4902
4903#[test]
4904fn import_namespace_advanced() {
4905 check(
4906 indoc! {"
4907 namespace A.B.C.D.E {
4908 operation DumpMachine() : Unit {}
4909 }
4910 namespace TestOne {
4911 import A;
4912 operation Main() : Unit {
4913 A.B.C.D.E.DumpMachine();
4914 }
4915 }
4916 namespace TestTwo {
4917 import A.B;
4918 operation Main() : Unit {
4919 B.C.D.E.DumpMachine();
4920 }
4921 }
4922 namespace TestThree {
4923 import A.B.C;
4924 operation Main() : Unit {
4925 C.D.E.DumpMachine();
4926 }
4927 }
4928 namespace TestFour {
4929 import A.B.C.D;
4930 operation Main() : Unit {
4931 D.E.DumpMachine();
4932 }
4933 }
4934 namespace TestFive {
4935 import A.B.C.D.E;
4936 operation Main() : Unit {
4937 E.DumpMachine();
4938 }
4939 }
4940 namespace TestSix {
4941 import A.B.C.D.E.DumpMachine;
4942 operation Main() : Unit {
4943 DumpMachine();
4944 }
4945 }
4946 "},
4947 &expect![[r#"
4948 namespace namespace7 {
4949 operation package2_item1() : Unit {}
4950 }
4951 namespace namespace8 {
4952 import namespace3 as package2_item3;
4953 operation package2_item4() : Unit {
4954 package2_item1();
4955 }
4956 }
4957 namespace namespace9 {
4958 import namespace4 as package2_item6;
4959 operation package2_item7() : Unit {
4960 package2_item1();
4961 }
4962 }
4963 namespace namespace10 {
4964 import namespace5 as package2_item9;
4965 operation package2_item10() : Unit {
4966 package2_item1();
4967 }
4968 }
4969 namespace namespace11 {
4970 import namespace6 as package2_item12;
4971 operation package2_item13() : Unit {
4972 package2_item1();
4973 }
4974 }
4975 namespace namespace12 {
4976 import namespace7 as package2_item15;
4977 operation package2_item16() : Unit {
4978 package2_item1();
4979 }
4980 }
4981 namespace namespace13 {
4982 import item1 as package2_item18;
4983 operation package2_item19() : Unit {
4984 package2_item1();
4985 }
4986 }
4987 "#]],
4988 );
4989}
4990
4991#[test]
4992fn import_namespace_does_not_open_it() {
4993 check(
4994 indoc! {"
4995 namespace Microsoft.Quantum.Diagnostics {
4996 operation DumpMachine() : Unit {}
4997 }
4998 namespace Main {
4999 import Microsoft.Quantum.Diagnostics;
5000 operation Main() : Unit {
5001 Diagnostics.DumpMachine();
5002 DumpMachine();
5003 }
5004 }
5005 "},
5006 &expect![[r#"
5007 namespace namespace5 {
5008 operation package2_item1() : Unit {}
5009 }
5010 namespace namespace6 {
5011 import namespace5 as package2_item3;
5012 operation package2_item4() : Unit {
5013 package2_item1();
5014 DumpMachine();
5015 }
5016 }
5017
5018 // NotFound("DumpMachine", Span { lo: 214, hi: 225 })
5019 "#]],
5020 );
5021}
5022
5023#[test]
5024fn invalid_import() {
5025 check(
5026 indoc! {"
5027 namespace Main {
5028 import A.B.C;
5029 operation Main() : Unit {
5030 }
5031 }
5032 "},
5033 &expect![[r#"
5034 namespace namespace3 {
5035 import A.B.C as package2_item1;
5036 operation package2_item2() : Unit {
5037 }
5038 }
5039
5040 // NotFound("A.B.C", Span { lo: 28, hi: 33 })
5041 "#]],
5042 );
5043}
5044
5045#[test]
5046fn export_namespace() {
5047 check(
5048 indoc! {"
5049 namespace Foo {
5050 operation ApplyX() : Unit {}
5051 operation ApplyY() : Unit {}
5052 }
5053 namespace Main {
5054 export Foo;
5055 }
5056 namespace Test {
5057 open Main.Foo;
5058 operation Main() : Unit {
5059 ApplyX();
5060 ApplyY();
5061 }
5062 }
5063 "},
5064 &expect![[r#"
5065 namespace namespace3 {
5066 operation package2_item1() : Unit {}
5067 operation package2_item2() : Unit {}
5068 }
5069 namespace namespace4 {
5070 export namespace3 as package2_item4;
5071 }
5072 namespace namespace5 {
5073 open namespace3;
5074 operation package2_item6() : Unit {
5075 package2_item1();
5076 package2_item2();
5077 }
5078 }
5079 "#]],
5080 );
5081}
5082
5083#[test]
5084fn export_namespace_contains_children() {
5085 check(
5086 indoc! {"
5087 namespace Foo.Bar {
5088 operation ApplyX() : Unit {}
5089 }
5090 namespace Main {
5091 export Foo;
5092 }
5093 namespace Test {
5094 open Main.Foo.Bar;
5095 operation Main() : Unit {
5096 ApplyX();
5097 }
5098 }
5099 "},
5100 &expect![[r#"
5101 namespace namespace4 {
5102 operation package2_item1() : Unit {}
5103 }
5104 namespace namespace5 {
5105 export namespace3 as package2_item3;
5106 }
5107 namespace namespace6 {
5108 open namespace4;
5109 operation package2_item5() : Unit {
5110 package2_item1();
5111 }
5112 }
5113 "#]],
5114 );
5115}
5116
5117#[test]
5118fn export_namespace_cyclic() {
5119 check(
5120 indoc! {"
5121 namespace Foo {
5122 export Bar;
5123 }
5124 namespace Bar {
5125 export Foo;
5126 operation Hello() : Unit {}
5127 }
5128 namespace Main {
5129 open Foo.Bar.Foo.Bar.Foo.Bar;
5130 import Foo.Bar.Foo.Bar.Foo.Bar;
5131 operation Main() : Unit { Hello(); Bar.Hello(); }
5132 }
5133 "},
5134 &expect![[r#"
5135 namespace namespace3 {
5136 export namespace4 as package2_item1;
5137 }
5138 namespace namespace4 {
5139 export namespace3 as package2_item3;
5140 operation package2_item4() : Unit {}
5141 }
5142 namespace namespace5 {
5143 open namespace4;
5144 import namespace4 as package2_item6;
5145 operation package2_item7() : Unit { package2_item4(); package2_item4(); }
5146 }
5147 "#]],
5148 );
5149}
5150
5151#[test]
5152fn export_direct_cycle() {
5153 check(
5154 indoc! {"
5155 namespace Foo {
5156 export Foo;
5157 }
5158
5159 namespace Main {
5160 open Foo.Foo.Foo.Foo.Foo;
5161 operation Main() : Unit { }
5162 }
5163 "},
5164 &expect![[r#"
5165 namespace namespace3 {
5166 export namespace3 as package2_item1;
5167 }
5168
5169 namespace namespace4 {
5170 open namespace3;
5171 operation package2_item3() : Unit { }
5172 }
5173 "#]],
5174 );
5175}
5176
5177#[test]
5178fn export_item_cycle() {
5179 check(
5180 indoc! {"
5181 namespace Foo {
5182 export AA as A;
5183 export A as AA;
5184 }
5185 "},
5186 &expect![[r#"
5187 namespace namespace3 {
5188 export AA as as package2_item1;
5189 export A as as package2_item2;
5190 }
5191
5192 // NotFound("AA", Span { lo: 27, hi: 29 })
5193 // NotFound("A", Span { lo: 47, hi: 48 })
5194 "#]],
5195 );
5196}
5197
5198#[test]
5199fn export_namespace_with_alias() {
5200 check(
5201 indoc! {"
5202 namespace Foo.Bar {
5203 operation ApplyX() : Unit {}
5204 }
5205 namespace Main {
5206 export Foo.Bar as Baz;
5207 }
5208 namespace Test {
5209 open Main.Baz;
5210 operation Main() : Unit {
5211 ApplyX();
5212 Main.Baz.ApplyX();
5213 }
5214 }
5215 "},
5216 &expect![[r#"
5217 namespace namespace4 {
5218 operation package2_item1() : Unit {}
5219 }
5220 namespace namespace5 {
5221 export namespace4 as package2_item3;
5222 }
5223 namespace namespace6 {
5224 open namespace4;
5225 operation package2_item5() : Unit {
5226 package2_item1();
5227 package2_item1();
5228 }
5229 }
5230 "#]],
5231 );
5232}
5233
5234#[test]
5235fn import_glob() {
5236 check(
5237 indoc! {"
5238 namespace Foo {
5239 operation ApplyX() : Unit {}
5240 operation ApplyY() : Unit {}
5241 }
5242 namespace Main {
5243 import Foo.*;
5244 operation Main() : Unit {
5245 ApplyX();
5246 ApplyY();
5247 }
5248 }
5249 "},
5250 &expect![[r#"
5251 namespace namespace3 {
5252 operation package2_item1() : Unit {}
5253 operation package2_item2() : Unit {}
5254 }
5255 namespace namespace4 {
5256 import Foo.*;
5257 operation package2_item4() : Unit {
5258 package2_item1();
5259 package2_item2();
5260 }
5261 }
5262 "#]],
5263 );
5264}
5265
5266#[test]
5267fn import_aliased_glob() {
5268 check(
5269 indoc! {"
5270 namespace Foo {
5271 operation ApplyX() : Unit {}
5272 operation ApplyY() : Unit {}
5273 }
5274 namespace Main {
5275 import Foo as Bar;
5276 operation Main() : Unit {
5277 Bar.ApplyX();
5278 Bar.ApplyY();
5279 }
5280 }
5281 "},
5282 &expect![[r#"
5283 namespace namespace3 {
5284 operation package2_item1() : Unit {}
5285 operation package2_item2() : Unit {}
5286 }
5287 namespace namespace4 {
5288 import namespace3 as package2_item4;
5289 operation package2_item5() : Unit {
5290 package2_item1();
5291 package2_item2();
5292 }
5293 }
5294 "#]],
5295 );
5296}
5297
5298#[test]
5299fn import_glob_in_list() {
5300 check(
5301 indoc! {"
5302 namespace Foo.Bar {
5303 operation ApplyX() : Unit {}
5304 operation ApplyY() : Unit {}
5305 }
5306 namespace Foo.Bar.Baz {
5307 operation ApplyZ() : Unit {}
5308 }
5309 namespace Main {
5310 import Foo.Bar.*, Foo.Bar.Baz.ApplyZ;
5311 operation Main() : Unit {
5312 ApplyX();
5313 ApplyY();
5314 Baz.ApplyZ();
5315 ApplyZ();
5316 }
5317 }
5318 "},
5319 &expect![[r#"
5320 namespace namespace4 {
5321 operation package2_item1() : Unit {}
5322 operation package2_item2() : Unit {}
5323 }
5324 namespace namespace5 {
5325 operation package2_item4() : Unit {}
5326 }
5327 namespace namespace6 {
5328 import Foo.Bar.*, item4 as package2_item6;
5329 operation package2_item7() : Unit {
5330 package2_item1();
5331 package2_item2();
5332 package2_item4();
5333 package2_item4();
5334 }
5335 }
5336 "#]],
5337 );
5338}
5339
5340#[test]
5341fn import_glob_in_list_with_alias() {
5342 check(
5343 indoc! {"
5344 namespace Foo.Bar {
5345 operation ApplyX() : Unit {}
5346 operation ApplyY() : Unit {}
5347 }
5348 namespace Foo.Bar.Baz {
5349 operation ApplyZ() : Unit {}
5350 }
5351 namespace Main {
5352 import Foo.Bar as Alias, Foo.Bar.Baz.ApplyZ as Foo;
5353 operation Main() : Unit {
5354 Alias.ApplyX();
5355 Alias.ApplyY();
5356 Alias.Baz.ApplyZ();
5357 Foo();
5358 }
5359 }
5360 "},
5361 &expect![[r#"
5362 namespace namespace4 {
5363 operation package2_item1() : Unit {}
5364 operation package2_item2() : Unit {}
5365 }
5366 namespace namespace5 {
5367 operation package2_item4() : Unit {}
5368 }
5369 namespace namespace6 {
5370 import namespace4 as package2_item6, item4 as package2_item7;
5371 operation package2_item8() : Unit {
5372 package2_item1();
5373 package2_item2();
5374 package2_item4();
5375 package2_item4();
5376 }
5377 }
5378 "#]],
5379 );
5380}
5381
5382#[test]
5383fn import_newtype() {
5384 check(
5385 indoc! {r#"
5386 namespace Foo {
5387 import Bar.NewType; // no error
5388
5389 operation FooOperation() : Unit {
5390 let x: NewType = NewType("a");
5391 }
5392 }
5393
5394 namespace Bar {
5395 newtype NewType = String;
5396 export NewType;
5397 }
5398 "#},
5399 &expect![[r#"
5400 namespace namespace3 {
5401 import item4 as package2_item1; // no error
5402
5403 operation package2_item2() : Unit {
5404 let local17: package2_item4 = package2_item4("a");
5405 }
5406 }
5407
5408 namespace namespace4 {
5409 newtype package2_item4 = String;
5410 export item4 as package2_item5;
5411 }
5412 "#]],
5413 );
5414}
5415
5416#[test]
5417fn glob_import_ns_not_found() {
5418 check(
5419 indoc! {r#"
5420 namespace Main {
5421 import Bar.*;
5422 }
5423 "#},
5424 &expect![[r#"
5425 namespace namespace3 {
5426 import Bar.*;
5427 }
5428
5429 // NotFound("Bar", Span { lo: 28, hi: 31 })
5430 "#]],
5431 );
5432}
5433
5434#[test]
5435fn allow_export_of_namespace_within_itself() {
5436 check(
5437 indoc! {r#"
5438 namespace Foo {
5439 export Foo;
5440 }
5441 "#},
5442 &expect![[r#"
5443 namespace namespace3 {
5444 export namespace3 as package2_item1;
5445 }
5446 "#]],
5447 );
5448}
5449
5450#[test]
5451fn export_of_item_with_same_name_as_namespace_resolves_to_item() {
5452 check(
5453 indoc! {r#"
5454 namespace Foo {
5455 operation Foo() : Unit {}
5456 export Foo;
5457 }
5458 "#},
5459 &expect![[r#"
5460 namespace namespace3 {
5461 operation package2_item1() : Unit {}
5462 export item1 as package2_item2;
5463 }
5464 "#]],
5465 );
5466}
5467
5468#[test]
5469fn export_of_item_with_same_name_as_namespace_resolves_to_item_even_when_before_item() {
5470 check(
5471 indoc! {r#"
5472 namespace Foo {
5473 export Foo;
5474 operation Foo() : Unit {}
5475 }
5476 "#},
5477 &expect![[r#"
5478 namespace namespace3 {
5479 export item2 as package2_item1;
5480 operation package2_item2() : Unit {}
5481 }
5482 "#]],
5483 );
5484}
5485
5486#[test]
5487fn ty_param_name_is_in_scope() {
5488 check(
5489 indoc! {r#"
5490 namespace Foo {
5491 operation Foo<'T: Eq>(a: 'T) : Unit {
5492 let x: 'T = a;
5493 }
5494 }
5495 "#},
5496 &expect![[r#"
5497 namespace namespace3 {
5498 operation package2_item1<param0: Eq>(local10: param0) : Unit {
5499 let local19: param0 = local10;
5500 }
5501 }
5502 "#]],
5503 );
5504}
5505
5506#[test]
5507fn export_namespace_by_name_only() {
5508 check(
5509 indoc! {"
5510 namespace A {
5511 operation B() : Unit {}
5512 }
5513
5514 namespace C {
5515 export A;
5516 }
5517
5518 namespace D {
5519 import C.A as CA;
5520
5521 operation E() : Unit {
5522 C.A.B();
5523 CA.B();
5524 }
5525 }
5526 "},
5527 &expect![[r#"
5528 namespace namespace3 {
5529 operation package2_item1() : Unit {}
5530 }
5531
5532 namespace namespace4 {
5533 export namespace3 as package2_item3;
5534 }
5535
5536 namespace namespace5 {
5537 import namespace3 as package2_item5;
5538
5539 operation package2_item6() : Unit {
5540 package2_item1();
5541 package2_item1();
5542 }
5543 }
5544 "#]],
5545 );
5546}
5547
5548#[test]
5549fn export_comes_before_open() {
5550 check(
5551 indoc! {"
5552 namespace A {
5553 function B() : Unit {}
5554 }
5555
5556 namespace C {
5557 export B;
5558 open A;
5559 }
5560
5561 namespace D {
5562 function E() : Unit {
5563 C.B();
5564 }
5565 }
5566 "},
5567 &expect![[r#"
5568 namespace namespace3 {
5569 function package2_item1() : Unit {}
5570 }
5571
5572 namespace namespace4 {
5573 export item1 as package2_item3;
5574 open namespace3;
5575 }
5576
5577 namespace namespace5 {
5578 function package2_item5() : Unit {
5579 package2_item1();
5580 }
5581 }
5582 "#]],
5583 );
5584}
5585
5586#[test]
5587fn export_comes_before_glob_import() {
5588 check(
5589 indoc! {"
5590 namespace A {
5591 function B() : Unit {}
5592 }
5593
5594 namespace C {
5595 export B;
5596 import A.*;
5597 }
5598
5599 namespace D {
5600 function E() : Unit {
5601 C.B();
5602 }
5603 }
5604 "},
5605 &expect![[r#"
5606 namespace namespace3 {
5607 function package2_item1() : Unit {}
5608 }
5609
5610 namespace namespace4 {
5611 export item1 as package2_item3;
5612 import A.*;
5613 }
5614
5615 namespace namespace5 {
5616 function package2_item5() : Unit {
5617 package2_item1();
5618 }
5619 }
5620 "#]],
5621 );
5622}
5623
5624#[test]
5625fn export_comes_after_reexport() {
5626 check(
5627 indoc! {"
5628 namespace A {
5629 function B() : Unit {}
5630 }
5631
5632 namespace D {
5633 export C.B;
5634 }
5635
5636 namespace C {
5637 export A.B;
5638 }
5639
5640 namespace E {
5641 operation F() : Unit {
5642 D.B();
5643 }
5644 }
5645 "},
5646 &expect![[r#"
5647 namespace namespace3 {
5648 function package2_item1() : Unit {}
5649 }
5650
5651 namespace namespace4 {
5652 export item1 as package2_item3;
5653 }
5654
5655 namespace namespace5 {
5656 export item1 as package2_item5;
5657 }
5658
5659 namespace namespace6 {
5660 operation package2_item7() : Unit {
5661 package2_item1();
5662 }
5663 }
5664 "#]],
5665 );
5666}
5667
5668#[test]
5669fn using_callable_export_as_ty() {
5670 check(
5671 indoc! {"
5672 namespace A {
5673 function B() : Unit {}
5674 }
5675
5676 namespace C {
5677 export A.B;
5678 }
5679
5680 namespace E {
5681 operation F() : Unit {
5682 let x: C.B = 1;
5683 C.B();
5684 }
5685 }
5686 "},
5687 &expect![[r#"
5688 namespace namespace3 {
5689 function package2_item1() : Unit {}
5690 }
5691
5692 namespace namespace4 {
5693 export item1 as package2_item3;
5694 }
5695
5696 namespace namespace5 {
5697 operation package2_item5() : Unit {
5698 let local29: namespace4.B = 1;
5699 package2_item1();
5700 }
5701 }
5702
5703 // NotFound("C.B", Span { lo: 133, hi: 136 })
5704 "#]],
5705 );
5706}
5707
5708#[test]
5709fn unresolved_local_import() {
5710 check(
5711 indoc! {"
5712 namespace Main {
5713 operation Foo(x: Int, y: Bool) : Int {
5714 x
5715 }
5716 export Foo;
5717 }
5718
5719 namespace Bar {
5720 operation Main() : Unit {
5721 import Main.Main.Foo;
5722
5723 operation ZZZ() : Unit {
5724 Foo(10, true);
5725 }
5726 }
5727 }
5728 "},
5729 &expect![[r#"
5730 namespace namespace3 {
5731 operation package2_item1(local8: Int, local13: Bool) : Int {
5732 local8
5733 }
5734 export item1 as package2_item2;
5735 }
5736
5737 namespace namespace4 {
5738 operation package2_item4() : Unit {
5739 import Main.Main.Foo as package2_item5;
5740
5741 operation package2_item6() : Unit {
5742 Foo(10, true);
5743 }
5744 }
5745 }
5746
5747 // NotFound("Main.Main.Foo", Span { lo: 156, hi: 169 })
5748 // NotFound("Foo", Span { lo: 217, hi: 220 })
5749 "#]],
5750 );
5751}
5752
5753#[test]
5754fn wildcard_import_child_namespace() {
5755 check(
5756 indoc! {r#"
5757 namespace Foo.Bar {
5758 operation Hello() : Unit {
5759 }
5760 }
5761
5762 namespace Foo {
5763 import Bar.*;
5764
5765 @EntryPoint()
5766 operation Main() : Unit {
5767 Hello();
5768 }
5769 }
5770 "#},
5771 &expect![[r#"
5772 namespace namespace4 {
5773 operation package2_item1() : Unit {
5774 }
5775 }
5776
5777 namespace namespace3 {
5778 import Bar.*;
5779
5780 @EntryPoint()
5781 operation package2_item3() : Unit {
5782 package2_item1();
5783 }
5784 }
5785 "#]],
5786 );
5787}
5788
5789#[test]
5790fn direct_import_child_namespace() {
5791 check(
5792 indoc! {r#"
5793 namespace Foo.Bar {
5794 operation Hello() : Unit {
5795 }
5796 }
5797
5798 namespace Foo {
5799 import Bar as Baz;
5800
5801 @EntryPoint()
5802 operation Main() : Unit {
5803 Baz.Hello();
5804 }
5805 }
5806 "#},
5807 &expect![[r#"
5808 namespace namespace4 {
5809 operation package2_item1() : Unit {
5810 }
5811 }
5812
5813 namespace namespace3 {
5814 import namespace4 as package2_item3;
5815
5816 @EntryPoint()
5817 operation package2_item4() : Unit {
5818 package2_item1();
5819 }
5820 }
5821 "#]],
5822 );
5823}
5824
5825#[test]
5826fn export_above_declaration() {
5827 check(
5828 indoc! {"
5829 namespace Foo {
5830 export ApplyX;
5831 operation ApplyX() : Unit {}
5832 }
5833 "},
5834 &expect![[r#"
5835 namespace namespace3 {
5836 export item2 as package2_item1;
5837 operation package2_item2() : Unit {}
5838 }
5839 "#]],
5840 );
5841}
5842
5843#[test]
5844fn export_name_collision_separate_namespace_declarations() {
5845 check(
5846 indoc! {"
5847 namespace A {
5848 function Foo() : Unit {}
5849 }
5850 namespace B {
5851 function Foo() : Unit {}
5852 }
5853 namespace C {
5854 export A.Foo;
5855 }
5856 namespace C {
5857 export B.Foo;
5858 }
5859 "},
5860 &expect![[r#"
5861 namespace namespace3 {
5862 function package2_item1() : Unit {}
5863 }
5864 namespace namespace4 {
5865 function package2_item3() : Unit {}
5866 }
5867 namespace namespace5 {
5868 export item1 as package2_item5;
5869 }
5870 namespace namespace5 {
5871 export item3 as package2_item7;
5872 }
5873
5874 // Duplicate("Foo", "C", Span { lo: 151, hi: 154 })
5875 "#]],
5876 );
5877}
5878
5879#[test]
5880fn import_shadowing_local_scope() {
5881 check(
5882 indoc! {"
5883 namespace Foo {
5884 function Bar() : Unit {}
5885 }
5886 namespace Main {
5887 function Bar() : Unit {}
5888 operation Main() : Unit {
5889 import Foo.Bar;
5890 Bar();
5891 }
5892 }
5893 "},
5894 &expect![[r#"
5895 namespace namespace3 {
5896 function package2_item1() : Unit {}
5897 }
5898 namespace namespace4 {
5899 function package2_item3() : Unit {}
5900 operation package2_item4() : Unit {
5901 import item1 as package2_item5;
5902 package2_item1();
5903 }
5904 }
5905 "#]],
5906 );
5907}
5908
5909#[test]
5910fn export_from_local_scope_disallowed() {
5911 check(
5912 indoc! {"
5913 namespace Main {
5914 operation Foo() : Unit {}
5915 operation Main() : Unit {
5916 export Foo;
5917 }
5918 }
5919 "},
5920 &expect![[r#"
5921 namespace namespace3 {
5922 operation package2_item1() : Unit {}
5923 operation package2_item2() : Unit {
5924 export Foo as package2_item3;
5925 }
5926 }
5927
5928 // ExportFromLocalScope(Span { lo: 85, hi: 96 })
5929 "#]],
5930 );
5931}
5932
5933#[test]
5934fn export_item_clobbers_namespace() {
5935 check(
5936 indoc! {"
5937 namespace A.B.C {
5938 operation X() : Unit {}
5939 }
5940 namespace A {
5941 operation D() : Unit {}
5942 export D as B;
5943 }
5944 "},
5945 &expect![[r#"
5946 namespace namespace5 {
5947 operation package2_item1() : Unit {}
5948 }
5949 namespace namespace3 {
5950 operation package2_item3() : Unit {}
5951 export item3 as package2_item4;
5952 }
5953
5954 // Duplicate("B", "A", Span { lo: 106, hi: 107 })
5955 "#]],
5956 );
5957}
5958
5959#[test]
5960fn export_namespace_clobbers_namespace() {
5961 check(
5962 indoc! {"
5963 namespace A.B.C {
5964 operation X() : Unit {}
5965 }
5966 namespace A.D {
5967 operation E() : Unit {}
5968 }
5969 namespace A {
5970 export D as B;
5971 }
5972 "},
5973 &expect![[r#"
5974 namespace namespace5 {
5975 operation package2_item1() : Unit {}
5976 }
5977 namespace namespace6 {
5978 operation package2_item3() : Unit {}
5979 }
5980 namespace namespace3 {
5981 export namespace6 as package2_item5;
5982 }
5983
5984 // Duplicate("B", "A", Span { lo: 124, hi: 125 })
5985 "#]],
5986 );
5987}
5988
5989#[test]
5990fn import_namespace_existing_namespace_collision() {
5991 check(
5992 indoc! {"
5993 namespace A.B {
5994 operation C() : Unit {}
5995 }
5996 namespace A.D {
5997 operation E() : Unit {}
5998 }
5999 namespace A {
6000 import D as B;
6001 operation F (): Unit {
6002 B.E();
6003 B.C();
6004 }
6005 }
6006 "},
6007 &expect![[r#"
6008 namespace namespace4 {
6009 operation package2_item1() : Unit {}
6010 }
6011 namespace namespace5 {
6012 operation package2_item3() : Unit {}
6013 }
6014 namespace namespace3 {
6015 import namespace5 as package2_item5;
6016 operation package2_item6 (): Unit {
6017 B.E();
6018 package2_item1();
6019 }
6020 }
6021
6022 // Duplicate("B", "A", Span { lo: 122, hi: 123 })
6023 // NotFound("B.E", Span { lo: 160, hi: 163 })
6024 "#]],
6025 );
6026}
6027
6028#[test]
6029fn item_name_collision_namespace_part() {
6030 check(
6031 indoc! {"
6032 namespace A.B.C {
6033 }
6034 namespace A {
6035 operation B() : Unit {}
6036 }
6037 "},
6038 &expect![[r#"
6039 namespace namespace5 {
6040 }
6041 namespace namespace3 {
6042 operation package2_item2() : Unit {}
6043 }
6044
6045 // Duplicate("B", "A", Span { lo: 48, hi: 49 })
6046 "#]],
6047 );
6048}
6049
6050#[test]
6051fn chained_import_namespace() {
6052 check(
6053 indoc! {"
6054 namespace A {
6055 operation B() : Unit {}
6056 }
6057 namespace C {
6058 operation D() : Unit {
6059 import A as E;
6060 import E as F;
6061 A.B();
6062 F.B();
6063 }
6064 }
6065 "},
6066 &expect![[r#"
6067 namespace namespace3 {
6068 operation package2_item1() : Unit {}
6069 }
6070 namespace namespace4 {
6071 operation package2_item3() : Unit {
6072 import namespace3 as package2_item4;
6073 import namespace3 as package2_item5;
6074 package2_item1();
6075 package2_item1();
6076 }
6077 }
6078 "#]],
6079 );
6080}
6081
6082#[test]
6083fn export_imported_namespace() {
6084 check(
6085 indoc! {"
6086 namespace A {
6087 operation B() : Unit {}
6088 }
6089 namespace C {
6090 import A as E;
6091 export E as F;
6092 }
6093 namespace G {
6094 operation D() : Unit {
6095 A.B();
6096 C.F.B();
6097 }
6098 }
6099 "},
6100 &expect![[r#"
6101 namespace namespace3 {
6102 operation package2_item1() : Unit {}
6103 }
6104 namespace namespace4 {
6105 import namespace3 as package2_item3;
6106 export namespace3 as package2_item4;
6107 }
6108 namespace namespace5 {
6109 operation package2_item6() : Unit {
6110 package2_item1();
6111 package2_item1();
6112 }
6113 }
6114 "#]],
6115 );
6116}