From 3c72a74694ed3a8a7020e6234b5cc38961abc316 Mon Sep 17 00:00:00 2001 From: duyuhan Date: Wed, 3 Sep 2025 11:16:39 +0800 Subject: [PATCH] cherry-pick !8101 from 0702 to 0728 Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICWQBS Signed-off-by: duyuhan --- ets2panda/checker/ETSAnalyzer.cpp | 18 ++++++++++++++++++ ets2panda/test/ast/compiler/ets/for_of.ets | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/for_of.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index f58ba747b6..5b6a9a3562 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -3450,6 +3450,18 @@ checker::Type *ETSAnalyzer::Check(ir::ForOfStatement *const st) const return ReturnTypeForStatement(st); } +static bool HasMissingInitOrType(ir::VariableDeclaration *varDecl, ETSChecker *checker) +{ + for (auto *decl : varDecl->Declarators()) { + if (decl->Id()->IsIdentifier() && !decl->Id()->AsIdentifier()->TypeAnnotation() && !decl->Init()) { + auto *ident = decl->Id()->AsIdentifier(); + checker->LogError(diagnostic::MISSING_INIT_OR_TYPE, {}, ident->Start()); + return true; + } + } + return false; +} + checker::Type *ETSAnalyzer::Check(ir::ForUpdateStatement *st) const { ETSChecker *checker = GetETSChecker(); @@ -3460,6 +3472,12 @@ checker::Type *ETSAnalyzer::Check(ir::ForUpdateStatement *st) const if (st->Init() != nullptr) { st->Init()->Check(checker); + if (st->Init()->IsVariableDeclaration()) { + auto *varDecl = st->Init()->AsVariableDeclaration(); + if (HasMissingInitOrType(varDecl, checker)) { + return checker->GlobalTypeError(); + } + } } if (st->Test() != nullptr) { diff --git a/ets2panda/test/ast/compiler/ets/for_of.ets b/ets2panda/test/ast/compiler/ets/for_of.ets new file mode 100644 index 0000000000..eba0bf72a0 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/for_of.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +for(let of; ;){ } + +/* @@? for_of.ets:16:9 Error SyntaxError: Variable must be initialized or it's type must be declared. */ \ No newline at end of file -- Gitee