From af4c0aa5aa54f59acc107f7c31f7f704c6355a37 Mon Sep 17 00:00:00 2001 From: Gabor Aron Takacs Date: Thu, 10 Jul 2025 13:57:24 +0200 Subject: [PATCH] Add diagnostic for for-in loop Fixes #23686 internal issue. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICLIJR Change-Id: If24fa5e45928a679fd2d718e4c4d602fc1e80441 Signed-off-by: Gabor Aron Takacs --- ets2panda/parser/ETSparserStatements.cpp | 2 +- .../ets/FixedArray/InvalidStatements3.ets | 2 +- .../ast/parser/ets/InvalidStatements3.ets | 4 ++-- ets2panda/test/ast/parser/ets/for_in.ets | 22 +++++++++++++++++++ ets2panda/util/diagnostic/syntax.yaml | 5 +++++ 5 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 ets2panda/test/ast/parser/ets/for_in.ets diff --git a/ets2panda/parser/ETSparserStatements.cpp b/ets2panda/parser/ETSparserStatements.cpp index 0b8fc5d913..1665d4b1a5 100644 --- a/ets2panda/parser/ETSparserStatements.cpp +++ b/ets2panda/parser/ETSparserStatements.cpp @@ -323,7 +323,7 @@ bool ETSParser::ValidateLabeledStatement(lexer::TokenType type) bool ETSParser::ValidateForInStatement() { - LogUnexpectedToken(lexer::TokenType::KEYW_IN); + LogError(diagnostic::ERROR_ARKTS_NO_FOR_IN_LOOP); return false; } diff --git a/ets2panda/test/ast/parser/ets/FixedArray/InvalidStatements3.ets b/ets2panda/test/ast/parser/ets/FixedArray/InvalidStatements3.ets index 29c5aced21..ef5fb16052 100644 --- a/ets2panda/test/ast/parser/ets/FixedArray/InvalidStatements3.ets +++ b/ets2panda/test/ast/parser/ets/FixedArray/InvalidStatements3.ets @@ -60,4 +60,4 @@ for (let i = 1 in [0, 1, 2]) {} /* @@? 38:13 Error SyntaxError: Unexpected token '@'. */ /* @@? 38:14 Error TypeError: Cannot find type 'annotate'. */ /* @@? 42:16 Error SyntaxError: for-in loop variable declaration may not have an initializer. */ -/* @@? 42:16 Error SyntaxError: Unexpected token 'in'. */ +/* @@? 42:16 Error SyntaxError: 'for ... in' loop is not supported, please use regular 'for' or 'for ... of ...' loop to iterate through arrays and iterable objects. */ diff --git a/ets2panda/test/ast/parser/ets/InvalidStatements3.ets b/ets2panda/test/ast/parser/ets/InvalidStatements3.ets index 85b2082bf4..308ab7542c 100644 --- a/ets2panda/test/ast/parser/ets/InvalidStatements3.ets +++ b/ets2panda/test/ast/parser/ets/InvalidStatements3.ets @@ -55,9 +55,9 @@ for (let i = 1 in [0, 1, 2]) {} /* @@? 31:9 Error SyntaxError: Unexpected token 'let'. */ /* @@? 31:13 Error SyntaxError: Unexpected token 'x'. */ /* @@? 34:5 Error SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list. */ -/* @@? 38:5 Error SyntaxError: Unexpected token 'private'. */ /* @@? 38:5 Error SyntaxError: Annotation declaration can not have access modifier. */ +/* @@? 38:5 Error SyntaxError: Unexpected token 'private'. */ /* @@? 38:13 Error SyntaxError: Unexpected token '@'. */ /* @@? 38:14 Error TypeError: Cannot find type 'annotate'. */ /* @@? 42:16 Error SyntaxError: for-in loop variable declaration may not have an initializer. */ -/* @@? 42:16 Error SyntaxError: Unexpected token 'in'. */ +/* @@? 42:16 Error SyntaxError: 'for ... in' loop is not supported, please use regular 'for' or 'for ... of ...' loop to iterate through arrays and iterable objects. */ diff --git a/ets2panda/test/ast/parser/ets/for_in.ets b/ets2panda/test/ast/parser/ets/for_in.ets new file mode 100644 index 0000000000..922bde17d5 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/for_in.ets @@ -0,0 +1,22 @@ +/* + * 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. + */ + +let a: number[] = [1.0, 2.0, 3.0] +for (let i /* @@ label */in a) { +} + +/* @@@ label Error SyntaxError: Variable must be initialized or it's type must be declared. */ +/* @@@ label Error SyntaxError: 'for ... in' loop is not supported, please use regular 'for' or 'for ... of ...' loop to iterate through arrays + and iterable objects. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index a998b7eacc..36dbf07b18 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -213,6 +213,11 @@ syntax: id: 307 message: "'export = ...' syntax is not supported, use regular import/export instead!" +- name: ERROR_ARKTS_NO_FOR_IN_LOOP + id: 6987 + message: "'for ... in' loop is not supported, please use regular 'for' or 'for ... of ...' loop to iterate through arrays + and iterable objects." + - name: ERROR_ARKTS_NO_IMPORT_ASSERTIONS id: 313 message: "Import assertion is not supported, please use the ordinary import syntax instead!" -- Gitee