diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp index 96da74bb4bfd40af081062b4814b08f8cad5c379..65cba7026ecc77a403c72ab49ed942ed4d74a1f1 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 0000000000000000000000000000000000000000..fa6ab227a59a4186cdd10e47e5c3839da42e54c3 --- /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 */