Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check map types for deepEquals method #1718

Merged
merged 5 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev-docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed the notion of the non-standard TL-B syntax `remainder<X>`: PR [#1599](https://github.com/tact-lang/tact/pull/1599)
- Added description of `.boc`, `.ts`, `.abi`, `.pkg` files and completed Compilation page: PR [#1676](https://github.com/tact-lang/tact/pull/1676)
- Marked gas-expensive functions and expressions: PR [#1703](https://github.com/tact-lang/tact/pull/1703)
- Check map types for `deepEquals` method: PR [#1718](https://github.com/tact-lang/tact/pull/1718)

### Release contributors

Expand Down
10 changes: 9 additions & 1 deletion src/abi/map.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { CompilerContext } from "../context/context";
import { SrcInfo } from "../grammar";
import { TypeRef } from "../types/types";
import { printTypeRef, TypeRef } from "../types/types";
import { WriterContext } from "../generator/Writer";
import { ops } from "../generator/writers/ops";
import { writeExpression } from "../generator/writers/writeExpression";
import { throwCompilationError } from "../error/errors";
import { getType } from "../types/resolveDescriptors";
import { AbiFunction } from "./AbiFunction";
import { AstExpression } from "../ast/ast";
import { isAssignable } from "../types/subtyping";

// Helper functions to avoid redundancy
function checkArgumentsLength(
Expand Down Expand Up @@ -492,6 +493,13 @@ export const MapFunctions: ReadonlyMap<string, AbiFunction> = new Map([
checkMapType(self, ref);
checkMapType(other, ref);

if (!isAssignable(self, other)) {
throwCompilationError(
`Type mismatch: cannot pass argument of type "${printTypeRef(other)}" to parameter of type "${printTypeRef(self)}"`,
ref,
);
}

return { kind: "ref", name: "Bool", optional: false };
},
generate(
Expand Down
45 changes: 45 additions & 0 deletions src/types/__snapshots__/resolveStatements.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,51 @@ exports[`resolveStatements should fail statements for init-vars-analysis-with-if
"
`;

exports[`resolveStatements should fail statements for map-deepEquals-type-mismatch 1`] = `
"<unknown>:12:16: Type mismatch: cannot pass argument of type "map<Address, Int>" to parameter of type "map<Int, Int>"
11 | let m2: map<Address, Int> = emptyMap();
> 12 | return m1.deepEquals(m2);
^~~~~~~~~~~~~~~~~
13 | }
"
`;

exports[`resolveStatements should fail statements for map-deepEquals-type-mismatch-2 1`] = `
"<unknown>:12:16: Type mismatch: cannot pass argument of type "map<Address, Address>" to parameter of type "map<Address, Int>"
11 | let m2: map<Address, Address> = emptyMap();
> 12 | return m1.deepEquals(m2);
^~~~~~~~~~~~~~~~~
13 | }
"
`;

exports[`resolveStatements should fail statements for map-deepEquals-type-mismatch-3 1`] = `
"<unknown>:12:16: Type mismatch: cannot pass argument of type "map<Int, Address>" to parameter of type "map<Int, Bool>"
11 | let m2: map<Int, Address> = emptyMap();
> 12 | return m1.deepEquals(m2);
^~~~~~~~~~~~~~~~~
13 | }
"
`;

exports[`resolveStatements should fail statements for map-deepEquals-type-mismatch-4 1`] = `
"<unknown>:12:16: Type mismatch: cannot pass argument of type "map<Int as uint16, Bool>" to parameter of type "map<Int as uint8, Bool>"
11 | let m2: map<Int as uint16, Bool> = emptyMap();
> 12 | return m1.deepEquals(m2);
^~~~~~~~~~~~~~~~~
13 | }
"
`;

exports[`resolveStatements should fail statements for map-deepEquals-type-mismatch-5 1`] = `
"<unknown>:12:16: Type mismatch: cannot pass argument of type "map<Int, Int>" to parameter of type "map<Int, Int as uint16>"
11 | let m2: map<Int, Int> = emptyMap();
> 12 | return m1.deepEquals(m2);
^~~~~~~~~~~~~~~~~
13 | }
"
`;

exports[`resolveStatements should fail statements for return-analysis-catch-if 1`] = `
"<unknown>:4:1: Function does not always return a result. Adding 'return' statement(s) should fix the issue.
3 |
Expand Down
14 changes: 14 additions & 0 deletions src/types/stmts-failed/map-deepEquals-type-mismatch-2.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
primitive Bool;
primitive Int;
primitive Address;

trait BaseTrait {}

contract Test {
get fun foo(): Bool {
let m1: map<Address, Int> = emptyMap();
m1.set(address(""), 0);
let m2: map<Address, Address> = emptyMap();
return m1.deepEquals(m2);
}
}
14 changes: 14 additions & 0 deletions src/types/stmts-failed/map-deepEquals-type-mismatch-3.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
primitive Bool;
primitive Int;
primitive Address;

trait BaseTrait {}

contract Test {
get fun foo(): Bool {
let m1: map<Int, Bool> = emptyMap();
m1.set(0, false);
let m2: map<Int, Address> = emptyMap();
return m1.deepEquals(m2);
}
}
14 changes: 14 additions & 0 deletions src/types/stmts-failed/map-deepEquals-type-mismatch-4.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
primitive Bool;
primitive Int;
primitive Address;

trait BaseTrait {}

contract Test {
get fun foo(): Bool {
let m1: map<Int as uint8, Bool> = emptyMap();
m1.set(0, false);
let m2: map<Int as uint16, Bool> = emptyMap();
return m1.deepEquals(m2);
}
}
14 changes: 14 additions & 0 deletions src/types/stmts-failed/map-deepEquals-type-mismatch-5.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
primitive Bool;
primitive Int;
primitive Address;

trait BaseTrait {}

contract Test {
get fun foo(): Bool {
let m1: map<Int, Int as uint16> = emptyMap();
m1.set(0, 10);
let m2: map<Int, Int> = emptyMap();
return m1.deepEquals(m2);
}
}
14 changes: 14 additions & 0 deletions src/types/stmts-failed/map-deepEquals-type-mismatch.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
primitive Bool;
primitive Int;
primitive Address;

trait BaseTrait {}

contract Test {
get fun foo(): Bool {
let m1: map<Int, Int> = emptyMap();
m1.set(42, 0);
let m2: map<Address, Int> = emptyMap();
return m1.deepEquals(m2);
}
}