From bbf0cc227d45807ae3489d4c503881b5541fe254 Mon Sep 17 00:00:00 2001 From: oh-rgx Date: Tue, 29 Jul 2025 20:34:22 +0800 Subject: [PATCH] Fix union method crash Issue: #ICPMQO Signed-off-by: oh-rgx --- ets2panda/ir/expressions/memberExpression.cpp | 3 +- .../test/ast/compiler/ets/union_method6.ets | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/union_method6.ets diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 016e43c9e5..0ef64ebe13 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 0000000000..c3dc0922a0 --- /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 '{'. */ -- Gitee