Skip to content

Fix assertion when referencing a global within its own initializer #1190

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

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 11 additions & 2 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5791,8 +5791,17 @@ export class Compiler extends DiagnosticEmitter {
var targetType: Type;
switch (target.kind) {
case ElementKind.GLOBAL: {
let global = <Global>target;
// not yet compiled if a static field compiled as a global
if (!this.compileGlobal(<Global>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:
Expand Down Expand Up @@ -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),
Expand Down
11 changes: 11 additions & 0 deletions tests/compiler/assign-self.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
17 changes: 17 additions & 0 deletions tests/compiler/assign-self.ts
Original file line number Diff line number Diff line change
@@ -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");