Skip to content

Commit

Permalink
fix: Diagnose recursion when resolving expressions (#2264)
Browse files Browse the repository at this point in the history
  • Loading branch information
technohippy committed Jul 23, 2022
1 parent 1e20054 commit 92ea1c4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,9 @@ export class Resolver extends DiagnosticEmitter {
return null;
}

/** resolving expressions */
private resolvingExpressions: Set<Expression> = new Set();

/** Resolves an expression to its static type. */
resolveExpression(
/** The expression to resolve. */
Expand All @@ -1006,6 +1009,21 @@ export class Resolver extends DiagnosticEmitter {
ctxType: Type = Type.auto,
/** How to proceed with eventual diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
const resolvingExpressions = this.resolvingExpressions;
if (resolvingExpressions.has(node)) return null;
resolvingExpressions.add(node);
const resolved = this.doResolveExpression(node, ctxFlow, ctxType, reportMode);
resolvingExpressions.delete(node);
return resolved;
}

/** Resolves an expression to its static type. (may cause stack overflow) */
private doResolveExpression(
node: Expression,
ctxFlow: Flow,
ctxType: Type = Type.auto,
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
while (node.kind == NodeKind.PARENTHESIZED) { // skip
node = (<ParenthesizedExpression>node).expression;
Expand Down
10 changes: 10 additions & 0 deletions tests/compiler/avoid-resolve-loop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"asc_flags": [
],
"stderr": [
"AS225: Expression cannot be represented by a type.",
"TS2448: Variable 'avoid-resolve-loop/e' used before its declaration.",
"TS2322: Type 'void' is not assignable to type '<auto>'.",
"AS225: Expression cannot be represented by a type."
]
}
4 changes: 4 additions & 0 deletions tests/compiler/avoid-resolve-loop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let a = a[0];

let e = e;
typeof [e];

0 comments on commit 92ea1c4

Please sign in to comment.