Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
annotate each variable declaration with its type (#1069)
Browse files Browse the repository at this point in the history
this solves the problem of type inference in generic functions
  • Loading branch information
temyurchenko authored Jun 6, 2023
1 parent 17c84ab commit bbf4834
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/cairoWriter/writers/variableDeclarationStatementWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { isExternalCall } from '../../utils/utils';
import { CairoASTNodeWriter } from '../base';
import { getDocumentation } from '../utils';
import endent from 'endent';
import { CairoType } from '../../export';

export class VariableDeclarationStatementWriter extends CairoASTNodeWriter {
gapVarCounter = 0;
Expand Down Expand Up @@ -68,17 +69,17 @@ export class VariableDeclarationStatementWriter extends CairoASTNodeWriter {
return [writer.write(getDeclarationForId(id))];
}
});
const solTyp = safeGetNodeType(node.vInitialValue, this.ast.inference);
const cairoTyp = CairoType.fromSol(solTyp, this.ast).toString();
let namePortion: string;
if (declarations.length > 1) {
return [
endent`${documentation}
let (${declarations.map((decl) => `mut ${decl}`).join(', ')}) = ${writer.write(
node.vInitialValue,
)};`,
];
namePortion = '(' + declarations.map((decl) => `mut ${decl}`).join(', ') + ')';
} else {
namePortion = `mut ${declarations[0]}`;
}
return [
endent`${documentation}
let mut ${declarations[0]} = ${writer.write(node.vInitialValue)};`,
let ${namePortion}: ${cairoTyp} = ${writer.write(node.vInitialValue)};`,
];
}
}
1 change: 1 addition & 0 deletions src/utils/importFuncGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const FUNCTION_IMPORTS = [
Paths.ARRAY_TRAIT,
Paths.BOOL_INTO_FELT252,
Paths.FELT252_INTO_BOOL,
Paths.WARPLIB_INTEGER,
Paths.WARP_MEMORY,
Paths.WM_NEW,
Paths.WM_READ,
Expand Down
1 change: 1 addition & 0 deletions src/utils/importPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const WARPLIB_KECCAK = ['warplib', 'keccak'];
export const WARPLIB_MATHS = ['warplib', 'maths'];
export const WARPLIB_CONVERSIONS = ['warplib', 'conversions'];
export const WARPLIB_EXT_INPUT_CHK = ['warplib', 'external_input_check'];
export const WARPLIB_INTEGER = ['warplib', 'integer'];

export const DYNAMIC_ARRAYS_UTIL = ['warplib', 'dynamic_arrays_util'];
export const BYTES_CONVERSIONS = [...WARPLIB_CONVERSIONS, 'bytes_conversions'];
Expand Down
6 changes: 4 additions & 2 deletions tests/compilation/passingContracts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const passingContracts = [
'tests/compilation/contracts/ERC20.sol',
// FIXME: uncomment, when storage is ready for Cairo 1
// 'tests/compilation/contracts/ERC20.sol',
// 'tests/compilation/contracts/ERC20Storage.sol',
'tests/compilation/contracts/address/7/160NotAllowed.sol',
// 'tests/compilation/contracts/address/7/256Address.sol',
Expand Down Expand Up @@ -121,7 +122,8 @@ export const passingContracts = [
// 'tests/compilation/contracts/mutableReferences/scalarStorage.sol',
// 'tests/compilation/contracts/namedArgs/constructor.sol',
// 'tests/compilation/contracts/namedArgs/eventsAndErrors.sol',
'tests/compilation/contracts/namedArgs/function.sol',
// FIXME: uncomment, when storage is ready for Cairo 1
// 'tests/compilation/contracts/namedArgs/function.sol',
// 'tests/compilation/contracts/nestedStaticArrayStruct.sol',
// 'tests/compilation/contracts/nestedStructStaticArray.sol',
'tests/compilation/contracts/nestedStructs.sol',
Expand Down

0 comments on commit bbf4834

Please sign in to comment.