diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 016e43c9e5d561f6d18a8326d0167ae20709b249..0ef64ebe13611dbe379df679e95d7691ffc382ae 100644 --- a/ets2panda/ir/expressions/memberExpression.cpp +++ b/ets2panda/ir/expressions/memberExpression.cpp @@ -202,7 +202,8 @@ checker::Type *MemberExpression::TraverseUnionMember(checker::ETSChecker *checke return; } - if (memberType == nullptr) { + if (memberType == nullptr || !(commonPropType->IsETSMethodType() && memberType->IsETSMethodType())) { + commonPropType = checker->GlobalTypeError(); checker->LogError(diagnostic::MEMBER_TYPE_MISMATCH_ACROSS_UNION, {}, Start()); return; } diff --git a/ets2panda/test/ast/compiler/ets/union_method6.ets b/ets2panda/test/ast/compiler/ets/union_method6.ets new file mode 100644 index 0000000000000000000000000000000000000000..c3dc0922a040e49354fd26cf08440910620862b2 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/union_method6.ets @@ -0,0 +1,35 @@ +/* + * 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(x: T, y: U) {} +} + +interface B { + foo?: () => void; +} + +function foo6(x: A | B) { + if ('foo' in x && x.foo) { + return; + } +} + +/* @@? 25:15 Error SyntaxError: Expected ')', got 'identification literal'. */ +/* @@? 25:15 Error TypeError: Unresolved reference in */ +/* @@? 25:18 Error SyntaxError: Unexpected token 'x'. */ +/* @@? 25:23 Error TypeError: Member type must be the same for all union objects. */ +/* @@? 25:28 Error SyntaxError: Unexpected token ')'. */ +/* @@? 25:30 Error SyntaxError: Unexpected token '{'. */