From 0e0ed5cf0558702da65595e97af59336ce90a1a2 Mon Sep 17 00:00:00 2001 From: likaizheng Date: Thu, 11 Sep 2025 14:29:59 +0800 Subject: [PATCH] Fix union member method reference error Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICXID7 Signed-off-by: likaizheng --- ets2panda/ir/expressions/memberExpression.cpp | 6 ++++ .../ast/parser/ets/unionMemberReference.ets | 28 +++++++++++++++++++ ets2panda/util/diagnostic/semantic.yaml | 4 +++ 3 files changed, 38 insertions(+) create mode 100644 ets2panda/test/ast/parser/ets/unionMemberReference.ets diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 9bd788ce38..7b9ad9fb97 100644 --- a/ets2panda/ir/expressions/memberExpression.cpp +++ b/ets2panda/ir/expressions/memberExpression.cpp @@ -336,6 +336,12 @@ checker::Type *MemberExpression::AdjustType(checker::ETSChecker *checker, checke return TsType(); } SetTsType(type == nullptr ? checker->GlobalTypeError() : type); + if (objType->IsETSUnionType() && type != nullptr && type->IsETSMethodType() && !type->IsETSArrowType()) { + if (!Parent()->IsCallExpression()) { + checker->LogError(diagnostic::UNION_MEMBER_METHOD_REFERENCE, {}, Start()); + SetTsType(checker->GlobalTypeError()); + } + } return TsType(); } diff --git a/ets2panda/test/ast/parser/ets/unionMemberReference.ets b/ets2panda/test/ast/parser/ets/unionMemberReference.ets new file mode 100644 index 0000000000..2f9e9716af --- /dev/null +++ b/ets2panda/test/ast/parser/ets/unionMemberReference.ets @@ -0,0 +1,28 @@ +/* + * 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. + */ + +interface AType { + f(): void; +} + +interface BType { + f(): void; +} + +declare const x: AType | BType; + +(x as AType | BType).f; + +/* @@? 26:1 Error TypeError: Union member method reference is not supported and cannot be treated as a callable method */ \ No newline at end of file diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index bb8e6f39ec..39be3ed1b1 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -1417,6 +1417,10 @@ semantic: id: 245 message: "Type {} is illegal in union member expression." +- name: UNION_MEMBER_METHOD_REFERENCE + id: 186623 + message: "Union member method reference is not supported and cannot be treated as a callable method" + - name: UNION_NONCONSTRUCTIBLE id: 300 message: "The union type is not constructible." -- Gitee