Skip to content

fix: diagnose not implement when assign value to closure variable #2750

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

Merged
merged 6 commits into from
Oct 1, 2023
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
20 changes: 14 additions & 6 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5596,13 +5596,21 @@ export class Compiler extends DiagnosticEmitter {
// to compile just the value, we need to know the target's type
let targetType: Type;
switch (target.kind) {
case ElementKind.Global: {
if (!this.compileGlobalLazy(<Global>target, expression)) {
case ElementKind.Global:
case ElementKind.Local: {
if (target.kind == ElementKind.Global) {
if (!this.compileGlobalLazy(<Global>target, expression)) {
return this.module.unreachable();
}
} else if (!(<Local>target).declaredByFlow(flow)) {
// TODO: closures
this.error(
DiagnosticCode.Not_implemented_0,
expression.range,
"Closures"
);
return this.module.unreachable();
}
// fall-through
}
case ElementKind.Local: {
if (this.pendingElements.has(target)) {
this.error(
DiagnosticCode.Variable_0_used_before_its_declaration,
Expand Down Expand Up @@ -7370,7 +7378,7 @@ export class Compiler extends DiagnosticEmitter {
this.currentType = localType;
}

if (target.parent != flow.targetFunction) {
if (!local.declaredByFlow(flow)) {
// TODO: closures
this.error(
DiagnosticCode.Not_implemented_0,
Expand Down
4 changes: 4 additions & 0 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3639,6 +3639,10 @@ export class Local extends VariableLikeElement {
assert(type != Type.void);
this.setType(type);
}

declaredByFlow(flow: Flow): bool {
return this.parent == flow.targetFunction;
}
}

/** A yet unresolved function prototype. */
Expand Down
2 changes: 2 additions & 0 deletions tests/compiler/closure.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"$local0; // closure 3",
"AS100: Not implemented: Closures",
"$local0(123); // closure 4",
"AS100: Not implemented: Closures",
"$local0 = 10; // closure 5",
"EOF"
]
}
8 changes: 8 additions & 0 deletions tests/compiler/closure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ function testFuncParam($local0: (x: i32) => void): () => void {
}
testFuncParam((x: i32) => {});

function testAssign(): (value: i32) => void {
let $local0 = 0;
return function inner(value: i32): void {
$local0 = 10; // closure 5
};
}
testAssign();

ERROR("EOF");