From d8ae907030894d8517d3082f457d3bf31f56ade3 Mon Sep 17 00:00:00 2001 From: Thibaut Schaeffer Date: Mon, 16 Aug 2021 10:44:00 +0200 Subject: [PATCH] Fix constant in struct type (#965) * add breaking test * implement from constant for uint * tweak * tweak * revert and add breaking test * implement from --- zokrates_cli/examples/structs/constant_in_member.zok | 11 +++++++++++ zokrates_cli/src/bin.rs | 6 +++++- zokrates_core/src/typed_absy/types.rs | 9 +++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 zokrates_cli/examples/structs/constant_in_member.zok diff --git a/zokrates_cli/examples/structs/constant_in_member.zok b/zokrates_cli/examples/structs/constant_in_member.zok new file mode 100644 index 00000000..95ea5712 --- /dev/null +++ b/zokrates_cli/examples/structs/constant_in_member.zok @@ -0,0 +1,11 @@ +const u32 SIZE = 42 + +struct Foo { + field[SIZE] foo +} + +def main(): + Foo f = Foo { + foo: [42; SIZE] + } + return \ No newline at end of file diff --git a/zokrates_cli/src/bin.rs b/zokrates_cli/src/bin.rs index 767571a7..c3bb2e05 100644 --- a/zokrates_cli/src/bin.rs +++ b/zokrates_cli/src/bin.rs @@ -126,7 +126,7 @@ mod tests { builder .spawn(|| { - for p in glob("./examples/**/!(*.sh)").expect("Failed to read glob pattern") { + for p in glob("./examples/**/*").expect("Failed to read glob pattern") { let path = match p { Ok(x) => x, Err(why) => panic!("Error: {:?}", why), @@ -136,6 +136,10 @@ mod tests { continue; } + if path.extension().expect("extension expected") == "sh" { + continue; + } + println!("Testing {:?}", path); assert_eq!(path.extension().expect("extension expected"), "zok"); diff --git a/zokrates_core/src/typed_absy/types.rs b/zokrates_core/src/typed_absy/types.rs index 6e9a13db..24d125c4 100644 --- a/zokrates_core/src/typed_absy/types.rs +++ b/zokrates_core/src/typed_absy/types.rs @@ -969,8 +969,13 @@ pub fn check_type<'ast, S: Clone + PartialEq + PartialEq>( } impl<'ast, T> From> for UExpression<'ast, T> { - fn from(_: CanonicalConstantIdentifier<'ast>) -> Self { - unreachable!("constants should have been removed in constant inlining") + fn from(c: CanonicalConstantIdentifier<'ast>) -> Self { + let bitwidth = match *c.ty { + DeclarationType::Uint(bitwidth) => bitwidth, + _ => unreachable!(), + }; + + UExpressionInner::Identifier(Identifier::from(c.id)).annotate(bitwidth) } }