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

feat: prevent variable declaration from shadowing functions #323

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions src/types/__snapshots__/resolveStatements.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1063,11 +1063,11 @@ exports[`resolveStatements should resolve statements for case-11 1`] = `
"Int",
],
[
"toString",
"number",
"Int",
],
[
"dump(toString)",
"dump(number)",
"<void>",
],
]
Expand Down
20 changes: 20 additions & 0 deletions src/types/resolveDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
TypeOrigin,
TypeRef,
typeRefEquals,
VariableDescription,
} from "./types";
import { getRawAST } from "../grammar/store";
import { cloneNode } from "../grammar/clone";
Expand All @@ -39,6 +40,7 @@ import { GlobalFunctions } from "../abi/global";
const store = createContextStore<TypeDescription>();
const staticFunctionsStore = createContextStore<FunctionDescription>();
const staticConstantsStore = createContextStore<ConstantDescription>();
const variablesStore = createContextStore<VariableDescription>();

function verifyMapType(
key: string,
Expand Down Expand Up @@ -1568,12 +1570,15 @@ export function resolveDescriptors(ctx: CompilerContext) {

for (const [k, t] of types) {
ctx = store.set(ctx, k, t);
//ctx = variablesStore.set(ctx, k, t);
}
for (const [k, t] of staticFunctions) {
ctx = staticFunctionsStore.set(ctx, k, t);
ctx = variablesStore.set(ctx, k, t);
}
for (const [k, t] of staticConstants) {
ctx = staticConstantsStore.set(ctx, k, t);
ctx = variablesStore.set(ctx, k, t);
}

return ctx;
Expand Down Expand Up @@ -1612,6 +1617,21 @@ export function hasStaticFunction(ctx: CompilerContext, name: string) {
return !!staticFunctionsStore.get(ctx, name);
}

export function getVariable(
ctx: CompilerContext,
name: string,
): VariableDescription {
const r = variablesStore.get(ctx, name);
if (!r) {
throw Error("Variable " + name + " not found");
}
return r;
}

export function hasVariable(ctx: CompilerContext, name: string) {
return !!variablesStore.get(ctx, name);
}

export function getStaticConstant(
ctx: CompilerContext,
name: string,
Expand Down
4 changes: 4 additions & 0 deletions src/types/resolveStatements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isAssignable } from "./isAssignable";
import {
getAllStaticFunctions,
getAllTypes,
hasVariable,
resolveTypeRef,
} from "./resolveDescriptors";
import {
Expand Down Expand Up @@ -180,6 +181,9 @@ function processStatements(
);
}

if (hasVariable(ctx, s.name)) {
throwError(`Variable already defined : ${s.name}`, s.ref);
}
// Add variable to statement context
if (sctx.vars.has(s.name)) {
throwError(`Variable "${s.name}" already exists`, s.ref);
Expand Down
6 changes: 6 additions & 0 deletions src/types/stmts-failed/case-53.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
primitive Int;

fun rec(): Int {
let rec: Int = 42; // shadowing error
return rec;
}
4 changes: 2 additions & 2 deletions src/types/stmts/case-11.tact
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
primitive Int;

fun testFunction() {
let toString: Int = 1;
dump(toString);
let number: Int = 1;
dump(number);
}
4 changes: 4 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export type ConstantDescription = {
ast: ASTConstant;
};

export type VariableDescription = {
name: string;
};

export type FunctionArgument = {
name: string;
type: TypeRef;
Expand Down
Loading