Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,26 @@ impl<'context> Elaborator<'context> {
}
}

match self.lookup(path) {
Ok(struct_id) => {
let span = path.span;
match self.resolve_path_or_error(path) {
Ok(ModuleDefId::TypeId(struct_id)) => {
let struct_type = self.get_struct(struct_id);
let generics = struct_type.borrow().instantiate(self.interner);
Some(Type::Struct(struct_type, generics))
}
Ok(ModuleDefId::TypeAliasId(alias_id)) => {
let alias = self.interner.get_type_alias(alias_id);
let alias = alias.borrow();
Some(alias.instantiate(self.interner))
}
Ok(other) => {
self.push_err(ResolverError::Expected {
expected: StructId::description(),
got: other.as_str().to_owned(),
span,
});
None
}
Err(error) => {
self.push_err(error);
None
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,11 @@ impl TypeAlias {

self.typ.substitute(&substitutions)
}

pub fn instantiate(&self, interner: &NodeInterner) -> Type {
let args = vecmap(&self.generics, |_| interner.next_type_variable());
self.get_type(&args)
}
}

/// A shared, mutable reference to some T.
Expand Down
16 changes: 16 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3546,3 +3546,19 @@ fn uses_self_in_import() {
"#;
assert_no_errors(src);
}

#[test]
fn alias_in_let_pattern() {
let src = r#"
struct Foo<T> { x: T }

type Bar<U> = Foo<U>;

fn main() {
let Bar { x } = Foo { x: [0] };
// This is just to show the compiler knows this is an array.
let _: [Field; 1] = x;
}
"#;
assert_no_errors(src);
}
19 changes: 0 additions & 19 deletions compiler/noirc_frontend/src/tests/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,3 @@ fn allows_usage_of_type_alias_as_return_type() {
"#;
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);
}