From 238c0117767a975bb7af013a94f70f4d05ed9dca Mon Sep 17 00:00:00 2001 From: MuSilk Date: Fri, 8 Aug 2025 10:53:12 +0800 Subject: [PATCH] Fix condition operator constant lowering Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICUMXC?from=project-issue Signed-off-by: musilk --- .../ets/constantExpressionLowering.cpp | 21 +++++++++-------- .../compiler/ets/extended_cond_arr_neg.ets | 23 +++++++++++++++++++ .../condition_operation_constant_lowering.ets | 19 +++++++++++++++ .../test/runtime/ets/extended_cond_array.ets | 21 +++++++++++++++++ 4 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/extended_cond_arr_neg.ets create mode 100644 ets2panda/test/runtime/ets/condition_operation_constant_lowering.ets create mode 100644 ets2panda/test/runtime/ets/extended_cond_array.ets diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp index eb32e5f004..05308a3315 100644 --- a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp +++ b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp @@ -799,28 +799,31 @@ private: return CreateBooleanLiteral(res); } - ir::Literal *HandleLogicalExpression(const ir::BinaryExpression *expr, const ir::Literal *left, - const ir::Literal *right) + ir::Literal *HandleLogicalExpression(const ir::BinaryExpression *expr, ir::Literal *left, ir::Literal *right) { + auto allocator = context_->allocator; + auto parent = const_cast(expr)->Parent(); bool lhs = TestLiteral(left); - bool rhs = TestLiteral(right); - bool res {}; auto opType = expr->OperatorType(); switch (opType) { case lexer::TokenType::PUNCTUATOR_LOGICAL_AND: { - res = lhs && rhs; - break; + if (lhs) { + return right->Clone(allocator, parent)->AsExpression()->AsLiteral(); + } + return left->Clone(allocator, parent)->AsExpression()->AsLiteral(); } case lexer::TokenType::PUNCTUATOR_LOGICAL_OR: { - res = lhs || rhs; - break; + if (lhs) { + return left->Clone(allocator, parent)->AsExpression()->AsLiteral(); + } + return right->Clone(allocator, parent)->AsExpression()->AsLiteral(); } default: { ES2PANDA_UNREACHABLE(); } } - return CreateBooleanLiteral(res); + ES2PANDA_UNREACHABLE(); } ir::Literal *Calculate(const ir::BinaryExpression *expr) diff --git a/ets2panda/test/ast/compiler/ets/extended_cond_arr_neg.ets b/ets2panda/test/ast/compiler/ets/extended_cond_arr_neg.ets new file mode 100644 index 0000000000..d0dcb0a29d --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/extended_cond_arr_neg.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. + */ + +let arr = [1,2] + +arr[1.5 || 4.67] + +/* @@? 18:1 Error TypeError: No matching indexing signature for $_get(Double) */ +/* @@? 18:5 Error TypeError: Index value cannot be less than zero or fractional. */ +/* @@? 18:5 Error TypeError: Type 'Double' is not compatible with type 'Int' at index 1 */ +/* @@? 18:5 Error TypeError: Cannot find index access method with the required signature. */ diff --git a/ets2panda/test/runtime/ets/condition_operation_constant_lowering.ets b/ets2panda/test/runtime/ets/condition_operation_constant_lowering.ets new file mode 100644 index 0000000000..3d02e08acf --- /dev/null +++ b/ets2panda/test/runtime/ets/condition_operation_constant_lowering.ets @@ -0,0 +1,19 @@ +/* + * 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. + */ + +let array = [1,2] + +arktest.assertEQ(array[1 && 1],2) +arktest.assertEQ(array[1 || 1],2) diff --git a/ets2panda/test/runtime/ets/extended_cond_array.ets b/ets2panda/test/runtime/ets/extended_cond_array.ets new file mode 100644 index 0000000000..41a2a86d83 --- /dev/null +++ b/ets2panda/test/runtime/ets/extended_cond_array.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +function main() { + let arr = [1,2]; + + arktest.assertEQ(arr[8237549235487923 && 1], 2); + arktest.assertEQ(arr[1 || 8237549235487923], 2); +} \ No newline at end of file -- Gitee