Skip to content

Commit 13ae014

Browse files
fix: search all levels of struct nesting before codegenning primitive types (#3970)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* We previously had a bug that if a primitive type only exists below at least 2 layers of nesting in the function arguments then we wouldn't generate a definition for it. We now search the entire depth of nested structs before generating the primitive type definitions. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: kevaundray <kevtheappdev@gmail.com>
1 parent f1de8fa commit 13ae014

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

tooling/noir_codegen/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ crs
22
lib
33

44
test/codegen
5+
test/test_lib/export

tooling/noir_codegen/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ export const codegen = (programs: [string, CompiledCircuit][]): string => {
4646
functions.push(codegenFunction(name, stripUnwantedFields(program), function_sig));
4747
}
4848

49+
const structTypeDefinitions: string = codegenStructDefinitions(structTypeMap, primitiveTypeMap);
50+
4951
// Add the primitive Noir types that do not have a 1-1 mapping to TypeScript.
5052
const primitiveTypeAliases: string[] = [];
5153
for (const value of primitiveTypeMap.values()) {
5254
primitiveTypeAliases.push(`export type ${value.aliasName} = ${value.tsType};`);
5355
}
5456

55-
const structTypeDefinitions: string = codegenStructDefinitions(structTypeMap, primitiveTypeMap);
56-
5757
results = results.concat(...primitiveTypeAliases, '', structTypeDefinitions, ...functions);
5858

5959
return results.join('\n');

tooling/noir_codegen/test/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'chai';
44
import { exported_function_foo, MyStruct, u64, ForeignCallHandler } from './codegen/index.js';
55

66
it('codegens a callable function', async () => {
7-
const my_struct = { foo: true, bar: ['12345', '12345', '12345'] };
7+
const my_struct = { foo: true, bar: ['12345', '12345', '12345'], baz: '0x00' };
88

99
const [sum, constant, struct]: [u64, u64, MyStruct] = await exported_function_foo(
1010
'2',
@@ -20,7 +20,7 @@ it('codegens a callable function', async () => {
2020

2121
expect(sum).to.be.eq('0x05');
2222
expect(constant).to.be.eq('0x03');
23-
expect(struct).to.be.deep.eq({ foo: true, bar: ['12345', '12345', '12345'] });
23+
expect(struct).to.be.deep.eq(my_struct);
2424
});
2525

2626
it('allows passing a custom foreign call handler', async () => {
@@ -35,7 +35,7 @@ it('allows passing a custom foreign call handler', async () => {
3535
return [];
3636
};
3737

38-
const my_struct = { foo: true, bar: ['12345', '12345', '12345'] };
38+
const my_struct = { foo: true, bar: ['12345', '12345', '12345'], baz: '0x00' };
3939

4040
const [sum, constant, struct]: [u64, u64, MyStruct] = await exported_function_foo(
4141
'2',
@@ -102,5 +102,5 @@ it('allows passing a custom foreign call handler', async () => {
102102

103103
expect(sum).to.be.eq('0x05');
104104
expect(constant).to.be.eq('0x03');
105-
expect(struct).to.be.deep.eq({ foo: true, bar: ['12345', '12345', '12345'] });
105+
expect(struct).to.be.deep.eq(my_struct);
106106
});

tooling/noir_codegen/test/test_lib/src/lib.nr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
struct MyStruct {
22
foo: bool,
33
bar: [str<5>; 3],
4+
baz: Field
45
}
56

67
struct NestedStruct {

0 commit comments

Comments
 (0)