From 800dbd6d95b441c062042ec4846247ecfd5a2e2e Mon Sep 17 00:00:00 2001 From: MuSilk Date: Fri, 25 Jul 2025 09:46:45 +0800 Subject: [PATCH] Cherrypick !7521 from 0702 to 0728 Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICRGHB Signed-off-by: zhaoshuting --- ets2panda/checker/ETSAnalyzer.cpp | 6 ++++- .../ets/return_protected_function.ets | 26 +++++++++++++++++++ ets2panda/util/diagnostic/semantic.yaml | 4 +-- 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/return_protected_function.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 6f1a8fbd83..bff9f5dda6 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1842,7 +1842,11 @@ static Type *TransformTypeForMethodReference(ETSChecker *checker, ir::Expression auto &signatures = functionType->CallSignatures(); if (signatures.at(0)->HasSignatureFlag(SignatureFlags::PRIVATE)) { - checker->LogError(diagnostic::PRIVATE_METHOD_AS_VALUE, getUseSite()); + checker->LogError(diagnostic::PRIVATE_OR_PROTECTED_METHOD_AS_VALUE, {"Private"}, getUseSite()); + return checker->GlobalTypeError(); + } + if (signatures.at(0)->HasSignatureFlag(SignatureFlags::PROTECTED)) { + checker->LogError(diagnostic::PRIVATE_OR_PROTECTED_METHOD_AS_VALUE, {"Protected"}, getUseSite()); return checker->GlobalTypeError(); } diff --git a/ets2panda/test/ast/compiler/ets/return_protected_function.ets b/ets2panda/test/ast/compiler/ets/return_protected_function.ets new file mode 100644 index 0000000000..bdf2e5318a --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/return_protected_function.ets @@ -0,0 +1,26 @@ +/* + * 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{ + protected static foo(){ + return "foo"; + } + static getfoo(){ + return A./* @@ label */foo; + } +}; + +/* @@@ label Error TypeError: Protected method is used as value */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 5289ec10e8..48fa844f68 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -1198,9 +1198,9 @@ semantic: id: 266 message: "Precondition check failed for {}" -- name: PRIVATE_METHOD_AS_VALUE +- name: PRIVATE_OR_PROTECTED_METHOD_AS_VALUE id: 306 - message: "Private method is used as value" + message: "{} method is used as value" - name: PROPERTY_CAPTURE id: 146 -- Gitee