Skip to content

Commit

Permalink
fix: handle more types in size_in_fields, and panic on unexpected type (
Browse files Browse the repository at this point in the history
#8887)

Please read [contributing guidelines](CONTRIBUTING.md) and remove this line.
  • Loading branch information
asterite authored Sep 30, 2024
1 parent 9cd450e commit 03280e9
Showing 1 changed file with 45 additions and 6 deletions.
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)
}

0 comments on commit 03280e9

Please sign in to comment.