From 300605e90ace6cf17154eb3a7b4bb988757f23b0 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Thu, 7 Aug 2025 11:26:35 +0800 Subject: [PATCH] Overriding private methods causes CTE. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICRQLC Signed-off-by: yp9522 --- .../compiler/ets/override_private_method.ets | 39 +++++++++++++++ .../runtime/ets/override_private_method.ets | 47 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/override_private_method.ets create mode 100644 ets2panda/test/runtime/ets/override_private_method.ets diff --git a/ets2panda/test/ast/compiler/ets/override_private_method.ets b/ets2panda/test/ast/compiler/ets/override_private_method.ets new file mode 100644 index 0000000000..c34126e906 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/override_private_method.ets @@ -0,0 +1,39 @@ +/* + * 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 Base { + public public_member() {} + protected protected_member() {} + private private_member() {} +} + +class Derived extends Base { + public override public_member() {} + protected override protected_member() {} + private override private_member/* @@ label */() {} +} + +interface Interface { + public_member(); + private private_member() {} +} + +class Derived2 implements Interface { + public override public_member() {} + private override private_member/* @@ label1 */() {} +} + +/* @@@ label Error TypeError: Method private_member(): void in Derived not overriding any method */ +/* @@@ label1 Error TypeError: Method private_member(): void in Derived2 not overriding any method */ diff --git a/ets2panda/test/runtime/ets/override_private_method.ets b/ets2panda/test/runtime/ets/override_private_method.ets new file mode 100644 index 0000000000..173b5e7086 --- /dev/null +++ b/ets2panda/test/runtime/ets/override_private_method.ets @@ -0,0 +1,47 @@ +/* + * 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 Base { + public public_member(): int { + return 10; + } + protected protected_member(): int { + return 20; + } + private private_member(): int { // Subclasses can't override parent's private member + return 30; + } +} + +interface Interface { + public_member(): int; + private private_member(): int {} +} + +class Derived extends Base implements Interface { + public override public_member(): int { + return 1; + } + protected override protected_member(): int { + return 2; + } +} + +function main() { + let base: Base = new Base(); + let derived: Base = new Derived(); + arktest.assertEQ(base.public_member(), 10); + arktest.assertEQ(derived.public_member(), 1); +} -- Gitee