From 4b1482c42779653ea420606c35e2dca722bc248f Mon Sep 17 00:00:00 2001 From: huangyu Date: Sun, 23 Jul 2023 22:25:03 +0800 Subject: [PATCH] Fix type extractor for mutual type alias got infinite loop issue Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I7NA5L Test: test/type_extractor/testcases Signed-off-by: huangyu Change-Id: Ie5cb0d88ce0fa7ede7e828a22a56147aacf640fb --- .../test-type-alias-for-loop-1-expected.txt | 141 +++++++++++++++ .../testcases/test-type-alias-for-loop-1.ts | 3 + .../test-type-alias-for-loop-2-expected.txt | 161 ++++++++++++++++++ .../testcases/test-type-alias-for-loop-2.ts | 5 + .../test-type-alias-for-loop-3-expected.txt | 161 ++++++++++++++++++ .../testcases/test-type-alias-for-loop-3.ts | 5 + .../typescript/extractor/typeExtractor.cpp | 8 +- es2panda/typescript/extractor/typeExtractor.h | 23 +++ 8 files changed, 506 insertions(+), 1 deletion(-) create mode 100644 es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1-expected.txt create mode 100644 es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1.ts create mode 100644 es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2-expected.txt create mode 100644 es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2.ts create mode 100644 es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3-expected.txt create mode 100644 es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3.ts diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1-expected.txt b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1-expected.txt new file mode 100644 index 0000000000..b806d76bba --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1-expected.txt @@ -0,0 +1,141 @@ +======> literal array buffer <====== +------------------------------------ +slot _.func_main_0_-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _2 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 5 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 0 +}, +------------------------------------ +slot _2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 5 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +------------------------------------ +slot _3 +{ + index: 0 + tag: 2 + val: 0 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 2 + val: 0 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 2 + val: 0 +}, +{ + index: 5 + tag: 2 + val: 0 +}, diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1.ts b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1.ts new file mode 100644 index 0000000000..31e1b8430c --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1.ts @@ -0,0 +1,3 @@ +type b = a +type a = b[] +const v: b[] = [] diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2-expected.txt b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2-expected.txt new file mode 100644 index 0000000000..7306bd8324 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2-expected.txt @@ -0,0 +1,161 @@ +======> literal array buffer <====== +------------------------------------ +slot _.func_main_0_-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 4 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 2 +}, +{ + index: 4 + tag: 0 + val: 25 +}, +{ + index: 5 + tag: 25 + val: 1 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _2 +}, +------------------------------------ +slot _2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 5 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 0 +}, +------------------------------------ +slot _3 +{ + index: 0 + tag: 2 + val: 0 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 2 + val: 0 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 2 + val: 0 +}, +{ + index: 5 + tag: 2 + val: 0 +}, diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2.ts b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2.ts new file mode 100644 index 0000000000..8d8ed0a69c --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2.ts @@ -0,0 +1,5 @@ +type x = y +type y = z | x[] +type z = w +type w = number +var a: x diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3-expected.txt b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3-expected.txt new file mode 100644 index 0000000000..e2030554b8 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3-expected.txt @@ -0,0 +1,161 @@ +======> literal array buffer <====== +------------------------------------ +slot _.func_main_0_-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 4 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 2 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _2 +}, +------------------------------------ +slot _2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 5 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 0 +}, +------------------------------------ +slot _3 +{ + index: 0 + tag: 2 + val: 0 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 2 + val: 0 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 2 + val: 0 +}, +{ + index: 5 + tag: 2 + val: 0 +}, diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3.ts b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3.ts new file mode 100644 index 0000000000..ddbea05330 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3.ts @@ -0,0 +1,5 @@ +type a = b +type b = c | a[] +type c = d +type d = c[] +var v: a; diff --git a/es2panda/typescript/extractor/typeExtractor.cpp b/es2panda/typescript/extractor/typeExtractor.cpp index 174c95d70b..430e451572 100644 --- a/es2panda/typescript/extractor/typeExtractor.cpp +++ b/es2panda/typescript/extractor/typeExtractor.cpp @@ -463,8 +463,14 @@ int64_t TypeExtractor::GetTypeIndexFromImportNode(const ir::AstNode *node, [[may int64_t TypeExtractor::GetTypeIndexFromTypeAliasNode(const ir::AstNode *node, [[maybe_unused]] bool isNewInstance) { - auto typeIndex = GetTypeIndexFromAnnotation(node->AsTSTypeAliasDeclaration()->TypeAnnotation()); + auto typeNode = node->AsTSTypeAliasDeclaration()->TypeAnnotation(); + if (!AddSearchingTypeRefNodes(typeNode)) { + RemoveSearchingTypeRefNodes(typeNode); + return PrimitiveType::ANY; + } + auto typeIndex = GetTypeIndexFromAnnotation(typeNode); TLOG(node->Type(), typeIndex); + RemoveSearchingTypeRefNodes(typeNode); return typeIndex; } diff --git a/es2panda/typescript/extractor/typeExtractor.h b/es2panda/typescript/extractor/typeExtractor.h index 1d0bedf0f2..3533e9df15 100644 --- a/es2panda/typescript/extractor/typeExtractor.h +++ b/es2panda/typescript/extractor/typeExtractor.h @@ -18,6 +18,7 @@ #include #include +#include #include "typeRecorder.h" @@ -60,8 +61,30 @@ public: static int64_t GetBuiltinTypeIndex(util::StringView name); + bool AddSearchingTypeRefNodes(const ir::Expression * node) + { + if (!IsInSearchingTypeRefNodes(node)) { + searchingTypeRefNodes_.insert(node); + return true; + } + return false; + } + + void RemoveSearchingTypeRefNodes(const ir::Expression * node) + { + if (IsInSearchingTypeRefNodes(node)) { + searchingTypeRefNodes_.erase(node); + } + } + + bool IsInSearchingTypeRefNodes(const ir::Expression * node) + { + return searchingTypeRefNodes_.find(node) != searchingTypeRefNodes_.end(); + } + private: const ir::BlockStatement *rootNode_; + std::unordered_set searchingTypeRefNodes_; const bool typeDtsExtractor_; const bool typeDtsBuiltin_; std::unique_ptr recorder_; -- Gitee