Skip to content

Commit 92ea1c4

Browse files
authored
fix: Diagnose recursion when resolving expressions (#2264)
1 parent 1e20054 commit 92ea1c4

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Diff for: src/resolver.ts

+18
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,9 @@ export class Resolver extends DiagnosticEmitter {
996996
return null;
997997
}
998998

999+
/** resolving expressions */
1000+
private resolvingExpressions: Set<Expression> = new Set();
1001+
9991002
/** Resolves an expression to its static type. */
10001003
resolveExpression(
10011004
/** The expression to resolve. */
@@ -1006,6 +1009,21 @@ export class Resolver extends DiagnosticEmitter {
10061009
ctxType: Type = Type.auto,
10071010
/** How to proceed with eventual diagnostics. */
10081011
reportMode: ReportMode = ReportMode.REPORT
1012+
): Type | null {
1013+
const resolvingExpressions = this.resolvingExpressions;
1014+
if (resolvingExpressions.has(node)) return null;
1015+
resolvingExpressions.add(node);
1016+
const resolved = this.doResolveExpression(node, ctxFlow, ctxType, reportMode);
1017+
resolvingExpressions.delete(node);
1018+
return resolved;
1019+
}
1020+
1021+
/** Resolves an expression to its static type. (may cause stack overflow) */
1022+
private doResolveExpression(
1023+
node: Expression,
1024+
ctxFlow: Flow,
1025+
ctxType: Type = Type.auto,
1026+
reportMode: ReportMode = ReportMode.REPORT
10091027
): Type | null {
10101028
while (node.kind == NodeKind.PARENTHESIZED) { // skip
10111029
node = (<ParenthesizedExpression>node).expression;

Diff for: tests/compiler/avoid-resolve-loop.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"asc_flags": [
3+
],
4+
"stderr": [
5+
"AS225: Expression cannot be represented by a type.",
6+
"TS2448: Variable 'avoid-resolve-loop/e' used before its declaration.",
7+
"TS2322: Type 'void' is not assignable to type '<auto>'.",
8+
"AS225: Expression cannot be represented by a type."
9+
]
10+
}

Diff for: tests/compiler/avoid-resolve-loop.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let a = a[0];
2+
3+
let e = e;
4+
typeof [e];

0 commit comments

Comments
 (0)