From f04619672f9507a4d4230abdd55cee5982520457 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 28 Sep 2023 11:28:54 +0800 Subject: [PATCH 1/5] fix: diagnoise not implememt when assign value to closure variable --- src/compiler.ts | 25 ++++++++++++++++++------- src/program.ts | 11 +++++------ tests/compiler/closure.json | 2 ++ tests/compiler/closure.ts | 8 ++++++++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index baa324e97c..d76a931862 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -5596,13 +5596,23 @@ 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)) { - return this.module.unreachable(); - } - // fall-through - } + case ElementKind.Global: case ElementKind.Local: { + if (target.kind == ElementKind.Global) { + if (!this.compileGlobalLazy(target, expression)) { + return this.module.unreachable(); + } + } else { + if ((target).isClosure(flow)) { + // TODO: closures + this.error( + DiagnosticCode.Not_implemented_0, + expression.range, + "Closures" + ); + return this.module.unreachable(); + } + } if (this.pendingElements.has(target)) { this.error( DiagnosticCode.Variable_0_used_before_its_declaration, @@ -5611,6 +5621,7 @@ export class Compiler extends DiagnosticEmitter { ); return this.module.unreachable(); } + targetType = (target).type; if (target.hasDecorator(DecoratorFlags.Unsafe)) this.checkUnsafe(expression); break; @@ -7370,7 +7381,7 @@ export class Compiler extends DiagnosticEmitter { this.currentType = localType; } - if (target.parent != flow.targetFunction) { + if (local.isClosure(flow)) { // TODO: closures this.error( DiagnosticCode.Not_implemented_0, diff --git a/src/program.ts b/src/program.ts index 5bae6bd76b..14c29f46ad 100644 --- a/src/program.ts +++ b/src/program.ts @@ -3628,17 +3628,16 @@ export class Local extends VariableLikeElement { /** Declaration reference. */ declaration: VariableLikeDeclarationStatement = parent.program.makeNativeVariableDeclaration(name) ) { - super( - ElementKind.Local, - name, - parent, - declaration - ); + super(ElementKind.Local, name, parent, declaration); this.originalName = name; this.index = index; assert(type != Type.void); this.setType(type); } + + isClosure(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"); From f12ad29962315461db3f05cdde183d26479d35ab Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 28 Sep 2023 13:17:44 +0800 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: CountBleck --- src/compiler.ts | 19 ++++++++----------- src/program.ts | 7 ++++++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index d76a931862..efc2811431 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -5602,16 +5602,14 @@ export class Compiler extends DiagnosticEmitter { if (!this.compileGlobalLazy(target, expression)) { return this.module.unreachable(); } - } else { - if ((target).isClosure(flow)) { - // TODO: closures - this.error( - DiagnosticCode.Not_implemented_0, - expression.range, - "Closures" - ); - return this.module.unreachable(); - } + } else if ((target).isClosure(flow)) { + // TODO: closures + this.error( + DiagnosticCode.Not_implemented_0, + expression.range, + "Closures" + ); + return this.module.unreachable(); } if (this.pendingElements.has(target)) { this.error( @@ -5621,7 +5619,6 @@ export class Compiler extends DiagnosticEmitter { ); return this.module.unreachable(); } - targetType = (target).type; if (target.hasDecorator(DecoratorFlags.Unsafe)) this.checkUnsafe(expression); break; diff --git a/src/program.ts b/src/program.ts index 14c29f46ad..e73a687963 100644 --- a/src/program.ts +++ b/src/program.ts @@ -3628,7 +3628,12 @@ export class Local extends VariableLikeElement { /** Declaration reference. */ declaration: VariableLikeDeclarationStatement = parent.program.makeNativeVariableDeclaration(name) ) { - super(ElementKind.Local, name, parent, declaration); + super( + ElementKind.Local, + name, + parent, + declaration + ); this.originalName = name; this.index = index; assert(type != Type.void); From 714ecd9a2a90c6ef160eb8436764ce556e685725 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 28 Sep 2023 17:52:52 +0800 Subject: [PATCH 3/5] rename --- src/compiler.ts | 4 ++-- src/program.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index d76a931862..dc524aed2d 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -5603,7 +5603,7 @@ export class Compiler extends DiagnosticEmitter { return this.module.unreachable(); } } else { - if ((target).isClosure(flow)) { + if (!(target).isInCurrentFunction(flow)) { // TODO: closures this.error( DiagnosticCode.Not_implemented_0, @@ -7381,7 +7381,7 @@ export class Compiler extends DiagnosticEmitter { this.currentType = localType; } - if (local.isClosure(flow)) { + if (!local.isInCurrentFunction(flow)) { // TODO: closures this.error( DiagnosticCode.Not_implemented_0, diff --git a/src/program.ts b/src/program.ts index 14c29f46ad..49e916912d 100644 --- a/src/program.ts +++ b/src/program.ts @@ -3635,8 +3635,8 @@ export class Local extends VariableLikeElement { this.setType(type); } - isClosure(flow: Flow): bool { - return this.parent != flow.targetFunction; + isInCurrentFunction(flow: Flow): bool { + return this.parent == flow.targetFunction; } } From 916b6066538f708f1bb3056c87f6c65a63dd73af Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 28 Sep 2023 17:59:47 +0800 Subject: [PATCH 4/5] refactor --- src/compiler.ts | 4 ++-- src/program.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 349ffa143b..b2523a53a7 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -5602,7 +5602,7 @@ export class Compiler extends DiagnosticEmitter { if (!this.compileGlobalLazy(target, expression)) { return this.module.unreachable(); } - } else if (!(target).isInCurrentFunction(flow)) { + } else if (!(target).isInFunction(flow.targetFunction)) { // TODO: closures this.error( DiagnosticCode.Not_implemented_0, @@ -7378,7 +7378,7 @@ export class Compiler extends DiagnosticEmitter { this.currentType = localType; } - if (!local.isInCurrentFunction(flow)) { + if (!local.isInFunction(flow.targetFunction)) { // TODO: closures this.error( DiagnosticCode.Not_implemented_0, diff --git a/src/program.ts b/src/program.ts index 595208376a..9abb558043 100644 --- a/src/program.ts +++ b/src/program.ts @@ -3640,8 +3640,8 @@ export class Local extends VariableLikeElement { this.setType(type); } - isInCurrentFunction(flow: Flow): bool { - return this.parent == flow.targetFunction; + isInFunction(fn: Function): bool { + return this.parent == fn; } } From 85cce53c46d94a4841c72bc1a59516effb761195 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Fri, 29 Sep 2023 12:36:57 +0800 Subject: [PATCH 5/5] refactor --- src/compiler.ts | 4 ++-- src/program.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index b2523a53a7..769d1fe79d 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -5602,7 +5602,7 @@ export class Compiler extends DiagnosticEmitter { if (!this.compileGlobalLazy(target, expression)) { return this.module.unreachable(); } - } else if (!(target).isInFunction(flow.targetFunction)) { + } else if (!(target).declaredByFlow(flow)) { // TODO: closures this.error( DiagnosticCode.Not_implemented_0, @@ -7378,7 +7378,7 @@ export class Compiler extends DiagnosticEmitter { this.currentType = localType; } - if (!local.isInFunction(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 9abb558043..1f9849eec2 100644 --- a/src/program.ts +++ b/src/program.ts @@ -3640,8 +3640,8 @@ export class Local extends VariableLikeElement { this.setType(type); } - isInFunction(fn: Function): bool { - return this.parent == fn; + declaredByFlow(flow: Flow): bool { + return this.parent == flow.targetFunction; } }