Skip to content

Commit

Permalink
chore: relative file paths in error messages (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov authored Jun 21, 2024
1 parent b75468f commit e4f1939
Show file tree
Hide file tree
Showing 16 changed files with 517 additions and 385 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update JSON Schema to inform about usage in Blueprint: PR [#330](https://github.com/tact-lang/tact/pull/330)
- All identifiers in error messages are now quoted for consistency: PR [#363](https://github.com/tact-lang/tact/pull/363)
- The Tact grammar has been refactored for better readability: PR [#365](https://github.com/tact-lang/tact/pull/365)
- Error messages now use relative file paths: PR [#456](https://github.com/tact-lang/tact/pull/456)

### Fixed

Expand Down
109 changes: 74 additions & 35 deletions src/abi/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Address, Cell, toNano } from "@ton/core";
import { enabledDebug, enabledMasterchain } from "../config/features";
import { writeAddress, writeCell } from "../generator/writers/writeConstant";
import { writeExpression } from "../generator/writers/writeExpression";
import { throwSyntaxError } from "../errors";
import { throwCompilationError } from "../errors";
import { evalConstantExpression } from "../constEval";
import { getErrorId } from "../types/resolveErrors";
import { AbiFunction } from "./AbiFunction";
Expand All @@ -17,19 +17,19 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
name: "ton",
resolve: (ctx, args, ref) => {
if (args.length !== 1) {
throwSyntaxError(
throwCompilationError(
"ton() expects single string argument",
ref,
);
}
if (args[0].kind !== "ref") {
throwSyntaxError(
throwCompilationError(
"ton() expects single string argument",
ref,
);
}
if (args[0].name !== "String") {
throwSyntaxError(
throwCompilationError(
"ton() expects single string argument",
ref,
);
Expand All @@ -38,7 +38,7 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
},
generate: (ctx, args, resolved, ref) => {
if (resolved.length !== 1) {
throwSyntaxError(
throwCompilationError(
"ton() expects single string argument",
ref,
);
Expand All @@ -57,28 +57,31 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
name: "require",
resolve: (ctx, args, ref) => {
if (args.length !== 2) {
throwSyntaxError("require() expects two arguments", ref);
throwCompilationError(
"require() expects two arguments",
ref,
);
}
if (args[0].kind !== "ref") {
throwSyntaxError(
throwCompilationError(
"require() expects first Bool argument",
ref,
);
}
if (args[0].name !== "Bool") {
throwSyntaxError(
throwCompilationError(
"require() expects first Bool argument",
ref,
);
}
if (args[1].kind !== "ref") {
throwSyntaxError(
throwCompilationError(
"require() expects second string argument",
ref,
);
}
if (args[1].name !== "String") {
throwSyntaxError(
throwCompilationError(
"require() expects second string argument",
ref,
);
Expand All @@ -87,7 +90,10 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
},
generate: (ctx, args, resolved, ref) => {
if (resolved.length !== 2) {
throwSyntaxError("require() expects two arguments", ref);
throwCompilationError(
"require() expects two arguments",
ref,
);
}
const str = evalConstantExpression(
resolved[1],
Expand All @@ -103,19 +109,31 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
name: "address",
resolve: (ctx, args, ref) => {
if (args.length !== 1) {
throwSyntaxError("address() expects one argument", ref);
throwCompilationError(
"address() expects one argument",
ref,
);
}
if (args[0].kind !== "ref") {
throwSyntaxError("address() expects string argument", ref);
throwCompilationError(
"address() expects string argument",
ref,
);
}
if (args[0].name !== "String") {
throwSyntaxError("address() expects string argument", ref);
throwCompilationError(
"address() expects string argument",
ref,
);
}
return { kind: "ref", name: "Address", optional: false };
},
generate: (ctx, args, resolved, ref) => {
if (resolved.length !== 1) {
throwSyntaxError("address() expects one argument", ref);
throwCompilationError(
"address() expects one argument",
ref,
);
}
const str = evalConstantExpression(
resolved[0],
Expand All @@ -125,14 +143,17 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
try {
address = Address.parse(str);
} catch {
throwSyntaxError(`${str} is not a valid address`, ref);
throwCompilationError(`${str} is not a valid address`, ref);
}
if (address.workChain !== 0 && address.workChain !== -1) {
throwSyntaxError(`Address ${str} invalid address`, ref);
throwCompilationError(
`Address ${str} invalid address`,
ref,
);
}
if (!enabledMasterchain(ctx.ctx)) {
if (address.workChain !== 0) {
throwSyntaxError(
throwCompilationError(
`Address ${str} from masterchain are not enabled for this contract`,
ref,
);
Expand All @@ -152,19 +173,25 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
name: "cell",
resolve: (ctx, args, ref) => {
if (args.length !== 1) {
throwSyntaxError("cell() expects one argument", ref);
throwCompilationError("cell() expects one argument", ref);
}
if (args[0].kind !== "ref") {
throwSyntaxError("cell() expects string argument", ref);
throwCompilationError(
"cell() expects string argument",
ref,
);
}
if (args[0].name !== "String") {
throwSyntaxError("cell() expects string argument", ref);
throwCompilationError(
"cell() expects string argument",
ref,
);
}
return { kind: "ref", name: "Cell", optional: false };
},
generate: (ctx, args, resolved, ref) => {
if (resolved.length !== 1) {
throwSyntaxError("cell() expects one argument", ref);
throwCompilationError("cell() expects one argument", ref);
}

// Load cell data
Expand All @@ -176,7 +203,7 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
try {
c = Cell.fromBase64(str);
} catch (e) {
throwSyntaxError(`Invalid cell ${str}`, ref);
throwCompilationError(`Invalid cell ${str}`, ref);
}

// Generate address
Expand All @@ -192,7 +219,7 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
name: "dump",
resolve: (ctx, args, ref) => {
if (args.length !== 1) {
throwSyntaxError("dump expects 1 argument", ref);
throwCompilationError("dump expects 1 argument", ref);
}
return { kind: "void" };
},
Expand Down Expand Up @@ -236,12 +263,15 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
const exp = writeExpression(resolved[0], ctx);
return `${ctx.used(`__tact_debug`)}(${exp}, "${debugPrint}")`;
}
throwSyntaxError(
throwCompilationError(
"dump() not supported for type: " + arg.name,
ref,
);
} else {
throwSyntaxError("dump() not supported for argument", ref);
throwCompilationError(
"dump() not supported for argument",
ref,
);
}
},
},
Expand All @@ -252,7 +282,10 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
name: "dumpStack",
resolve: (_ctx, args, ref) => {
if (args.length !== 0) {
throwSyntaxError("dumpStack expects no arguments", ref);
throwCompilationError(
"dumpStack expects no arguments",
ref,
);
}
return { kind: "void" };
},
Expand All @@ -275,7 +308,7 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
name: "emptyMap",
resolve: (ctx, args, ref) => {
if (args.length !== 0) {
throwSyntaxError("emptyMap expects no arguments", ref);
throwCompilationError("emptyMap expects no arguments", ref);
}
return { kind: "null" };
},
Expand All @@ -290,13 +323,16 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
name: "sha256",
resolve: (ctx, args, ref) => {
if (args.length !== 1) {
throwSyntaxError("sha256 expects 1 argument", ref);
throwCompilationError("sha256 expects 1 argument", ref);
}
if (args[0].kind !== "ref") {
throwSyntaxError("sha256 expects string argument", ref);
throwCompilationError(
"sha256 expects string argument",
ref,
);
}
if (args[0].name !== "String" && args[0].name !== "Slice") {
throwSyntaxError(
throwCompilationError(
"sha256 expects string or slice argument",
ref,
);
Expand All @@ -305,10 +341,13 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
},
generate: (ctx, args, resolved, ref) => {
if (args.length !== 1) {
throwSyntaxError("sha256 expects 1 argument", ref);
throwCompilationError("sha256 expects 1 argument", ref);
}
if (args[0].kind !== "ref") {
throwSyntaxError("sha256 expects string argument", ref);
throwCompilationError(
"sha256 expects string argument",
ref,
);
}

// String case
Expand All @@ -319,7 +358,7 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
ctx.ctx,
) as string;
if (Buffer.from(str).length > 128) {
throwSyntaxError(
throwCompilationError(
"sha256 expects string argument with byte length <= 128",
ref,
);
Expand All @@ -340,7 +379,7 @@ export const GlobalFunctions: Map<string, AbiFunction> = new Map([
return `string_hash(${exp})`;
}

throwSyntaxError(
throwCompilationError(
"sha256 expects string or slice argument",
ref,
);
Expand Down
Loading

0 comments on commit e4f1939

Please sign in to comment.