diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 716f65e4b265689381f6f4d0dc745543106a8068..016e43c9e5d561f6d18a8326d0167ae20709b249 100644 --- a/ets2panda/ir/expressions/memberExpression.cpp +++ b/ets2panda/ir/expressions/memberExpression.cpp @@ -212,6 +212,14 @@ checker::Type *MemberExpression::TraverseUnionMember(checker::ETSChecker *checke return; } + if (memberType->IsETSMethodType() && memberType->AsETSFunctionType()->CallSignatures().size() > 1U) { + if (!Parent()->IsCallExpression() || Parent()->AsCallExpression()->Callee() != this) { + commonPropType = checker->GlobalTypeError(); + checker->LogError(diagnostic::OVERLOADED_METHOD_AS_VALUE, Start()); + return; + } + } + if (!commonPropType->IsETSMethodType() && !memberType->IsETSMethodType()) { if (!checker->IsTypeIdenticalTo(commonPropType, memberType)) { checker->LogError(diagnostic::MEMBER_TYPE_MISMATCH_ACROSS_UNION, {}, Start()); diff --git a/ets2panda/test/ast/compiler/ets/union_overload_crash.ets b/ets2panda/test/ast/compiler/ets/union_overload_crash.ets new file mode 100644 index 0000000000000000000000000000000000000000..01d75715b7a7840938025bb0989de29ffe62b83d --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/union_overload_crash.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. + */ + +class A { + foo() {} + foo(a : number) {} +} + +type AVoidNumber = A | A + +let a = new A(); + +(a as AVoidNumber).foo !== undefined + +/* @@? 25:1 Error TypeError: Overloaded method is used as value */