Skip to content

Commit

Permalink
fix: search all levels of struct nesting before codegenning primitive…
Browse files Browse the repository at this point in the history
… 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 <[email protected]>
  • Loading branch information
TomAFrench and kevaundray authored Jan 8, 2024
1 parent f1de8fa commit 13ae014
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions tooling/noir_codegen/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ crs
lib

test/codegen
test/test_lib/export
4 changes: 2 additions & 2 deletions tooling/noir_codegen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ export const codegen = (programs: [string, CompiledCircuit][]): string => {
functions.push(codegenFunction(name, stripUnwantedFields(program), function_sig));
}

const structTypeDefinitions: string = codegenStructDefinitions(structTypeMap, primitiveTypeMap);

// Add the primitive Noir types that do not have a 1-1 mapping to TypeScript.
const primitiveTypeAliases: string[] = [];
for (const value of primitiveTypeMap.values()) {
primitiveTypeAliases.push(`export type ${value.aliasName} = ${value.tsType};`);
}

const structTypeDefinitions: string = codegenStructDefinitions(structTypeMap, primitiveTypeMap);

results = results.concat(...primitiveTypeAliases, '', structTypeDefinitions, ...functions);

return results.join('\n');
Expand Down
8 changes: 4 additions & 4 deletions tooling/noir_codegen/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect } from 'chai';
import { exported_function_foo, MyStruct, u64, ForeignCallHandler } from './codegen/index.js';

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

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

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

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

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

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

expect(sum).to.be.eq('0x05');
expect(constant).to.be.eq('0x03');
expect(struct).to.be.deep.eq({ foo: true, bar: ['12345', '12345', '12345'] });
expect(struct).to.be.deep.eq(my_struct);
});
1 change: 1 addition & 0 deletions tooling/noir_codegen/test/test_lib/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
struct MyStruct {
foo: bool,
bar: [str<5>; 3],
baz: Field
}

struct NestedStruct {
Expand Down

0 comments on commit 13ae014

Please sign in to comment.