From 99d8eb3368e1986652e8032705af9645e6f9cbff Mon Sep 17 00:00:00 2001 From: zmw Date: Tue, 1 Jul 2025 15:33:29 +0800 Subject: [PATCH] Fix arrow function in record Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICJ5LZ Description: Fix arrow function call in record property as key assert fail Signed-off-by: zmw --- ets2panda/checker/ets/helpers.cpp | 4 +++ .../compiler/lowering/ets/recordLowering.cpp | 6 +--- ...w_function_call_as_record_property_key.ets | 29 +++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/arrow_function_call_as_record_property_key.ets diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 51e0dad3cd..1fce5c4242 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -1849,6 +1849,10 @@ Type *ETSChecker::GetReferencedTypeBase(ir::Expression *name) } ES2PANDA_ASSERT(name->IsIdentifier()); + if (name->AsIdentifier()->Variable() == nullptr) { + // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) + VarBinder()->AsETSBinder()->LookupTypeReference(name->AsIdentifier()); + } auto *const var = name->AsIdentifier()->Variable(); ES2PANDA_ASSERT(var != nullptr); diff --git a/ets2panda/compiler/lowering/ets/recordLowering.cpp b/ets2panda/compiler/lowering/ets/recordLowering.cpp index 889c864962..32c30d0353 100644 --- a/ets2panda/compiler/lowering/ets/recordLowering.cpp +++ b/ets2panda/compiler/lowering/ets/recordLowering.cpp @@ -108,12 +108,8 @@ void RecordLowering::CheckDuplicateKey(KeySetType &keySet, ir::ObjectExpression ctx->GetChecker()->AsETSChecker()->LogError(diagnostic::OBJ_LIT_PROP_NAME_COLLISION, {}, expr->Start()); break; } - case ir::AstNodeType::IDENTIFIER: { - ctx->GetChecker()->AsETSChecker()->LogError(diagnostic::OBJ_LIT_UNKNOWN_PROP, {}, expr->Start()); - break; - } default: { - ES2PANDA_UNREACHABLE(); + ctx->GetChecker()->AsETSChecker()->LogError(diagnostic::OBJ_LIT_UNKNOWN_PROP, {}, expr->Start()); break; } } diff --git a/ets2panda/test/ast/compiler/ets/arrow_function_call_as_record_property_key.ets b/ets2panda/test/ast/compiler/ets/arrow_function_call_as_record_property_key.ets new file mode 100644 index 0000000000..aac66bdd54 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/arrow_function_call_as_record_property_key.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. + */ + +const a: Record = { + [((): number => 2)()]: '1' +} + +const b: Record number> = { + [(():number => +("foo"))()]: function (y: string): number { + return y.length; + }, + [(():number => +("bar"))()]: (y: string):number => y.length +}; + +/* @@? 21:20 Error TypeError: Wrong operand type for unary expression */ +/* @@? 21:34 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 24:20 Error TypeError: Wrong operand type for unary expression */ -- Gitee