diff --git a/ets2panda/checker/ets/arithmetic.cpp b/ets2panda/checker/ets/arithmetic.cpp index b0b3e32efd5af3ce71e4525bf4e8fca6e88a970f..e1d6539b8d3c8f10460db71f3dd4ea9d48a41bdd 100644 --- a/ets2panda/checker/ets/arithmetic.cpp +++ b/ets2panda/checker/ets/arithmetic.cpp @@ -657,11 +657,18 @@ static Type *CheckOperatorEqualDynamic(ETSChecker *checker, BinaryArithmOperands return checker->GlobalETSNullishObjectType(); } +static bool BinaryEqualityWithNullish(Type const *typeL, checker::Type const *typeR) +{ + return (typeR->IsETSNullType() && typeL->IsETSPrimitiveType()) || + (typeL->IsETSNullType() && typeR->IsETSPrimitiveType()) || + (typeR->IsETSUndefinedType() && typeL->IsETSPrimitiveType()) || + (typeL->IsETSUndefinedType() && typeR->IsETSPrimitiveType()); +} + static Type *HandelReferenceBinaryEquality(ETSChecker *checker, BinaryArithmOperands const &ops) { [[maybe_unused]] auto const [expr, typeL, typeR, reducedL, reducedR] = ops; - if ((typeR->IsETSNullType() && typeL->IsETSPrimitiveType()) || - (typeL->IsETSNullType() && typeR->IsETSPrimitiveType())) { + if (BinaryEqualityWithNullish(typeL, typeR)) { return checker->CreateETSUnionType({typeL, typeR}); } diff --git a/ets2panda/test/ast/parser/ets/SmartCast_1.ets b/ets2panda/test/ast/parser/ets/SmartCast_1.ets index 003aeff8da6b1b48d1888e6968c8c76afbefff9b..bdfed2596bf2c9a18bee921910e70955510b3376 100644 --- a/ets2panda/test/ast/parser/ets/SmartCast_1.ets +++ b/ets2panda/test/ast/parser/ets/SmartCast_1.ets @@ -22,7 +22,8 @@ function foo(a: boolean | undefined) : int { } } else { //a is undefined - if (/* @@ label */a == false) { + //according to spec 7.25.9, any entity can be compared to null and undefined + if (a == false) { return 0; } else { return 1; @@ -35,5 +36,3 @@ function main() { foo(false); foo(undefined); } - -/* @@@ label Error TypeError: Bad operand type, the types of the operands must be numeric, same enumeration, or boolean type. */ diff --git a/ets2panda/test/ast/parser/ets/SmartCast_2.ets b/ets2panda/test/ast/parser/ets/SmartCast_2.ets index 771614964cb87abed844a208f01db95f5722a681..e86db121845df74f5b593719400fc538813bf4a3 100644 --- a/ets2panda/test/ast/parser/ets/SmartCast_2.ets +++ b/ets2panda/test/ast/parser/ets/SmartCast_2.ets @@ -21,8 +21,9 @@ function foo(a: boolean | undefined) : int { return 1; } } else { - //a is undefined - if (/* @@ label */a == false) { + //a is undefined, + //according to spec 7.25.9, any entity can be compared to null and undefined + if (a == false) { return 0; } else { return 1; @@ -35,5 +36,3 @@ function main() { foo(false); foo(undefined); } - -/* @@@ label Error TypeError: Bad operand type, the types of the operands must be numeric, same enumeration, or boolean type. */ diff --git a/ets2panda/test/runtime/ets/numeric_equality_with_undefine.ets b/ets2panda/test/runtime/ets/numeric_equality_with_undefine.ets new file mode 100644 index 0000000000000000000000000000000000000000..e9d7483f60ad80b596727dcd7b05c9fcde2d0620 --- /dev/null +++ b/ets2panda/test/runtime/ets/numeric_equality_with_undefine.ets @@ -0,0 +1,36 @@ +/* + * 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 a: number = 5; +if (a == undefined) { + a = 6; +} +assertEQ(a, 5); + +if (a != undefined) { + a = 6; +} +assertEQ(a, 6); + +let b: int = 5; +if (b == undefined) { + b = 6; +} +assertEQ(b, 5); + +if (b != undefined) { + b = 6; +} +assertEQ(b, 6);