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");