From 41bbda67b3abf89138c7a46d727c5d038d58a88d Mon Sep 17 00:00:00 2001 From: xingshunxiang Date: Thu, 7 Aug 2025 18:15:08 +0800 Subject: [PATCH] Fix CTE when assign class prop in namespace Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICRWRO?from=project-issue Description: the unexpected situation show in the test case Reason: in typeCheckingHelpers.cpp, the function ETSChecker::IterateInVariableContext should find the classType of the nearest classScope and then set it as the containingClass in only one time. Tests: ninja tests passed tests/tests-u-runner/runner.sh --ets-cts --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-func-tests --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --astchecker --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-runtime --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --parser --no-js --show-progress --build-dir x64.release --processes=all passed Signed-off-by: xingshunxiang --- ets2panda/checker/ets/typeCheckingHelpers.cpp | 3 +- .../ets/namespaceClassPropertyAssignTest.ets | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/runtime/ets/namespaceClassPropertyAssignTest.ets diff --git a/ets2panda/checker/ets/typeCheckingHelpers.cpp b/ets2panda/checker/ets/typeCheckingHelpers.cpp index 79205c2b9e..238e29964c 100644 --- a/ets2panda/checker/ets/typeCheckingHelpers.cpp +++ b/ets2panda/checker/ets/typeCheckingHelpers.cpp @@ -556,7 +556,8 @@ void ETSChecker::IterateInVariableContext(varbinder::Variable *const var) } ES2PANDA_ASSERT(classDef->TsType()); - if (!containingClass->IsTypeError()) { + // Note: find the classType of the nearest classScope and then set it as the containingClass. + if (!containingClass->IsTypeError() && Context().ContainingClass() == nullptr) { Context().SetContainingClass(containingClass->AsETSObjectType()); } } diff --git a/ets2panda/test/runtime/ets/namespaceClassPropertyAssignTest.ets b/ets2panda/test/runtime/ets/namespaceClassPropertyAssignTest.ets new file mode 100644 index 0000000000..1b4aec9afd --- /dev/null +++ b/ets2panda/test/runtime/ets/namespaceClassPropertyAssignTest.ets @@ -0,0 +1,32 @@ +/* + * 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. + */ + +namespace ns { + export interface A { + a: int; + f: () => void; + } + + export class AC implements A { + a: int = 100; + f: () => void = (): void => { + this.a = 200 + } + } +} + +let ac: ns.AC = new ns.AC(); +ac.f(); +arktest.assertEQ(ac.a, 200) -- Gitee