From bbf5d4bd0893040e8951dcb6652cb040c88a068b Mon Sep 17 00:00:00 2001 From: ANDO Yasushi Date: Fri, 15 Apr 2022 22:22:57 +0900 Subject: [PATCH 1/6] fix: #2204, #2211 resolve loop --- src/resolver.ts | 20 ++++++++++++++++++++ tests/compiler/avoid-resolve-loop.json | 10 ++++++++++ tests/compiler/avoid-resolve-loop.ts | 4 ++++ 3 files changed, 34 insertions(+) create mode 100644 tests/compiler/avoid-resolve-loop.json create mode 100644 tests/compiler/avoid-resolve-loop.ts diff --git a/src/resolver.ts b/src/resolver.ts index db485a0973..a375ec576d 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -996,6 +996,9 @@ export class Resolver extends DiagnosticEmitter { return null; } + /** resolving expressions */ + private resolvingExpressions: Expression[] = []; + /** Resolves an expression to its static type. */ resolveExpression( /** The expression to resolve. */ @@ -1006,6 +1009,23 @@ export class Resolver extends DiagnosticEmitter { ctxType: Type = Type.auto, /** How to proceed with eventual diagnostics. */ reportMode: ReportMode = ReportMode.REPORT + ): Type | null { + if (this.resolvingExpressions.includes(node)) return null; + + this.resolvingExpressions.push(node); + try { + return this.unsafeResolveExpression(node, ctxFlow, ctxType, reportMode); + } finally { + this.resolvingExpressions.pop(); + } + } + + /** Resolves an expression to its static type. (may cause stack overflow) */ + private unsafeResolveExpression( + node: Expression, + ctxFlow: Flow, + ctxType: Type = Type.auto, + reportMode: ReportMode = ReportMode.REPORT ): Type | null { while (node.kind == NodeKind.PARENTHESIZED) { // skip node = (node).expression; diff --git a/tests/compiler/avoid-resolve-loop.json b/tests/compiler/avoid-resolve-loop.json new file mode 100644 index 0000000000..9499b5939d --- /dev/null +++ b/tests/compiler/avoid-resolve-loop.json @@ -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 ''.", + "AS225: Expression cannot be represented by a type." + ] +} \ No newline at end of file diff --git a/tests/compiler/avoid-resolve-loop.ts b/tests/compiler/avoid-resolve-loop.ts new file mode 100644 index 0000000000..1c0d726068 --- /dev/null +++ b/tests/compiler/avoid-resolve-loop.ts @@ -0,0 +1,4 @@ +let a = a[0]; + +let e = e; +typeof [e]; \ No newline at end of file From ae6f3dc62cd5fa9b524ed85df2e177ff93fd936b Mon Sep 17 00:00:00 2001 From: ANDO Yasushi Date: Fri, 15 Apr 2022 22:39:57 +0900 Subject: [PATCH 2/6] AS100: Not implemented: Exceptions --- src/resolver.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/resolver.ts b/src/resolver.ts index a375ec576d..64ce76dd8f 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -1013,11 +1013,9 @@ export class Resolver extends DiagnosticEmitter { if (this.resolvingExpressions.includes(node)) return null; this.resolvingExpressions.push(node); - try { - return this.unsafeResolveExpression(node, ctxFlow, ctxType, reportMode); - } finally { - this.resolvingExpressions.pop(); - } + const resolved = this.unsafeResolveExpression(node, ctxFlow, ctxType, reportMode); + this.resolvingExpressions.pop(); + return resolved; } /** Resolves an expression to its static type. (may cause stack overflow) */ From 9d4bfcd4bed9011a65051ee625b931efacce902e Mon Sep 17 00:00:00 2001 From: ANDO Yasushi Date: Sat, 16 Apr 2022 09:01:58 +0900 Subject: [PATCH 3/6] use Set instead of Array --- src/resolver.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/resolver.ts b/src/resolver.ts index 64ce76dd8f..7ea8134508 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -997,7 +997,7 @@ export class Resolver extends DiagnosticEmitter { } /** resolving expressions */ - private resolvingExpressions: Expression[] = []; + private resolvingExpressions: Set = new Set(); /** Resolves an expression to its static type. */ resolveExpression( @@ -1010,11 +1010,11 @@ export class Resolver extends DiagnosticEmitter { /** How to proceed with eventual diagnostics. */ reportMode: ReportMode = ReportMode.REPORT ): Type | null { - if (this.resolvingExpressions.includes(node)) return null; + if (this.resolvingExpressions.has(node)) return null; - this.resolvingExpressions.push(node); + this.resolvingExpressions.add(node); const resolved = this.unsafeResolveExpression(node, ctxFlow, ctxType, reportMode); - this.resolvingExpressions.pop(); + this.resolvingExpressions.delete(node); return resolved; } From addf33ce845f6c1a0434eb90bcb511aaa99595f4 Mon Sep 17 00:00:00 2001 From: ANDO Yasushi Date: Tue, 26 Apr 2022 12:31:34 +0900 Subject: [PATCH 4/6] Update src/resolver.ts Co-authored-by: dcode --- src/resolver.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/resolver.ts b/src/resolver.ts index 7ea8134508..c17fa04c87 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -1010,11 +1010,11 @@ export class Resolver extends DiagnosticEmitter { /** How to proceed with eventual diagnostics. */ reportMode: ReportMode = ReportMode.REPORT ): Type | null { - if (this.resolvingExpressions.has(node)) return null; - - this.resolvingExpressions.add(node); + const resolvingExpressions = this.resolvingExpressions; + if (resolvingExpressions.has(node)) return null; + resolvingExpressions.add(node); const resolved = this.unsafeResolveExpression(node, ctxFlow, ctxType, reportMode); - this.resolvingExpressions.delete(node); + resolvingExpressions.delete(node); return resolved; } From 8f575eecd9821a44f29f97ede2cb0c513ad92221 Mon Sep 17 00:00:00 2001 From: ANDO Yasushi Date: Tue, 26 Apr 2022 12:31:43 +0900 Subject: [PATCH 5/6] Update src/resolver.ts Co-authored-by: dcode --- src/resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resolver.ts b/src/resolver.ts index c17fa04c87..4890c1c43a 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -1019,7 +1019,7 @@ export class Resolver extends DiagnosticEmitter { } /** Resolves an expression to its static type. (may cause stack overflow) */ - private unsafeResolveExpression( + private doResolveExpression( node: Expression, ctxFlow: Flow, ctxType: Type = Type.auto, From 368e8636d3d8eacdef52be5acd0bfba5a361aead Mon Sep 17 00:00:00 2001 From: ANDO Yasushi Date: Tue, 26 Apr 2022 12:52:06 +0900 Subject: [PATCH 6/6] unsafe -> do --- src/resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resolver.ts b/src/resolver.ts index 4890c1c43a..e1d7989805 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -1013,7 +1013,7 @@ export class Resolver extends DiagnosticEmitter { const resolvingExpressions = this.resolvingExpressions; if (resolvingExpressions.has(node)) return null; resolvingExpressions.add(node); - const resolved = this.unsafeResolveExpression(node, ctxFlow, ctxType, reportMode); + const resolved = this.doResolveExpression(node, ctxFlow, ctxType, reportMode); resolvingExpressions.delete(node); return resolved; }