From e4b004248291ab0f74c5c79e7920afb3dfffedc6 Mon Sep 17 00:00:00 2001 From: dcode Date: Thu, 26 Mar 2020 03:00:55 +0100 Subject: [PATCH] Fix assertion when referencing a global within its own initializer --- src/compiler.ts | 13 +++++++++++-- tests/compiler/assign-self.json | 11 +++++++++++ tests/compiler/assign-self.ts | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 tests/compiler/assign-self.json create mode 100644 tests/compiler/assign-self.ts diff --git a/src/compiler.ts b/src/compiler.ts index 495f670822..a224581c17 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -5791,8 +5791,17 @@ export class Compiler extends DiagnosticEmitter { var targetType: Type; switch (target.kind) { case ElementKind.GLOBAL: { + let global = target; // not yet compiled if a static field compiled as a global - if (!this.compileGlobal(target)) return this.module.unreachable(); // reports + if (!this.compileGlobal(global)) return this.module.unreachable(); // reports + if (!global.is(CommonFlags.RESOLVED)) { + // used inside of its own initializer, calling compileGlobal in compileGlobal + this.error( + DiagnosticCode.Cannot_find_name_0, + expression.range, global.name + ); + return this.module.unreachable(); + } // fall-through } case ElementKind.LOCAL: @@ -5875,9 +5884,9 @@ export class Compiler extends DiagnosticEmitter { } // compile the value and do the assignment - assert(targetType != Type.void); var valueExpr = this.compileExpression(valueExpression, targetType, Constraints.WILL_RETAIN); var valueType = this.currentType; + if (targetType == Type.auto) targetType = valueType; return this.makeAssignment( target, this.convertExpression(valueExpr, valueType, targetType, false, false, valueExpression), diff --git a/tests/compiler/assign-self.json b/tests/compiler/assign-self.json new file mode 100644 index 0000000000..e507477a5f --- /dev/null +++ b/tests/compiler/assign-self.json @@ -0,0 +1,11 @@ +{ + "asc_flags": [ + "--runtime none" + ], + "stderr": [ + "TS2304: Cannot find name 'a'.", + "TS2304: Cannot find name 'b'.", + "TS2304: Cannot find name 'c'.", + "EOF" + ] +} \ No newline at end of file diff --git a/tests/compiler/assign-self.ts b/tests/compiler/assign-self.ts new file mode 100644 index 0000000000..e21fc02116 --- /dev/null +++ b/tests/compiler/assign-self.ts @@ -0,0 +1,17 @@ +var a = (a = 4, 3); + +function testVar(): i32 { + var b = (b = 4, 3); + return b; +} +testVar(); + +function testLet(): i32 { + { + let c = (c = 4, 3); + return c; + } +} +testLet(); + +ERROR("EOF");