diff --git a/src/abi/map.ts b/src/abi/map.ts index 148269a68..87b7c57a2 100644 --- a/src/abi/map.ts +++ b/src/abi/map.ts @@ -1,6 +1,6 @@ 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"; @@ -8,6 +8,7 @@ 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( @@ -492,6 +493,13 @@ export const MapFunctions: ReadonlyMap = new Map([ checkMapType(self, ref); checkMapType(other, ref); + if (!isAssignable(self, other)) { + throwCompilationError( + `Type mismatch: "${printTypeRef(self)}" is not assignable to "${printTypeRef(other)}"`, + ref, + ); + } + return { kind: "ref", name: "Bool", optional: false }; }, generate( diff --git a/src/types/__snapshots__/resolveStatements.spec.ts.snap b/src/types/__snapshots__/resolveStatements.spec.ts.snap index d78dc86d7..3330099da 100644 --- a/src/types/__snapshots__/resolveStatements.spec.ts.snap +++ b/src/types/__snapshots__/resolveStatements.spec.ts.snap @@ -783,6 +783,24 @@ exports[`resolveStatements should fail statements for init-vars-analysis-with-if " `; +exports[`resolveStatements should fail statements for map-deepEquals-type-mismatch 1`] = ` +":12:16: Type mismatch: "map" is not assignable to "map" + 11 | let m2: map = emptyMap(); +> 12 | return m1.deepEquals(m2); + ^~~~~~~~~~~~~~~~~ + 13 | } +" +`; + +exports[`resolveStatements should fail statements for map-deepEquals-type-mismatch-2 1`] = ` +":12:16: Type mismatch: "map" is not assignable to "map" + 11 | let m2: map = emptyMap(); +> 12 | return m1.deepEquals(m2); + ^~~~~~~~~~~~~~~~~ + 13 | } +" +`; + exports[`resolveStatements should fail statements for return-analysis-catch-if 1`] = ` ":4:1: Function does not always return a result. Adding 'return' statement(s) should fix the issue. 3 | diff --git a/src/types/stmts-failed/map-deepEquals-type-mismatch-2.tact b/src/types/stmts-failed/map-deepEquals-type-mismatch-2.tact new file mode 100644 index 000000000..a4a89102a --- /dev/null +++ b/src/types/stmts-failed/map-deepEquals-type-mismatch-2.tact @@ -0,0 +1,14 @@ +primitive Bool; +primitive Int; +primitive Address; + +trait BaseTrait {} + +contract Test { + get fun foo(): Bool { + let m1: map = emptyMap(); + m1.set(address(""), 0); + let m2: map = emptyMap(); + return m1.deepEquals(m2); + } +} diff --git a/src/types/stmts-failed/map-deepEquals-type-mismatch.tact b/src/types/stmts-failed/map-deepEquals-type-mismatch.tact new file mode 100644 index 000000000..58dd1a5b7 --- /dev/null +++ b/src/types/stmts-failed/map-deepEquals-type-mismatch.tact @@ -0,0 +1,14 @@ +primitive Bool; +primitive Int; +primitive Address; + +trait BaseTrait {} + +contract Test { + get fun foo(): Bool { + let m1: map = emptyMap(); + m1.set(42, 0); + let m2: map = emptyMap(); + return m1.deepEquals(m2); + } +}