Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow type aliases in let patterns #6356

Merged
merged 4 commits into from
Oct 25, 2024
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 @@

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 Expand Up @@ -2122,7 +2127,7 @@
}

let recur_on_binding = |id, replacement: &Type| {
// Prevent recuring forever if there's a `T := T` binding

Check warning on line 2130 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (recuring)
if replacement.type_variable_id() == Some(id) {
replacement.clone()
} else {
Expand Down Expand Up @@ -2202,7 +2207,7 @@
Type::Tuple(fields)
}
Type::Forall(typevars, typ) => {
// Trying to substitute_helper a variable de, substitute_bound_typevarsfined within a nested Forall

Check warning on line 2210 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevarsfined)
// is usually impossible and indicative of an error in the type checker somewhere.
for var in typevars {
assert!(!type_bindings.contains_key(&var.id()));
Expand Down Expand Up @@ -2368,7 +2373,7 @@

/// Replace any `Type::NamedGeneric` in this type with a `Type::TypeVariable`
/// using to the same inner `TypeVariable`. This is used during monomorphization
/// to bind to named generics since they are unbindable during type checking.

Check warning on line 2376 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (unbindable)
pub fn replace_named_generics_with_type_variables(&mut self) {
match self {
Type::FieldElement
Expand Down Expand Up @@ -2807,7 +2812,7 @@
len.hash(state);
env.hash(state);
}
Type::Tuple(elems) => elems.hash(state),

Check warning on line 2815 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)

Check warning on line 2815 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)
Type::Struct(def, args) => {
def.hash(state);
args.hash(state);
Expand Down Expand Up @@ -2908,7 +2913,7 @@
// Special case: we consider unbound named generics and type variables to be equal to each
// other if their type variable ids match. This is important for some corner cases in
// monomorphization where we call `replace_named_generics_with_type_variables` but
// still want them to be equal for canonicalization checks in arithmetic generics.

Check warning on line 2916 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (canonicalization)
// Without this we'd fail the `serialize` test.
(
NamedGeneric(lhs_var, _) | TypeVariable(lhs_var),
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 @@ -1976,7 +1976,7 @@
}

// TODO(https://github.com/noir-lang/noir/issues/6238):
// The EvaluatedGlobalIsntU32 warning is a stopgap

Check warning on line 1979 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
// (originally from https://github.com/noir-lang/noir/issues/6125)
#[test]
fn numeric_generic_field_larger_than_u32() {
Expand All @@ -1993,7 +1993,7 @@
assert_eq!(errors.len(), 2);
assert!(matches!(
errors[0].0,
CompilationError::TypeError(TypeCheckError::EvaluatedGlobalIsntU32 { .. }),

Check warning on line 1996 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
));
assert!(matches!(
errors[1].0,
Expand All @@ -2002,7 +2002,7 @@
}

// TODO(https://github.com/noir-lang/noir/issues/6238):
// The EvaluatedGlobalIsntU32 warning is a stopgap

Check warning on line 2005 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
// (originally from https://github.com/noir-lang/noir/issues/6126)
#[test]
fn numeric_generic_field_arithmetic_larger_than_u32() {
Expand Down Expand Up @@ -2031,7 +2031,7 @@

assert!(matches!(
errors[0].0,
CompilationError::TypeError(TypeCheckError::EvaluatedGlobalIsntU32 { .. }),

Check warning on line 2034 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
));

assert!(matches!(
Expand Down Expand Up @@ -3546,3 +3546,19 @@
"#;
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);
}
Loading