Skip to content

Commit

Permalink
feat(all): Replace throw Error with throwInternalCompilerError
Browse files Browse the repository at this point in the history
  • Loading branch information
jubnzv committed Aug 8, 2024
1 parent 671b471 commit c9475cf
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 46 deletions.
3 changes: 2 additions & 1 deletion src/grammar/clone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AstNode, cloneAstNode } from "./ast";
import { throwInternalCompilerError } from "../errors";

export function cloneNode<T extends AstNode>(src: T): T {
if (src.kind === "boolean") {
Expand Down Expand Up @@ -174,5 +175,5 @@ export function cloneNode<T extends AstNode>(src: T): T {
});
}

throw Error("Not implemented for " + src.kind);
throwInternalCompilerError(`Not implemented for ${src.kind}`);
}
5 changes: 3 additions & 2 deletions src/grammar/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Grammar,
} from "ohm-js";
import tactGrammar from "./grammar.ohm-bundle";
import { throwInternalCompilerError } from "../errors";
import {
AstAugmentedAssignOperation,
AstConstantAttribute,
Expand Down Expand Up @@ -675,8 +676,8 @@ semantics.addOperation<AstNode>("astOfStatement", {
op = "^";
break;
default:
throw Error(
"Internal compiler error: unreachable augmented assignment operator. Please report at https://github.com/tact-lang/tact/issues",
throwInternalCompilerError(
"Unreachable augmented assignment operator.",
);
}
return createAstNode({
Expand Down
3 changes: 2 additions & 1 deletion src/grammar/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AstNativeFunctionDecl,
AstTypeDecl,
} from "./ast";
import { throwInternalCompilerError } from "../errors";
import { CompilerContext, createContextStore } from "../context";
import { ItemOrigin, parse } from "./grammar";

Expand Down Expand Up @@ -40,7 +41,7 @@ const store = createContextStore<AstStore>();
export function getRawAST(ctx: CompilerContext): AstStore {
const r = store.get(ctx, "types");
if (!r) {
throw Error("No AST found in context");
throwInternalCompilerError("No AST found in context");
}
return r;
}
Expand Down
40 changes: 30 additions & 10 deletions src/storage/allocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ export function getAllocationOperationFromField(
optional: src.optional ? src.optional : false,
};
} else if (src.format !== null && src.format !== undefined) {
throw Error("Unsupported int format " + src.format);
throwInternalCompilerError(
`Unsupported int format: ${src.format}`,
);
}
return {
kind: "int",
Expand Down Expand Up @@ -170,7 +172,9 @@ export function getAllocationOperationFromField(
optional: src.optional ? src.optional : false,
};
} else if (src.format !== null && src.format !== undefined) {
throw Error("Unsupported int format " + src.format);
throwInternalCompilerError(
`Unsupported int format: ${src.format}`,
);
}
return {
kind: "uint",
Expand All @@ -180,7 +184,9 @@ export function getAllocationOperationFromField(
}
if (src.type === "bool") {
if (src.format !== null && src.format !== undefined) {
throw Error("Unsupported bool format " + src.format);
throwInternalCompilerError(
`Unsupported bool format: ${src.format}`,
);
}
return {
kind: "boolean",
Expand All @@ -195,7 +201,9 @@ export function getAllocationOperationFromField(
format: "remainder",
};
} else if (src.format !== null && src.format !== undefined) {
throw Error("Unsupported cell format " + src.format);
throwInternalCompilerError(
`Unsupported cell format: ${src.format}`,
);
}
return {
kind: "cell",
Expand All @@ -211,7 +219,9 @@ export function getAllocationOperationFromField(
format: "remainder",
};
} else if (src.format !== null && src.format !== undefined) {
throw Error("Unsupported slice format " + src.format);
throwInternalCompilerError(
`Unsupported slice format: ${src.format}`,
);
}
return {
kind: "slice",
Expand All @@ -227,7 +237,9 @@ export function getAllocationOperationFromField(
format: "remainder",
};
} else if (src.format !== null && src.format !== undefined) {
throw Error("Unsupported slice format " + src.format);
throwInternalCompilerError(
`Unsupported slice format: ${src.format}`,
);
}
return {
kind: "builder",
Expand All @@ -249,12 +261,16 @@ export function getAllocationOperationFromField(
optional: src.optional ? src.optional : false,
};
} else {
throw Error("Unsupported fixed-bytes format " + src.format);
throwInternalCompilerError(
`Unsupported fixed-bytes format: ${src.format}`,
);
}
}
if (src.type === "string") {
if (src.format !== null && src.format !== undefined) {
throw Error("Unsupported string format " + src.format);
throwInternalCompilerError(
`Unsupported string format: ${src.format}`,
);
}
return {
kind: "string",
Expand All @@ -273,7 +289,9 @@ export function getAllocationOperationFromField(
size,
};
} else if (src.format !== undefined && src.format !== null) {
throw Error("Unsupported struct format " + src.format);
throwInternalCompilerError(
`Unsupported struct format: ${src.format}`,
);
} else {
return {
kind: "struct",
Expand All @@ -286,7 +304,9 @@ export function getAllocationOperationFromField(
}
case "dict": {

Check failure on line 305 in src/storage/allocator.ts

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest)

Expected a 'break' statement before 'case'

Check failure on line 305 in src/storage/allocator.ts

View workflow job for this annotation

GitHub Actions / test (20, windows-latest)

Expected a 'break' statement before 'case'

Check failure on line 305 in src/storage/allocator.ts

View workflow job for this annotation

GitHub Actions / test (20, macos-latest)

Expected a 'break' statement before 'case'
if (src.format !== null && src.format !== undefined) {
throw Error("Unsupported map format " + src.format);
throwInternalCompilerError(
`Unsupported map format: ${src.format}`,
);
}
return { kind: "map" };
}
Expand Down
3 changes: 2 additions & 1 deletion src/storage/resolveAllocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { AllocationOperation } from "./operation";
import { allocate, getAllocationOperationFromField } from "./allocator";
import { createABITypeRefFromTypeRef } from "../types/resolveABITypeRef";
import { funcInitIdOf } from "../generator/writers/id";
import { throwInternalCompilerError } from "../errors";
import { idText } from "../grammar/ast";

const store = createContextStore<StorageAllocation>();

export function getAllocation(ctx: CompilerContext, name: string) {
const t = store.get(ctx, name);
if (!t) {
throw Error("Allocation for " + name + " not found");
throwInternalCompilerError(`Allocation for ${name} not found`);
}
return t;
}
Expand Down
20 changes: 13 additions & 7 deletions src/types/resolveABITypeRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ export function createABITypeRefFromTypeRef(
return { kind: "simple", type: "string", optional: src.optional };
}
if (src.name === "StringBuilder") {
throw Error(`Unsupported type "${src.name}"`);
throwInternalCompilerError(`Unsupported type "${src.name}"`);
}

// Structs
Expand Down Expand Up @@ -444,7 +444,7 @@ export function createABITypeRefFromTypeRef(
);
}
} else {
throw Error(`Unsupported map key type "${src.key}"`);
throwInternalCompilerError(`Unsupported map key type "${src.key}"`);
}

// Resolve value type
Expand Down Expand Up @@ -479,7 +479,9 @@ export function createABITypeRefFromTypeRef(
);
}
} else if (src.value === "Slice") {
throw Error(`Unsupported map value type "${src.value}"`);
throwInternalCompilerError(
`Unsupported map value type "${src.value}"`,
);
} else if (src.value === "Address") {
value = "address";
if (src.valueAs) {
Expand All @@ -489,9 +491,13 @@ export function createABITypeRefFromTypeRef(
);
}
} else if (src.value === "String") {
throw Error(`Unsupported map value type "${src.value}"`);
throwInternalCompilerError(
`Unsupported map value type "${src.value}"`,
);
} else if (src.value === "StringBuilder" || src.value === "Builder") {
throw Error(`Unsupported map value type "${src.value}"`);
throwInternalCompilerError(
`Unsupported map value type "${src.value}"`,
);
} else {
value = src.value;
valueFormat = "ref";
Expand All @@ -507,8 +513,8 @@ export function createABITypeRefFromTypeRef(
}

if (src.kind === "ref_bounced") {
throw Error("Unexpected bounced reference");
throwInternalCompilerError("Unexpected bounced reference");
}

throw Error(`Unsupported type`);
throwInternalCompilerError(`Unsupported type`);
}
10 changes: 6 additions & 4 deletions src/types/resolveDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,9 @@ export function resolveDescriptors(ctx: CompilerContext) {
if (d.kind === "function_def" || d.kind === "function_decl") {
const f = resolveFunctionDescriptor(s.name, d, s.origin);
if (f.self !== s.name) {
throw Error("Function self must be " + s.name); // Impossible
throwInternalCompilerError(
`Function self must be ${s.name}`,
); // Impossible
}
if (s.functions.has(f.name)) {
throwCompilationError(
Expand Down Expand Up @@ -1818,7 +1820,7 @@ export function getType(
const name = typeof ident === "string" ? ident : idText(ident);
const r = store.get(ctx, name);
if (!r) {
throw Error("Type " + name + " not found");
throwInternalCompilerError(`Type ${name} not found`);
}
return r;
}
Expand All @@ -1839,7 +1841,7 @@ export function getStaticFunction(
): FunctionDescription {
const r = staticFunctionsStore.get(ctx, name);
if (!r) {
throw Error("Static function " + name + " not found");
throwInternalCompilerError(`Static function ${name} not found`);
}
return r;
}
Expand All @@ -1854,7 +1856,7 @@ export function getStaticConstant(
): ConstantDescription {
const r = staticConstantsStore.get(ctx, name);
if (!r) {
throw Error("Static constant " + name + " not found");
throwInternalCompilerError(`Static constant ${name} not found`);
}
return r;
}
Expand Down
5 changes: 3 additions & 2 deletions src/types/resolveExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { StatementContext } from "./resolveStatements";
import { MapFunctions } from "../abi/map";
import { GlobalFunctions } from "../abi/global";
import { isAssignable, moreGeneralType } from "./subtyping";
import { throwInternalCompilerError } from "../errors";
import { StructFunctions } from "../abi/struct";

const store = createContextStore<{
Expand All @@ -45,7 +46,7 @@ const store = createContextStore<{
export function getExpType(ctx: CompilerContext, exp: AstExpression) {
const t = store.get(ctx, exp.id);
if (!t) {
throw Error("Expression " + exp.id + " not found");
throwInternalCompilerError(`Expression ${exp.id} not found`);
}
return t.description;
}
Expand All @@ -60,7 +61,7 @@ function registerExpType(
if (typeRefEquals(ex.description, description)) {
return ctx;
}
throw Error("Expression " + exp.id + " already has a type");
throwInternalCompilerError(`Expression ${exp.id} already has a type`);
}
return store.set(ctx, exp.id, { ast: exp, description });
}
Expand Down
Loading

0 comments on commit c9475cf

Please sign in to comment.