diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 6f1a8fbd8357124d35c78616907ee3d60d3b859b..bff9f5dda6c8fc4a4386ceb5d0b0293c96b68b03 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 0000000000000000000000000000000000000000..bdf2e5318a9108ff2e1dbbf530be0f7de1f0c031 --- /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 5289ec10e81b61a513e3c0a95e5d7e463016f3d4..48fa844f68cab0808baa1e609a2e89a85ba39289 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