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

Commit 321bfe7

Browse files
esdras-senacicr99
andauthored
replaced CairoAssert by FunctionCall (#1057)
* replaced CairoAssert by FunctionCall * lint fixed * lint fixed * fixed Expression problems * renamed test files * added errorHandling tests to expectedResults * added errorHandling imports * updating * removed unused function * Comment failing test add some comments * Handle assertion functions at ast level --------- Co-authored-by: Carmen Cabrera <[email protected]>
1 parent 74a9b02 commit 321bfe7

19 files changed

+53
-131
lines changed

src/ast/cairoNodes/cairoAssert.ts

-31
This file was deleted.

src/ast/cairoNodes/export.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './cairoAssert';
21
export * from './cairoFunctionDefinition';
32
export * from './cairoContract';
43
export * from './cairoTempVarStatement';

src/ast/cairoNodes/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './cairoAssert';
21
export * from './cairoContract';
32
export * from './cairoGeneratedFunctionDefinition';
43
export * from './cairoFunctionDefinition';

src/ast/visitor.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,7 @@ import {
6565
StatementWithChildren,
6666
ASTNodeWithChildren,
6767
} from 'solc-typed-ast';
68-
import {
69-
CairoAssert,
70-
CairoContract,
71-
CairoFunctionDefinition,
72-
CairoTempVarStatement,
73-
} from './cairoNodes';
68+
import { CairoContract, CairoFunctionDefinition, CairoTempVarStatement } from './cairoNodes';
7469

7570
import { AST } from './ast';
7671
import { CairoGeneratedFunctionDefinition } from './cairoNodes/cairoGeneratedFunctionDefinition';
@@ -106,7 +101,6 @@ export abstract class ASTVisitor<T> {
106101
// Expression
107102
else if (node instanceof Assignment) res = this.visitAssignment(node, ast);
108103
else if (node instanceof BinaryOperation) res = this.visitBinaryOperation(node, ast);
109-
else if (node instanceof CairoAssert) res = this.visitCairoAssert(node, ast);
110104
else if (node instanceof Conditional) res = this.visitConditional(node, ast);
111105
else if (node instanceof ElementaryTypeNameExpression)
112106
res = this.visitElementaryTypeNameExpression(node, ast);
@@ -349,9 +343,6 @@ export abstract class ASTVisitor<T> {
349343
visitSourceUnit(node: SourceUnit, ast: AST): T {
350344
return this.visitASTNodeWithChildren(node, ast);
351345
}
352-
visitCairoAssert(node: CairoAssert, ast: AST): T {
353-
return this.visitExpression(node, ast);
354-
}
355346
visitTypeName(node: TypeName, ast: AST): T {
356347
return this.commonVisit(node, ast);
357348
}

src/cairoWriter/index.ts

-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import {
5858
WhileStatement,
5959
} from 'solc-typed-ast';
6060
import {
61-
CairoAssert,
6261
CairoContract,
6362
CairoFunctionDefinition,
6463
CairoGeneratedFunctionDefinition,
@@ -69,7 +68,6 @@ import {
6968
AssignmentWriter,
7069
BinaryOperationWriter,
7170
BlockWriter,
72-
CairoAssertWriter,
7371
CairoContractWriter,
7472
CairoFunctionDefinitionWriter,
7573
CairoTempVarWriter,
@@ -104,7 +102,6 @@ export const CairoASTMapping = (ast: AST, throwOnUnimplemented: boolean) =>
104102
[BinaryOperation, new BinaryOperationWriter(ast, throwOnUnimplemented)],
105103
[Block, new BlockWriter(ast, throwOnUnimplemented)],
106104
[Break, new NotImplementedWriter(ast, throwOnUnimplemented)],
107-
[CairoAssert, new CairoAssertWriter(ast, throwOnUnimplemented)],
108105
[CairoContract, new CairoContractWriter(ast, throwOnUnimplemented)],
109106
[CairoFunctionDefinition, new CairoFunctionDefinitionWriter(ast, throwOnUnimplemented)],
110107
[

src/cairoWriter/writers/cairoAssertWriter.ts

-11
This file was deleted.

src/cairoWriter/writers/expressionStatementWriter.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
FunctionCallKind,
77
SrcDesc,
88
} from 'solc-typed-ast';
9-
import { CairoAssert } from '../../ast/cairoNodes';
109
import { CairoASTNodeWriter } from '../base';
1110
import { getDocumentation } from '../utils';
1211

@@ -17,8 +16,7 @@ export class ExpressionStatementWriter extends CairoASTNodeWriter {
1716
if (
1817
(node.vExpression instanceof FunctionCall &&
1918
node.vExpression.kind !== FunctionCallKind.StructConstructorCall) ||
20-
node.vExpression instanceof Assignment ||
21-
node.vExpression instanceof CairoAssert
19+
node.vExpression instanceof Assignment
2220
) {
2321
return [[documentation, `${writer.write(node.vExpression)};`].join('\n')];
2422
} else {

src/cairoWriter/writers/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export * from './assignmentWriter';
22
export * from './binaryOperationWriter';
33
export * from './blockWriter';
4-
export * from './cairoAssertWriter';
54
export * from './cairoContractWriter';
65
export * from './cairoFunctionDefinitionWriter';
76
export * from './cairoGeneratedFunctionDefinitionWriter';

src/passes/builtinHandler/require.ts

+39-29
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import {
44
ExpressionStatement,
55
ExternalReferenceType,
66
FunctionCall,
7+
Identifier,
78
Literal,
9+
LiteralKind,
810
Return,
911
} from 'solc-typed-ast';
1012
import { AST } from '../../ast/ast';
11-
import { CairoAssert } from '../../ast/cairoNodes';
1213
import { ASTMapper } from '../../ast/mapper';
14+
import { cloneASTNode } from '../../utils/cloning';
1315
import { createBoolLiteral } from '../../utils/nodeTemplates';
16+
import { toHexString } from '../../utils/utils';
1417

1518
export class Require extends ASTMapper {
1619
// Function to add passes that should have been run before this pass
@@ -28,7 +31,7 @@ export class Require extends ASTMapper {
2831
return;
2932
}
3033

31-
// Since the cairoAssert is not null, we have a require/revert/assert function call at hand
34+
// Since cairoAssert is not null, this is a require/revert/assert function call
3235
assert(expressionNode instanceof FunctionCall);
3336

3437
ast.replaceNode(node, cairoAssert);
@@ -44,42 +47,49 @@ export class Require extends ASTMapper {
4447
}
4548

4649
requireToCairoAssert(expression: Expression | undefined, ast: AST): ExpressionStatement | null {
47-
if (!(expression instanceof FunctionCall)) return null;
48-
if (expression.vFunctionCallType !== ExternalReferenceType.Builtin) {
49-
return null;
50-
}
50+
if (
51+
expression instanceof FunctionCall &&
52+
expression.vFunctionCallType === ExternalReferenceType.Builtin &&
53+
['assert', 'require', 'revert'].includes(expression.vIdentifier)
54+
) {
55+
// TODO: The identifier node generated by the solc-typed-ast has different typestrings
56+
// and referencedDeclaration number for assert, require and revert Solidity functions.
57+
// Check typestring when updating solc-typed-ast version.
58+
const assertIdentifier = cloneASTNode(expression.vExpression, ast);
59+
assert(assertIdentifier instanceof Identifier);
60+
assertIdentifier.name = 'assert';
5161

52-
if (expression.vIdentifier === 'require' || expression.vIdentifier === 'assert') {
53-
const requireMessage =
54-
expression.vArguments[1] instanceof Literal ? expression.vArguments[1].value : null;
62+
const args: Expression[] = [];
63+
if (expression.vIdentifier === 'revert') args.push(createBoolLiteral(false, ast));
64+
args.push(...expression.vArguments);
65+
if (args.length < 2) {
66+
const message = 'Assertion error';
67+
args.push(
68+
new Literal(
69+
ast.reserveId(),
70+
'',
71+
`literal_string "${message}"`,
72+
LiteralKind.String,
73+
toHexString(message),
74+
message,
75+
),
76+
);
77+
}
5578

5679
return new ExpressionStatement(
5780
ast.reserveId(),
5881
expression.src,
59-
new CairoAssert(
82+
new FunctionCall(
6083
ast.reserveId(),
61-
expression.src,
62-
expression.vArguments[0],
63-
requireMessage,
64-
expression.raw,
65-
),
66-
);
67-
} else if (expression.vIdentifier === 'revert') {
68-
const revertMessage =
69-
expression.vArguments[0] instanceof Literal ? expression.vArguments[0].value : null;
70-
71-
return new ExpressionStatement(
72-
ast.reserveId(),
73-
expression.src,
74-
new CairoAssert(
75-
ast.reserveId(),
76-
expression.src,
77-
createBoolLiteral(false, ast),
78-
revertMessage,
79-
expression.raw,
84+
'',
85+
expression.typeString,
86+
expression.kind,
87+
assertIdentifier,
88+
args,
8089
),
8190
);
8291
}
92+
8393
return null;
8494
}
8595
}

src/passes/references/expectedLocationAnalyser.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
VariableDeclarationStatement,
2525
} from 'solc-typed-ast';
2626
import { AST } from '../../ast/ast';
27-
import { CairoAssert } from '../../ast/cairoNodes';
2827
import { ASTMapper } from '../../ast/mapper';
2928
import { locationIfComplexType } from '../../cairoUtilFuncGen/base';
3029
import { printNode } from '../../utils/astPrinter';
@@ -139,9 +138,9 @@ export class ExpectedLocationAnalyser extends ASTMapper {
139138

140139
const parameterTypes = getParameterTypes(node, ast);
141140
// When calling `push`, the function receives two parameters nonetheless the argument is just one
142-
// This does not explode because javascript does not gives an index out of range exception
141+
// This does not explode because javascript does not give an index out of range exception
143142
node.vArguments.forEach((arg, index) => {
144-
// Solc 0.7.0 types push and pop as you would expect, 0.8.0 adds an extra initial argument
143+
// Solc 0.7.0 types push and pop as expected, 0.8.0 adds an extra initial argument
145144
const paramIndex = index + parameterTypes.length - node.vArguments.length;
146145
const t = parameterTypes[paramIndex];
147146
if (t instanceof PointerType) {
@@ -320,11 +319,6 @@ export class ExpectedLocationAnalyser extends ASTMapper {
320319
this.visitStatement(node, ast);
321320
}
322321

323-
visitCairoAssert(node: CairoAssert, ast: AST): void {
324-
this.expectedLocations.set(node.vExpression, DataLocation.Default);
325-
this.visitExpression(node, ast);
326-
}
327-
328322
visitIfStatement(node: IfStatement, ast: AST): void {
329323
this.expectedLocations.set(node.vCondition, DataLocation.Default);
330324
this.visitStatement(node, ast);

src/solWriter.ts

-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
SrcDesc,
1010
} from 'solc-typed-ast';
1111
import {
12-
CairoAssert,
1312
CairoContract,
1413
CairoFunctionDefinition,
1514
CairoGeneratedFunctionDefinition,
@@ -108,21 +107,12 @@ class CairoImportFunctionDefinitionSolWriter extends ASTNodeWriter {
108107
}
109108
}
110109

111-
class CairoAssertSolWriter extends ASTNodeWriter {
112-
writeInner(node: CairoAssert, writer: ASTWriter): SrcDesc {
113-
const result: SrcDesc = [];
114-
result.push(`<cairo information> assert ${writer.write(node.vExpression)} = 1`);
115-
return result;
116-
}
117-
}
118-
119110
const CairoExtendedASTWriterMapping = (printStubs: boolean) =>
120111
new Map<ASTNodeConstructor<ASTNode>, ASTNodeWriter>([
121112
[CairoContract, new CairoContractSolWriter()],
122113
[CairoFunctionDefinition, new CairoFunctionDefinitionSolWriter(printStubs)],
123114
[CairoGeneratedFunctionDefinition, new CairoGeneratedFunctionDefinitionSolWriter()],
124115
[CairoImportFunctionDefinition, new CairoImportFunctionDefinitionSolWriter()],
125-
[CairoAssert, new CairoAssertSolWriter()],
126116
]);
127117

128118
export const CairoToSolASTWriterMapping = (printStubs: boolean) =>

src/utils/astChecking.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ import {
6464
} from 'solc-typed-ast';
6565
import { pp } from 'solc-typed-ast/dist/misc/index';
6666
import { AST } from '../ast/ast';
67-
import { CairoAssert, CairoTempVarStatement } from '../ast/cairoNodes';
67+
import { CairoTempVarStatement } from '../ast/cairoNodes';
6868
import { ASTMapper } from '../ast/mapper';
6969
import { printNode } from './astPrinter';
7070
import { InsaneASTError } from './errors';
7171
import { safeGetNodeType } from './nodeTypeProcessing';
7272
import { isNameless } from './utils';
7373

74-
// This is the solc-typed-ast AST checking code, with additions for CairoAssert and CairoContract
74+
// This is the solc-typed-ast AST checking code, with additions for CairoContract
7575

7676
/**
7777
* Helper function to check if the node/nodes `arg` is in the `ASTContext` `ctx`.
@@ -667,8 +667,6 @@ export function checkSane(unit: SourceUnit, ctx: ASTContext): void {
667667
} else if (node instanceof UnaryOperation) {
668668
checkVFieldCtx(node, 'vSubExpression', ctx);
669669
checkDirectChildren(node, 'vSubExpression');
670-
} else if (node instanceof CairoAssert) {
671-
checkDirectChildren(node, 'vExpression');
672670
} else if (node instanceof CairoTempVarStatement) {
673671
// Not being checked because this node does not get affected by any
674672
// other ast pass

src/utils/cloning.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import {
4444
WhileStatement,
4545
} from 'solc-typed-ast';
4646
import { AST } from '../ast/ast';
47-
import { CairoAssert, CairoFunctionDefinition } from '../ast/cairoNodes';
47+
import { CairoFunctionDefinition } from '../ast/cairoNodes';
4848
import { printNode } from './astPrinter';
4949
import { NotSupportedYetError, TranspileFailedError } from './errors';
5050
import { createParameterList } from './nodeTemplates';
@@ -104,14 +104,6 @@ function cloneASTNodeImpl<T extends ASTNode>(
104104
cloneASTNodeImpl(node.vRightExpression, ast, remappedIds),
105105
node.raw,
106106
);
107-
} else if (node instanceof CairoAssert) {
108-
newNode = new CairoAssert(
109-
replaceId(node.id, ast, remappedIds),
110-
node.src,
111-
cloneASTNodeImpl(node.vExpression, ast, remappedIds),
112-
node.assertMessage,
113-
node.raw,
114-
);
115107
} else if (node instanceof ElementaryTypeNameExpression) {
116108
newNode = new ElementaryTypeNameExpression(
117109
replaceId(node.id, ast, remappedIds),

src/utils/nodeTypeProcessing.ts

-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import { TranspileFailedError } from './errors';
3838
import { error } from './formatting';
3939
import { getContainingSourceUnit } from './utils';
4040
import { getNodeType, getNodeTypeInCtx } from './typeStrings/typeStringParser';
41-
import { CairoAssert } from '../ast/cairoNodes'; // eslint-disable-line
4241

4342
/*
4443
Normal function calls and struct constructors require different methods for
@@ -298,9 +297,6 @@ export function safeGetNodeType(
298297
// if (node instanceof Literal) {
299298
// return getNodeType(node, inference);
300299
// }
301-
// if (node instanceof CairoAssert){
302-
// return new TupleType([]);
303-
// }
304300
// if (node instanceof VariableDeclaration) {
305301
// return inference.variableDeclarationToTypeNode(node);
306302
// }

tests/compilation/compilation.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ const expectedResults = new Map<string, ResultType>(
4545
['deleteUses.sol', 'Success'],
4646
['enums.sol', 'Success'],
4747
['enums7.sol', 'Success'],
48-
['errorHandling/assert.sol', 'Success'],
49-
['errorHandling/require.sol', 'Success'],
50-
['errorHandling/revert.sol', 'Success'],
48+
['errorHandling/assertHandling.sol', 'Success'],
49+
['errorHandling/requireHandling.sol', 'Success'],
50+
['errorHandling/revertHandling.sol', 'Success'],
5151
['events.sol', 'Success'],
5252
['expressionSplitter/assignSimple.sol', 'Success'],
5353
['expressionSplitter/funcCallSimple.sol', 'Success'],

tests/compilation/passingContracts.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ export const passingContracts = [
3535
// 'tests/compilation/contracts/deleteUses.sol',
3636
'tests/compilation/contracts/enums.sol',
3737
// 'tests/compilation/contracts/enums7.sol',
38-
// 'tests/compilation/contracts/errorHandling/assert.sol',
39-
// 'tests/compilation/contracts/errorHandling/require.sol',
40-
// 'tests/compilation/contracts/errorHandling/revert.sol',
38+
'tests/compilation/contracts/errorHandling/assertHandling.sol',
39+
'tests/compilation/contracts/errorHandling/requireHandling.sol',
40+
// TODO: uncomment, when input checks are handled
41+
// 'tests/compilation/contracts/errorHandling/revertHandling.sol',
4142
// 'tests/compilation/contracts/events.sol',
4243
// 'tests/compilation/contracts/expressionSplitter/assignSimple.sol',
4344
// 'tests/compilation/contracts/expressionSplitter/funcCallSimple.sol',

0 commit comments

Comments
 (0)