From 391d8a7dc2fc68c73f06fe6be85531542f362709 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Tue, 1 Jul 2025 12:32:24 -0300 Subject: [PATCH 1/2] Revert "fix: codegen generic type arguments (#9044)" This reverts commit b8abf30ba853b26aabc9819d495c9dff7fc7cdc5. --- .../src/utils/abi_type_with_generics.ts | 43 ++++++------------- .../src/utils/typings_generator.ts | 6 ++- tooling/noir_codegen/test/index.test.ts | 18 -------- tooling/noir_codegen/test/test_lib/src/lib.nr | 14 ------ 4 files changed, 16 insertions(+), 65 deletions(-) diff --git a/tooling/noir_codegen/src/utils/abi_type_with_generics.ts b/tooling/noir_codegen/src/utils/abi_type_with_generics.ts index 6e8b5a17a8e..844e116f444 100644 --- a/tooling/noir_codegen/src/utils/abi_type_with_generics.ts +++ b/tooling/noir_codegen/src/utils/abi_type_with_generics.ts @@ -60,63 +60,44 @@ export type AbiTypeWithGenerics = * Maps an ABI type to an ABI type with generics. * This performs pure type conversion, and does not generate any bindings. */ -export function mapAbiTypeToAbiTypeWithGenerics( - abiType: AbiType, - allTypes: AbiTypeWithGenerics[], -): AbiTypeWithGenerics { - let returnedType: AbiTypeWithGenerics; +export function mapAbiTypeToAbiTypeWithGenerics(abiType: AbiType): AbiTypeWithGenerics { switch (abiType.kind) { case 'field': case 'boolean': case 'string': case 'integer': - returnedType = abiType; - break; - case 'array': { - const type = mapAbiTypeToAbiTypeWithGenerics(abiType.type, allTypes); - returnedType = { + return abiType; + case 'array': + return { kind: 'array', length: abiType.length, - type, + type: mapAbiTypeToAbiTypeWithGenerics(abiType.type), }; - break; - } case 'struct': { const structType = { path: abiType.path, - fields: abiType.fields.map(function (field) { - const type = mapAbiTypeToAbiTypeWithGenerics(field.type, allTypes); - return { - name: field.name, - type, - }; - }), + fields: abiType.fields.map((field) => ({ + name: field.name, + type: mapAbiTypeToAbiTypeWithGenerics(field.type), + })), generics: [], }; - returnedType = { + return { kind: 'struct', structType, args: [], }; - break; } case 'tuple': - returnedType = { + return { kind: 'tuple', - fields: abiType.fields.map(function (field) { - const type = mapAbiTypeToAbiTypeWithGenerics(field, allTypes); - allTypes.push(type); - return type; - }), + fields: abiType.fields.map(mapAbiTypeToAbiTypeWithGenerics), }; - break; default: { const exhaustiveCheck: never = abiType; throw new Error(`Unhandled abi type: ${exhaustiveCheck}`); } } - allTypes.push(returnedType); - return returnedType; } /** diff --git a/tooling/noir_codegen/src/utils/typings_generator.ts b/tooling/noir_codegen/src/utils/typings_generator.ts index 886ae8b7c55..ccdaf1dc0a0 100644 --- a/tooling/noir_codegen/src/utils/typings_generator.ts +++ b/tooling/noir_codegen/src/utils/typings_generator.ts @@ -99,11 +99,13 @@ export class TypingsGenerator { // Map all the types used in the ABIs to the demonomorphized types for (const { abi, circuitName, artifact } of circuits) { const params = abi.parameters.map((param) => { - const type = mapAbiTypeToAbiTypeWithGenerics(param.type, this.allTypes); + const type = mapAbiTypeToAbiTypeWithGenerics(param.type); + this.allTypes.push(type); return { name: param.name, type }; }); if (abi.return_type) { - const returnType = mapAbiTypeToAbiTypeWithGenerics(abi.return_type.abi_type, this.allTypes); + const returnType = mapAbiTypeToAbiTypeWithGenerics(abi.return_type.abi_type); + this.allTypes.push(returnType); this.demonomorphizedAbis.push({ circuitName, params, returnType, artifact }); } else { this.demonomorphizedAbis.push({ circuitName, params, artifact }); diff --git a/tooling/noir_codegen/test/index.test.ts b/tooling/noir_codegen/test/index.test.ts index 2442dd3a761..e1e9380a542 100644 --- a/tooling/noir_codegen/test/index.test.ts +++ b/tooling/noir_codegen/test/index.test.ts @@ -4,13 +4,10 @@ import { expect } from 'chai'; import { exported_function_foo, exported_function_baz, - exported_function_parent, MyStruct, u64, u32, ForeignCallHandler, - Parent, - Child, } from './codegen/index.js'; it('codegens a callable function', async () => { @@ -119,18 +116,3 @@ it('codegens a callable argless function', async () => { const val: u64 = await exported_function_baz(); expect(val).to.be.eq('0x01'); }); - -it('codegens types for generic arguments', async () => { - const a: Parent = { - inner: { - value: '1', - }, - counter: '2', - }; - const b: Parent = { - inner: '3', - counter: '4', - }; - const val: u32 = await exported_function_parent(a, b); - expect(val).to.be.eq('0x0a'); // 1 + 2 + 3 + 4 = 10 -}); diff --git a/tooling/noir_codegen/test/test_lib/src/lib.nr b/tooling/noir_codegen/test/test_lib/src/lib.nr index 24d219956b0..8cb37612ca0 100644 --- a/tooling/noir_codegen/test/test_lib/src/lib.nr +++ b/tooling/noir_codegen/test/test_lib/src/lib.nr @@ -36,17 +36,3 @@ fn exported_function_bar(my_struct: NestedStruct<1, 2, 3, u64>) -> (u64) { fn exported_function_baz() -> u64 { 1 } - -pub struct Parent { - inner: T, - counter: u32, -} - -pub struct Child { - value: u32, -} - -#[export] -fn exported_function_parent(a: Parent, b: Parent) -> u32 { - a.counter + a.inner.value + b.counter + b.inner -} From 750512eeffb273db548da1a9eb3faaff2810ee81 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Tue, 1 Jul 2025 12:34:59 -0300 Subject: [PATCH 2/2] Delete exports before running test --- tooling/noir_codegen/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/noir_codegen/package.json b/tooling/noir_codegen/package.json index a16b35ca085..86c9ee556a2 100644 --- a/tooling/noir_codegen/package.json +++ b/tooling/noir_codegen/package.json @@ -35,7 +35,7 @@ "dev": "tsc-multi --watch", "build": "tsc", "test": "yarn test:codegen && yarn test:node && yarn test:clean", - "test:codegen": "${NARGO:-nargo} export --program-dir=./test/test_lib && tsx src/main.ts ./test/test_lib/export/** --out-dir ./test/codegen", + "test:codegen": "rm -rf ./test/test_lib/export && ${NARGO:-nargo} export --program-dir=./test/test_lib && tsx src/main.ts ./test/test_lib/export/** --out-dir ./test/codegen", "test:node": "mocha --timeout 25000 --exit --config ./.mocharc.json", "test:clean": "rm -rf ./test/codegen ./test/test_lib/export", "prettier": "prettier 'src/**/*.ts'",