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

feat(experimental): Support struct constructors in match patterns #7489

Merged
merged 4 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
83 changes: 80 additions & 3 deletions compiler/noirc_frontend/src/elaborator/enums.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::collections::BTreeMap;

use fm::FileId;
use fxhash::FxHashMap as HashMap;
use iter_extended::{try_vecmap, vecmap};
use noirc_errors::Location;

use crate::{
ast::{
EnumVariant, Expression, ExpressionKind, FunctionKind, Ident, Literal, NoirEnumeration,
StatementKind, UnresolvedType, Visibility,
ConstructorExpression, EnumVariant, Expression, ExpressionKind, FunctionKind, Ident,
Literal, NoirEnumeration, StatementKind, UnresolvedType, Visibility,
},
elaborator::path_resolution::PathResolutionItem,
hir::{comptime::Value, resolution::errors::ResolverError, type_check::TypeCheckError},
Expand Down Expand Up @@ -376,7 +378,9 @@
expected_type,
variables_defined,
),
ExpressionKind::Constructor(_) => todo!("handle constructors"),
ExpressionKind::Constructor(constructor) => {
self.constructor_to_pattern(*constructor, variables_defined)
}
ExpressionKind::Tuple(fields) => {
let field_types = vecmap(0..fields.len(), |_| self.interner.next_type_variable());
let actual = Type::Tuple(field_types.clone());
Expand Down Expand Up @@ -431,6 +435,60 @@
}
}

fn constructor_to_pattern(
&mut self,
constructor: ConstructorExpression,
variables_defined: &mut Vec<Ident>,
) -> Pattern {
let location = constructor.typ.location;
let typ = match constructor.struct_type {
Some(id) => {
let typ = self.interner.get_type(id);
let generics = typ.borrow().instantiate(self.interner);
Type::DataType(typ, generics)
}
None => self.resolve_type(constructor.typ),
};

let Some((struct_name, mut expected_field_types)) =
self.struct_name_and_field_types(&typ, location)
else {
return Pattern::Error;
};

let mut fields = BTreeMap::default();
for (field_name, field) in constructor.fields {
let Some(field_index) =
expected_field_types.iter().position(|(name, _)| *name == field_name.0.contents)
else {
let error = if fields.contains_key(&field_name.0.contents) {
ResolverError::DuplicateField { field: field_name }
} else {
let struct_definition = struct_name.clone();
ResolverError::NoSuchField { field: field_name, struct_definition }
};
self.push_err(error, self.file);
continue;
};

let (field_name, expected_field_type) = expected_field_types.swap_remove(field_index);
let pattern =
self.expression_to_pattern(field, &expected_field_type, variables_defined);
fields.insert(field_name, pattern);
}

if !expected_field_types.is_empty() {
let struct_definition = struct_name;
let span = location.span;
let missing_fields = vecmap(expected_field_types, |(name, _)| name);
let error = ResolverError::MissingFields { span, missing_fields, struct_definition };
self.push_err(error, self.file);
}

let args = vecmap(fields, |(_name, field)| field);
Pattern::Constructor(Constructor::Variant(typ, 0), args)
}

fn expression_to_constructor(
&mut self,
name: Expression,
Expand Down Expand Up @@ -552,6 +610,23 @@
Pattern::Constructor(constructor, args)
}

fn struct_name_and_field_types(
&mut self,
typ: &Type,
location: Location,
) -> Option<(Ident, Vec<(String, Type)>)> {
if let Type::DataType(typ, generics) = typ.follow_bindings_shallow().as_ref() {
if let Some(fields) = typ.borrow().get_fields(generics) {
return Some((typ.borrow().name.clone(), fields));
}
}

let error =
ResolverError::NonStructUsedInConstructor { typ: typ.to_string(), span: location.span };
self.push_err(error, location.file);
None
}

/// Compiles the rows of a match expression, outputting a decision tree for the match.
///
/// This is an adaptation of https://github.com/yorickpeterse/pattern-matching-in-rust/tree/main/jacobs2021
Expand Down Expand Up @@ -687,7 +762,7 @@

/// Compiles the cases and fallback cases for integer and range patterns.
///
/// Integers have an infinite number of constructors, so we specialise the

Check warning on line 765 in compiler/noirc_frontend/src/elaborator/enums.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (specialise)
/// compilation of integer and range patterns.
fn compile_int_cases(
&mut self,
Expand Down Expand Up @@ -931,6 +1006,8 @@
/// 1 <= n < 20.
#[allow(unused)]
Range(SignedField, SignedField),

Error,
}

#[derive(Clone)]
Expand Down
27 changes: 26 additions & 1 deletion test_programs/compile_success_empty/enums/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ fn main() {
foo_tests();
option_tests();
abc_tests();
match_on_structs();
}

fn primitive_tests() {
Expand Down Expand Up @@ -103,7 +104,7 @@ fn abc_tests() {
// Mut is only to throw the optimizer off a bit so we can see
// the `eq`s that get generated before they're removed because each of these are constant
let mut tuple = (ABC::A, ABC::B);
match tuple {
let _ = match tuple {
(ABC::A, _) => 1,
(_, ABC::A) => 2,
(_, ABC::B) => 3,
Expand All @@ -114,3 +115,27 @@ fn abc_tests() {
_ => 0,
};
}

fn match_on_structs() {
let foo = MyStruct { x: 10, y: 20 };
match foo {
MyStruct { x, y } => {
assert_eq(x, 10);
assert_eq(y, 20);
},
}

match MyOption::Some(foo) {
MyOption::Some(MyStruct { x: x2, y: y2 }) => {
assert_eq(x2, 10);
assert_eq(y2, 20);
},
MyOption::None => fail(),
MyOption::Maybe => fail(),
}
}

struct MyStruct {
x: i32,
y: Field,
}
Loading