From 370e3fe735a77d22454870d5a41da218adf89dac Mon Sep 17 00:00:00 2001 From: daizihan Date: Sun, 22 Jun 2025 10:46:18 +0800 Subject: [PATCH] Fix late iniit bug Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICGW4R?from=project-issue Signed-off-by: daizihan --- .../lowering/ets/lateInitialization.cpp | 18 +++++----- ...e_initialization_with_generic_field_01.ets | 2 +- .../class_late_initlization_with_export.ets | 34 +++++++++++++++++++ .../interfaceToExport/interfaceToExport.ets | 19 +++++++++++ 4 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 ets2panda/test/runtime/ets/fields_with_late_initialization/class_late_initlization_with_export.ets create mode 100644 ets2panda/test/runtime/ets/fields_with_late_initialization/interfaceToExport/interfaceToExport.ets diff --git a/ets2panda/compiler/lowering/ets/lateInitialization.cpp b/ets2panda/compiler/lowering/ets/lateInitialization.cpp index 108d21c318..6c57beb75e 100644 --- a/ets2panda/compiler/lowering/ets/lateInitialization.cpp +++ b/ets2panda/compiler/lowering/ets/lateInitialization.cpp @@ -41,6 +41,7 @@ ir::ClassProperty *TransformerClassProperty(public_lib::Context *ctx, ir::ClassP property->SetTypeAnnotation(typeAnnotation); property->SetTsType(annotationType); property->Key()->Variable()->SetTsType(annotationType); + property->ClearModifier(ir::ModifierFlags::DEFINITE); return property; } @@ -83,15 +84,6 @@ static ir::AstNode *TransformerMemberExpression(ir::MemberExpression *memberExpr bool LateInitializationConvert::PerformForModule(public_lib::Context *ctx, parser::Program *program) { - program->Ast()->TransformChildrenRecursively( - [ctx](ir::AstNode *node) -> AstNodePtr { - if (node->IsClassProperty() && node->IsDefinite()) { - return TransformerClassProperty(ctx, node->AsClassProperty()); - } - return node; - }, - Name()); - program->Ast()->TransformChildrenRecursively( [ctx](ir::AstNode *node) -> AstNodePtr { if (node->IsMemberExpression()) { @@ -109,6 +101,14 @@ bool LateInitializationConvert::PerformForModule(public_lib::Context *ctx, parse }, Name()); + program->Ast()->TransformChildrenRecursively( + [ctx](ir::AstNode *node) -> AstNodePtr { + if (node->IsClassProperty() && node->IsDefinite()) { + return TransformerClassProperty(ctx, node->AsClassProperty()); + } + return node; + }, + Name()); return true; } } // namespace ark::es2panda::compiler diff --git a/ets2panda/test/runtime/ets/fields_with_late_initialization/class_late_initialization_with_generic_field_01.ets b/ets2panda/test/runtime/ets/fields_with_late_initialization/class_late_initialization_with_generic_field_01.ets index e91a1228bf..d024fdd770 100644 --- a/ets2panda/test/runtime/ets/fields_with_late_initialization/class_late_initialization_with_generic_field_01.ets +++ b/ets2panda/test/runtime/ets/fields_with_late_initialization/class_late_initialization_with_generic_field_01.ets @@ -22,6 +22,6 @@ function main(): void { const gen = new Generic(); console.log(gen.genericField); } catch (e) { - arktest.assertTrue(e instanceof ClassCastError) + arktest.assertTrue(e instanceof NullPointerError) } } \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/fields_with_late_initialization/class_late_initlization_with_export.ets b/ets2panda/test/runtime/ets/fields_with_late_initialization/class_late_initlization_with_export.ets new file mode 100644 index 0000000000..9424c15917 --- /dev/null +++ b/ets2panda/test/runtime/ets/fields_with_late_initialization/class_late_initlization_with_export.ets @@ -0,0 +1,34 @@ +/* + * 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. + */ + +/*--- + files: ['./interfaceToExport/interfaceToExport.ets'] +---*/ + +import { A } from './interfaceToExport/interfaceToExport.ets'; + +class Aimpl implements A { + a: number = 0; + b!: string; +} + +function main() { + try { + let a: Aimpl = new Aimpl(); + console.log('a.b = ' + a.b); + } catch (e) { + arktest.assertTrue(e instanceof NullPointerError); + } +} diff --git a/ets2panda/test/runtime/ets/fields_with_late_initialization/interfaceToExport/interfaceToExport.ets b/ets2panda/test/runtime/ets/fields_with_late_initialization/interfaceToExport/interfaceToExport.ets new file mode 100644 index 0000000000..d188f56498 --- /dev/null +++ b/ets2panda/test/runtime/ets/fields_with_late_initialization/interfaceToExport/interfaceToExport.ets @@ -0,0 +1,19 @@ +/* + * 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. + */ + +export interface A { + a: number; + b: string; +} \ No newline at end of file -- Gitee