diff --git a/src/compiler.ts b/src/compiler.ts index baa324e97c..769d1fe79d 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -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(target, expression)) { + case ElementKind.Global: + case ElementKind.Local: { + if (target.kind == ElementKind.Global) { + if (!this.compileGlobalLazy(target, expression)) { + return this.module.unreachable(); + } + } else if (!(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, @@ -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, diff --git a/src/program.ts b/src/program.ts index 5bae6bd76b..1f9849eec2 100644 --- a/src/program.ts +++ b/src/program.ts @@ -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. */ diff --git a/tests/compiler/closure.json b/tests/compiler/closure.json index 60af5f4ab5..043415b403 100644 --- a/tests/compiler/closure.json +++ b/tests/compiler/closure.json @@ -10,6 +10,8 @@ "$local0; // closure 3", "AS100: Not implemented: Closures", "$local0(123); // closure 4", + "AS100: Not implemented: Closures", + "$local0 = 10; // closure 5", "EOF" ] } diff --git a/tests/compiler/closure.ts b/tests/compiler/closure.ts index 8d83cc791c..26ba15304b 100644 --- a/tests/compiler/closure.ts +++ b/tests/compiler/closure.ts @@ -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");