From 54c7592d2873fbe3be7225338c2f576d29a7d326 Mon Sep 17 00:00:00 2001 From: xuxinjie4 Date: Wed, 16 Jul 2025 16:04:36 +0800 Subject: [PATCH] Fix serval crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICMQY8?from=project-issue Signed-off-by: xuxinjie4 --- ets2panda/checker/ets/typeCheckingHelpers.cpp | 6 +++- ets2panda/checker/types/typeError.h | 6 ++++ .../test/ast/compiler/ets/forof_bad_type.ets | 28 +++++++++++++++++++ .../test/ast/compiler/ets/union_access.ets | 23 +++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/forof_bad_type.ets create mode 100644 ets2panda/test/ast/compiler/ets/union_access.ets diff --git a/ets2panda/checker/ets/typeCheckingHelpers.cpp b/ets2panda/checker/ets/typeCheckingHelpers.cpp index dbc753ee3c..087c77677e 100644 --- a/ets2panda/checker/ets/typeCheckingHelpers.cpp +++ b/ets2panda/checker/ets/typeCheckingHelpers.cpp @@ -666,7 +666,11 @@ Type *ETSChecker::GuaranteedTypeForUnionFieldAccess(ir::MemberExpression *member { const auto &types = etsUnionType->ConstituentTypes(); ArenaVector apparentTypes {Allocator()->Adapter()}; - const auto &propertyName = memberExpression->Property()->AsIdentifier()->Name(); + const auto *prop = memberExpression->Property(); + if (!prop->IsIdentifier() && !prop->IsStringLiteral()) { + return GlobalTypeError(); + } + const auto &propertyName = prop->IsIdentifier() ? prop->AsIdentifier()->Name() : prop->AsStringLiteral()->Str(); for (auto *type : types) { auto searchFlags = PropertySearchFlags::SEARCH_FIELD | PropertySearchFlags::SEARCH_METHOD | PropertySearchFlags::SEARCH_IN_BASE; diff --git a/ets2panda/checker/types/typeError.h b/ets2panda/checker/types/typeError.h index 9b2d87c407..86f49b40cc 100644 --- a/ets2panda/checker/types/typeError.h +++ b/ets2panda/checker/types/typeError.h @@ -42,6 +42,12 @@ public: { ss << ERROR_TYPE; } + + Type *Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation, + [[maybe_unused]] GlobalTypesHolder *globalTypes) override + { + return this; + } }; } // namespace ark::es2panda::checker diff --git a/ets2panda/test/ast/compiler/ets/forof_bad_type.ets b/ets2panda/test/ast/compiler/ets/forof_bad_type.ets new file mode 100644 index 0000000000..1f20a9aece --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/forof_bad_type.ets @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare class A { + a: A + foo(f: (a: FixedArray | A[]) => void):void + bar():A +} + +new A().bar().a.foo((x) => { + for (let e of x) { + console.log(e) + } +}) + +/* @@? 18:27 Error TypeError: Cannot find type 'Tadad'. */ diff --git a/ets2panda/test/ast/compiler/ets/union_access.ets b/ets2panda/test/ast/compiler/ets/union_access.ets new file mode 100644 index 0000000000..8132d86e19 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/union_access.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class C{} +class A{} + +function foo(x: A | C) { + x[x+1] +} + +/* @@? 20:5 Error TypeError: Indexed access is not supported for such expression type. */ \ No newline at end of file -- Gitee