diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 016e43c9e5d561f6d18a8326d0167ae20709b249..0137fb8c31ad11e8050c74926352c52cccd298eb 100644 --- a/ets2panda/ir/expressions/memberExpression.cpp +++ b/ets2panda/ir/expressions/memberExpression.cpp @@ -227,6 +227,12 @@ checker::Type *MemberExpression::TraverseUnionMember(checker::ETSChecker *checke return; } + if (!commonPropType->IsETSFunctionType() || !memberType->IsETSFunctionType()) { + checker->LogError(diagnostic::MEMBER_TYPE_MISMATCH_ACROSS_UNION, {}, Start()); + commonPropType = checker->GlobalTypeError(); + return; + } + auto newType = checker->IntersectSignatureSets(commonPropType->AsETSFunctionType(), memberType->AsETSFunctionType()); if (newType->AsETSFunctionType()->CallSignatures().empty()) { diff --git a/ets2panda/test/ast/compiler/ets/union_type.ets b/ets2panda/test/ast/compiler/ets/union_type.ets new file mode 100644 index 0000000000000000000000000000000000000000..9e3cb3e06d0aeb0c298d4e2a16563321655fb514 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/union_type.ets @@ -0,0 +1,30 @@ +/* + * 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() {} +} + +interface B { + foo?: () => void; +} + +function foo1(x : A | B) { + if (x.foo) { + return; + } +} + +/* @@? 25:9 Error TypeError: Member type must be the same for all union objects. */