From 540877bc9c36ffb10daa08f0beda2afa9d98f756 Mon Sep 17 00:00:00 2001 From: oh-rgx Date: Wed, 13 Aug 2025 14:24:58 +0800 Subject: [PATCH] Fix union type crash Issue: #ICSQFT Signed-off-by: oh-rgx --- ets2panda/ir/expressions/memberExpression.cpp | 6 ++++ .../test/ast/compiler/ets/union_type.ets | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/union_type.ets diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 016e43c9e5..0137fb8c31 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 0000000000..9e3cb3e06d --- /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. */ -- Gitee