diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index ab87ed9b46aab0375ecb4a7396fca515aedbbab6..6a3b0f0ee353af5b0cbb7bab6b260238d91163ce 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -3458,6 +3458,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(); @@ -3468,6 +3480,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 0000000000000000000000000000000000000000..eba0bf72a01448f42152f13ac183fc770373a154 --- /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