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: handle more types in size_in_fields, and panic on unexpected type #8887

Merged
merged 1 commit into from
Sep 30, 2024
Merged
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
51 changes: 45 additions & 6 deletions noir-projects/aztec-nr/aztec/src/macros/dispatch/mod.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::utils::compute_fn_selector;
use std::panic;

/// Returns an `fn public_dispatch(...)` function for the given module that's assumed to be an Aztec contract.
pub comptime fn generate_public_dispatch(m: Module) -> Quoted {
Expand Down Expand Up @@ -102,13 +103,18 @@ pub comptime fn generate_public_dispatch(m: Module) -> Quoted {
}

comptime fn size_in_fields(typ: Type) -> u32 {
if typ.as_slice().is_some() {
panic(f"Can't determine size in fields of Slice type")
let size = array_size_in_fields(typ);
let size = size.or_else(|| bool_size_in_fields(typ));
let size = size.or_else(|| constant_size_in_fields(typ));
let size = size.or_else(|| field_size_in_fields(typ));
let size = size.or_else(|| int_size_in_fields(typ));
let size = size.or_else(|| str_size_in_fields(typ));
let size = size.or_else(|| struct_size_in_fields(typ));
let size = size.or_else(|| tuple_size_in_fields(typ));
if size.is_some() {
size.unwrap()
} else {
let size = array_size_in_fields(typ);
let size = size.or_else(|| struct_size_in_fields(typ));
let size = size.or_else(|| tuple_size_in_fields(typ));
size.unwrap_or(1)
panic(f"Can't determine size in fields of {typ}")
}
}

Expand All @@ -123,6 +129,38 @@ comptime fn array_size_in_fields(typ: Type) -> Option<u32> {
)
}

comptime fn bool_size_in_fields(typ: Type) -> Option<u32> {
if typ.is_bool() {
Option::some(1)
} else {
Option::none()
}
}

comptime fn field_size_in_fields(typ: Type) -> Option<u32> {
if typ.is_field() {
Option::some(1)
} else {
Option::none()
}
}

comptime fn int_size_in_fields(typ: Type) -> Option<u32> {
if typ.as_integer().is_some() {
Option::some(1)
} else {
Option::none()
}
}

comptime fn constant_size_in_fields(typ: Type) -> Option<u32> {
typ.as_constant()
}

comptime fn str_size_in_fields(typ: Type) -> Option<u32> {
typ.as_str().map(|typ| size_in_fields(typ))
}

comptime fn struct_size_in_fields(typ: Type) -> Option<u32> {
typ.as_struct().map(
|typ: (StructDefinition, [Type])| {
Expand Down Expand Up @@ -152,3 +190,4 @@ comptime fn get_type<T>() -> Type {
let t: T = std::mem::zeroed();
std::meta::type_of(t)
}

Loading