From eb90440ee307335dc51c3f5fbf0fb6802eb18d3e Mon Sep 17 00:00:00 2001 From: yp9522 Date: Thu, 17 Jul 2025 15:14:32 +0800 Subject: [PATCH] Ofix parseforinof nullptr Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICNB8H Signed-off-by: yp9522 --- ets2panda/parser/statementParser.cpp | 11 +++ .../ets/ParserImpl_ParserForInOf_nullptr.ets | 80 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/ParserImpl_ParserForInOf_nullptr.ets diff --git a/ets2panda/parser/statementParser.cpp b/ets2panda/parser/statementParser.cpp index aa5bdc2ae8..ab9aa9d5c4 100644 --- a/ets2panda/parser/statementParser.cpp +++ b/ets2panda/parser/statementParser.cpp @@ -770,6 +770,12 @@ std::tuple ParserImpl::Par ir::Expression *rightNode = nullptr; if (lexer_->GetToken().IsForInOf()) { + if (initNode == nullptr || !initNode->IsVariableDeclaration()) { + return {forKind, rightNode, updateNode}; + } + if (initNode->AsVariableDeclaration()->Declarators().empty()) { + return {forKind, rightNode, updateNode}; + } const ir::VariableDeclarator *varDecl = initNode->AsVariableDeclaration()->Declarators().front(); if (lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_IN) { @@ -863,6 +869,9 @@ std::tuple std::tuple ParserImpl::ParseForInOf( ir::Expression *leftNode, ExpressionParseFlags exprFlags, bool isAwait) { + if (lexer_ == nullptr || leftNode == nullptr) { + return {ForStatementKind::UPDATE, nullptr, nullptr, nullptr}; + } ir::Expression *updateNode = nullptr; ir::Expression *rightNode = nullptr; if (lexer_->GetToken().IsForInOf()) { @@ -994,6 +1003,8 @@ bool ParserImpl::GetCanBeForInOf(ir::Expression *leftNode, ir::AstNode *initNode if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_SEMI_COLON) { lexer_->NextToken(); canBeForInOf = false; + } else if (!initNode->IsVariableDeclaration()) { + LogError(diagnostic::INVALID_LEFT_HAND_IN_FOR_OF); } else if (initNode->AsVariableDeclaration()->Declarators().size() > 1 && lexer_->GetToken().IsForInOf()) { LogError(diagnostic::INVALID_LEFT_HAND_IN_FOR_OF, {}, initNode->AsVariableDeclaration()->Declarators()[1]->Start()); diff --git a/ets2panda/test/ast/compiler/ets/ParserImpl_ParserForInOf_nullptr.ets b/ets2panda/test/ast/compiler/ets/ParserImpl_ParserForInOf_nullptr.ets new file mode 100644 index 0000000000..5cc9e3ef63 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/ParserImpl_ParserForInOf_nullptr.ets @@ -0,0 +1,80 @@ +/* + * 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 + const enum Direction { + __empty = 1, + } + + in instance.CONST, 'Value: 56' +entifiers %} +{% for item in default_value %} +/*--- +desc: Interface fields with specified types +tags: [compile-only, negative] +params: "{{id}}: {{item.type}} = {{item.value}}" +---*/ + +interface Name { + {{ return "sfn_base"; +}}: {{item.type}} = {{item.value}} +} + +{% endfor %} +{% endfor %} + +/* @@? 16:2 Error SyntaxError: Unexpected token '%'. */ +/* @@? 16:4 Error SyntaxError: Unexpected token 'for'. */ +/* @@? 17:3 Error SyntaxError: Expected '(', got 'const'. */ +/* @@? 21:2 Error SyntaxError: Invalid left-hand side in 'for' statement: must have a single binding. */ +/* @@? 21:2 Error SyntaxError: Expected ')', got 'identification literal'. */ +/* @@? 21:2 Error TypeError: Unresolved reference in */ +/* @@? 21:5 Error SyntaxError: Unexpected token 'instance'. */ +/* @@? 21:5 Error TypeError: Unresolved reference instance */ +/* @@? 21:19 Error SyntaxError: Unexpected token ','. */ +/* @@? 21:21 Error SyntaxError: Unexpected token 'Value: 56'. */ +/* @@? 22:1 Error TypeError: Unresolved reference entifiers */ +/* @@? 22:12 Error SyntaxError: Unexpected token '}'. */ +/* @@? 23:2 Error SyntaxError: Unexpected token '%'. */ +/* @@? 23:4 Error SyntaxError: Unexpected token 'for'. */ +/* @@? 23:8 Error SyntaxError: Expected '(', got 'identification literal'. */ +/* @@? 23:16 Error TypeError: Unresolved reference default_value */ +/* @@? 23:16 Error TypeError: 'For-of' statement source expression is not of iterable type. */ +/* @@? 23:31 Error SyntaxError: Unexpected token '}'. */ +/* @@? 30:1 Error SyntaxError: Expected ')', got 'interface'. */ +/* @@? 31:3 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 31:4 Error SyntaxError: Unexpected token, expected ','. */ +/* @@? 31:4 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 31:4 Error SyntaxError: Identifier expected. */ +/* @@? 31:6 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 31:6 Error SyntaxError: Unexpected token, expected ','. */ +/* @@? 31:6 Error SyntaxError: Identifier expected. */ +/* @@? 31:13 Error SyntaxError: Identifier expected. */ +/* @@? 31:13 Error SyntaxError: Unexpected token, expected ','. */ +/* @@? 31:13 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 31:23 Error SyntaxError: Identifier expected. */ +/* @@? 32:3 Error SyntaxError: Unexpected token ':'. */ +/* @@? 32:5 Error SyntaxError: Unexpected token '{'. */ +/* @@? 32:7 Error TypeError: Unresolved reference item */ +/* @@? 32:19 Error SyntaxError: Unexpected token '='. */ +/* @@? 32:21 Error SyntaxError: Unexpected token '{'. */ +/* @@? 35:2 Error SyntaxError: Unexpected token '%'. */ +/* @@? 35:4 Error SyntaxError: Unexpected token 'endfor'. */ +/* @@? 35:4 Error TypeError: Unresolved reference endfor */ +/* @@? 35:12 Error SyntaxError: Unexpected token '}'. */ +/* @@? 36:2 Error SyntaxError: Unexpected token '%'. */ +/* @@? 36:4 Error SyntaxError: Unexpected token 'endfor'. */ +/* @@? 36:12 Error SyntaxError: Unexpected token '}'. */ +/* @@? 81:1 Error SyntaxError: Expected '}', got 'end of stream'. */ -- Gitee