diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 9bd788ce380e17b04d77aaabf94dca4f03ff28bf..7b9ad9fb97381417223c6e853f316394a91c835e 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 0000000000000000000000000000000000000000..2f9e9716af3bb4fb3b3f263e6814a3dad72b2689 --- /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 bb8e6f39eca5bff4e51557d8cd44aa3786da5065..39be3ed1b18d7ffac0244a4749c0d798dae869a0 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."