diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp index eb32e5f0040da45702a05a8db8b7475afe2431f9..05308a331508889f19c952bb6052c3995b907953 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 0000000000000000000000000000000000000000..d0dcb0a29d3baf5228fcd8d448b75eba7bc2780c --- /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 0000000000000000000000000000000000000000..3d02e08acf8fcaf770a272e6bbe16070e00e05ec --- /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 0000000000000000000000000000000000000000..41a2a86d83287a432ea228c40e86bc7568e4e04d --- /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