From 600ffeb05cd2538604570f3e2aa1b2e560e577c3 Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 24 Oct 2024 22:47:40 +0100 Subject: [PATCH] chore: add some tests for type aliases --- compiler/noirc_frontend/src/tests.rs | 1 + compiler/noirc_frontend/src/tests/aliases.rs | 52 ++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 compiler/noirc_frontend/src/tests/aliases.rs diff --git a/compiler/noirc_frontend/src/tests.rs b/compiler/noirc_frontend/src/tests.rs index 286986e5d61..e06881127fd 100644 --- a/compiler/noirc_frontend/src/tests.rs +++ b/compiler/noirc_frontend/src/tests.rs @@ -1,5 +1,6 @@ #![cfg(test)] +mod aliases; mod bound_checks; mod imports; mod metaprogramming; diff --git a/compiler/noirc_frontend/src/tests/aliases.rs b/compiler/noirc_frontend/src/tests/aliases.rs new file mode 100644 index 00000000000..2ca460ca635 --- /dev/null +++ b/compiler/noirc_frontend/src/tests/aliases.rs @@ -0,0 +1,52 @@ +use super::assert_no_errors; + +#[test] +fn allows_usage_of_type_alias_as_argument_type() { + let src = r#" + type Foo = Field; + + fn accepts_a_foo(x: Foo) { + assert_eq(x, 42); + } + + fn main() { + accepts_a_foo(42); + } + "#; + assert_no_errors(src); +} + +#[test] +fn allows_usage_of_type_alias_as_return_type() { + let src = r#" + type Foo = Field; + + fn returns_a_foo() -> Foo { + 42 + } + + fn main() { + let _ = returns_a_foo(); + } + "#; + assert_no_errors(src); +} + +// This is a regression test for https://github.com/noir-lang/noir/issues/6347 +#[test] +#[should_panic = r#"ResolverError(Expected { span: Span(Span { start: ByteIndex(95), end: ByteIndex(98) }), expected: "type", got: "type alias" }"#] +fn allows_destructuring_a_type_alias_of_a_struct() { + let src = r#" + struct Foo { + inner: Field + } + + type Bar = Foo; + + fn main() { + let Bar { inner } = Foo { inner: 42 }; + assert_eq(inner, 42); + } + "#; + assert_no_errors(src); +}