From c974d409f5d223b9f29bac14217c3218f4a0c671 Mon Sep 17 00:00:00 2001 From: Daniel Kofanov Date: Sat, 5 Jul 2025 01:22:11 +0800 Subject: [PATCH] [ETS] Fix const-folding corner cases Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICK522 Change-Id: I81416b0511079539ae20978321934f2afa325e5e Signed-off-by: Daniel Kofanov --- .../ets/constantExpressionLowering.cpp | 32 +++++++++++++++++-- .../ets/constantExpressionLowering.ets | 27 ++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/constantExpressionLowering.ets diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp index 96da74bb4b..65cba7026e 100644 --- a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp +++ b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp @@ -674,7 +674,13 @@ ir::AstNode *ConstantExpressionLowering::FoldUnaryConstant(ir::UnaryExpression * return FoldUnaryBooleanConstant(unary); } - return FoldUnaryNumericConstant(unary); + auto lit = unary->Argument()->AsLiteral(); + if (lit->IsNumberLiteral() || lit->IsCharLiteral()) { + return FoldUnaryNumericConstant(unary, context->allocator); + } + + LogError(context, diagnostic::WRONG_OPERAND_TYPE_FOR_UNARY_EXPRESSION, {}, unary->Start()); + return CreateErrorIdentifier(unary, context->allocator); } ir::AstNode *ConstantExpressionLowering::TryFoldTSAsExpressionForString(ir::TSAsExpression *expr) @@ -792,8 +798,28 @@ ir::AstNode *ConstantExpressionLowering::UnFoldEnumMemberExpression(ir::AstNode return node; }; - constantNode->TransformChildrenRecursivelyPostorder(handleUnfoldEnumMember, Name()); - return constantNode; + + util::UString result(allocator); + auto quasis = expr->Quasis(); + auto expressions = expr->Expressions(); + + if (!quasis.empty() && !quasis[0]->Raw().Empty()) { + result.Append(quasis[0]->Cooked()); + } + + auto const num = expressions.size(); + std::size_t i = 0U; + while (i < num) { + result.Append(litToString(expressions[i]->AsLiteral())); + if (!quasis[++i]->Raw().Empty()) { + result.Append(quasis[i]->Cooked()); + } + } + + auto *strLit = util::NodeAllocator::Alloc(allocator, result.View()); + strLit->SetParent(expr->Parent()); + strLit->SetRange(expr->Range()); + return strLit; } ir::AstNode *ConstantExpressionLowering::FindNameInEnumMember(ArenaVector *members, diff --git a/ets2panda/test/ast/compiler/ets/constantExpressionLowering.ets b/ets2panda/test/ast/compiler/ets/constantExpressionLowering.ets new file mode 100644 index 0000000000..fa6ab227a5 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/constantExpressionLowering.ets @@ -0,0 +1,27 @@ +/* + * 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. + */ + +// 1. +const tmpl = `` + +// 2. +const flag = true +const a = +(flag) +const b = -(flag) +const c = ~(flag) + +/* @@? 21:11 Error TypeError: Wrong operand type for unary expression */ +/* @@? 22:11 Error TypeError: Wrong operand type for unary expression */ +/* @@? 23:11 Error TypeError: Wrong operand type for unary expression */ -- Gitee