From c893ef21b081b0f41e391f68fe8b783e40dc3aef Mon Sep 17 00:00:00 2001 From: fcc Date: Mon, 14 Jul 2025 16:35:36 +0800 Subject: [PATCH] Fix crash when checking union field access When downcasting a node, checking its type before cast. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICM69K Signed-off-by: fcc --- ets2panda/checker/ets/typeCheckingHelpers.cpp | 10 ++++++- .../ast/compiler/ets/union_field_access_2.ets | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/union_field_access_2.ets diff --git a/ets2panda/checker/ets/typeCheckingHelpers.cpp b/ets2panda/checker/ets/typeCheckingHelpers.cpp index 6496cdb8ce..c225e9188d 100644 --- a/ets2panda/checker/ets/typeCheckingHelpers.cpp +++ b/ets2panda/checker/ets/typeCheckingHelpers.cpp @@ -731,7 +731,15 @@ Type *ETSChecker::GuaranteedTypeForUnionFieldAccess(ir::MemberExpression *member const auto &types = etsUnionType->ConstituentTypes(); ArenaVector apparentTypes {ProgramAllocator()->Adapter()}; const auto *prop = memberExpression->Property(); - const auto &propertyName = prop->IsIdentifier() ? prop->AsIdentifier()->Name() : prop->AsStringLiteral()->Str(); + util::StringView propertyName; + if (prop->IsIdentifier()) { + propertyName = prop->AsIdentifier()->Name(); + } else if (prop->IsStringLiteral()) { + propertyName = prop->AsStringLiteral()->Str(); + } else { + return GlobalTypeError(); + } + for (auto *type : types) { auto searchFlags = PropertySearchFlags::SEARCH_FIELD | PropertySearchFlags::SEARCH_METHOD | PropertySearchFlags::SEARCH_IN_BASE; diff --git a/ets2panda/test/ast/compiler/ets/union_field_access_2.ets b/ets2panda/test/ast/compiler/ets/union_field_access_2.ets new file mode 100644 index 0000000000..f381005632 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/union_field_access_2.ets @@ -0,0 +1,29 @@ +/* + * 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. + */ + +// ensure compiler doesn't crash when compiling invalid code. + +(a ? delete arguments==({: false, Float64Array: []}) : [ArrayBuffer[false]]) + +/* @@? 18:1 Error TypeError: Indexed access is not supported for such expression type. */ +/* @@? 18:2 Error TypeError: Unresolved reference a */ +/* @@? 18:6 Error TypeError: Unresolved reference delete */ +/* @@? 18:13 Error SyntaxError: Unexpected token. */ +/* @@? 18:13 Error TypeError: Unresolved reference arguments */ +/* @@? 18:24 Error TypeError: need to specify target type for class composite */ +/* @@? 18:26 Error SyntaxError: Unexpected token. */ +/* @@? 18:28 Error SyntaxError: Unexpected token. */ +/* @@? 18:54 Error SyntaxError: Unexpected token, expected ')'. */ +/* @@? 18:76 Error SyntaxError: Unexpected token ')'. */ -- Gitee