From c1f3622046ff978b356445dca201482832b6495b Mon Sep 17 00:00:00 2001 From: oh-rgx Date: Tue, 22 Jul 2025 16:40:44 +0800 Subject: [PATCH] Remove nullptr in parser Issue: #ICO1BI Signed-off-by: oh-rgx --- ets2panda/checker/ets/function.cpp | 3 --- ets2panda/parser/ETSFormattedParser.cpp | 4 ++-- ets2panda/parser/ETSparser.cpp | 4 ++-- ets2panda/parser/ETSparserExpressions.cpp | 2 +- ets2panda/parser/ETSparserTypes.cpp | 2 ++ ets2panda/parser/expressionParser.cpp | 1 + ets2panda/test/ast/compiler/ets/fuzzingtest1.ets | 8 ++++---- .../ets/returnTypeAnnotation_typeParams_nullptr.ets | 2 -- .../parser/ets/FixedArray/functionTypeParam_neg2.ets | 9 +++++++-- .../ast/parser/ets/FixedArray/unexpected_token_38.ets | 6 ++---- .../annotationDecl_with_access_modifier03.ets | 1 - .../test/ast/parser/ets/functionTypeParam_neg2.ets | 9 +++++++-- .../ast/parser/ets/function_implicit_return_type.ets | 8 +++----- .../test/ast/parser/ets/invalid_decorator_usage.ets | 10 ++++------ ets2panda/test/ast/parser/ets/unexpected_token_38.ets | 6 ++---- 15 files changed, 37 insertions(+), 38 deletions(-) diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 317fc2afcbd..89d5693507b 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -135,9 +135,6 @@ bool ETSChecker::EnhanceSubstitutionForType(const ArenaVector &typeParam if (paramType->IsETSArrayType()) { return EnhanceSubstitutionForArray(typeParams, paramType->AsETSArrayType(), argumentType, substitution); } - if (paramType->IsETSFunctionType()) { - return EnhanceSubstitutionForFunction(typeParams, paramType->AsETSFunctionType(), argumentType, substitution); - } return true; } diff --git a/ets2panda/parser/ETSFormattedParser.cpp b/ets2panda/parser/ETSFormattedParser.cpp index b3acf5e5596..b999b9a7bf2 100644 --- a/ets2panda/parser/ETSFormattedParser.cpp +++ b/ets2panda/parser/ETSFormattedParser.cpp @@ -134,7 +134,7 @@ ir::Identifier *ETSParser::ParseIdentifierFormatPlaceholder(std::optionalGetToken().Start()); - return nullptr; + return AllocBrokenExpression(Lexer()->GetToken().Loc()); } nodeFormat = GetFormatPlaceholderType(); @@ -482,7 +482,7 @@ ir::Statement *ETSParser::CreateClassDeclaration(std::string_view sourceCode, bo } default: { LogUnexpectedToken(Lexer()->GetToken()); - return nullptr; + return AllocBrokenStatement(lexer->GetToken().Start()); } } } diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index d9970074638..2708b82ee19 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -753,7 +753,7 @@ ir::Statement *ETSParser::ParseTypeDeclarationAbstractFinal(bool allowStatic, ir } LogUnexpectedToken(Lexer()->GetToken()); - return nullptr; + return AllocBrokenStatement(Lexer()->GetToken().Loc()); } ir::Statement *ETSParser::ParseTypeDeclaration(bool allowStatic) @@ -1682,7 +1682,7 @@ ir::Expression *ETSParser::ParseFunctionParameterAnnotations() auto annotations = ParseAnnotations(false); auto savePos = Lexer()->GetToken().Start(); ir::Expression *result = ParseFunctionParameter(); - if (result != nullptr) { + if (result != nullptr && !result->IsBrokenExpression()) { ApplyAnnotationsToNode(result, std::move(annotations), savePos); } return result; diff --git a/ets2panda/parser/ETSparserExpressions.cpp b/ets2panda/parser/ETSparserExpressions.cpp index 2a7e6dc5d02..59775285a2f 100644 --- a/ets2panda/parser/ETSparserExpressions.cpp +++ b/ets2panda/parser/ETSparserExpressions.cpp @@ -65,7 +65,7 @@ ir::Expression *ETSParser::ParseFunctionParameterExpression(ir::AnnotatedExpress auto defaultValue = ParseExpression(); if (!paramIdent->IsIdentifier()) { LogError(diagnostic::IDENTIFIER_EXPECTED); - return nullptr; + return AllocBrokenExpression(Lexer()->GetToken().Loc()); } paramExpression = AllocNode(paramIdent->AsIdentifier(), defaultValue, Allocator()); diff --git a/ets2panda/parser/ETSparserTypes.cpp b/ets2panda/parser/ETSparserTypes.cpp index 209397b1dfd..17dec93abcd 100644 --- a/ets2panda/parser/ETSparserTypes.cpp +++ b/ets2panda/parser/ETSparserTypes.cpp @@ -199,6 +199,7 @@ ir::TypeNode *ETSParser::ParseFunctionType(TypeAnnotationParsingOptions *options if (!Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_ARROW)) { if (((*options) & TypeAnnotationParsingOptions::REPORT_ERROR) != 0) { LogExpectedToken(lexer::TokenType::PUNCTUATOR_ARROW); + return AllocBrokenType(Lexer()->GetToken().Loc()); } return nullptr; } @@ -436,6 +437,7 @@ std::pair ETSParser::GetTypeAnnotationFromParentheses( if (!Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_LEFT_PARENTHESIS)) { if (((*options) & TypeAnnotationParsingOptions::REPORT_ERROR) != 0) { LogExpectedToken(lexer::TokenType::PUNCTUATOR_LEFT_PARENTHESIS); + return {AllocBrokenType(Lexer()->GetToken().Loc()), false}; } return {nullptr, false}; } diff --git a/ets2panda/parser/expressionParser.cpp b/ets2panda/parser/expressionParser.cpp index 89cd7fa0698..0b86c0984d7 100644 --- a/ets2panda/parser/expressionParser.cpp +++ b/ets2panda/parser/expressionParser.cpp @@ -1498,6 +1498,7 @@ ir::Expression *ParserImpl::ParseOptionalChain(ir::Expression *leftSideExpr) if (tokenType == lexer::TokenType::PUNCTUATOR_BACK_TICK || lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_BACK_TICK) { LogError(diagnostic::TAGGED_TEMPLATE_LITERALS_IN_OPTIONALCHAIN); + return AllocBrokenExpression(lexer_->GetToken().Loc()); } return returnExpression; diff --git a/ets2panda/test/ast/compiler/ets/fuzzingtest1.ets b/ets2panda/test/ast/compiler/ets/fuzzingtest1.ets index 32a58853e99..08bf93ddf63 100644 --- a/ets2panda/test/ast/compiler/ets/fuzzingtest1.ets +++ b/ets2panda/test/ast/compiler/ets/fuzzingtest1.ets @@ -16,7 +16,7 @@ // the test case is from fuzzer. /* @@ label */fuzzz./* @@ label2 */@@/* @@ label3 */@@ -/* @@@ label Error TypeError: Unresolved reference fuzzz */ -/* @@@ label2 Error SyntaxError: There is no any node to insert at the placeholder position. */ -/* @@@ label2 Error SyntaxError: Identifier expected, got '@@'. */ -/* @@@ label3 Error SyntaxError: Unexpected token '@@'. */ \ No newline at end of file +/* @@? 17:15 Error TypeError: Unresolved reference fuzzz */ +/* @@? 17:36 Error SyntaxError: There is no any node to insert at the placeholder position. */ +/* @@? 17:36 Error SyntaxError: Unexpected token '@@'. */ +/* @@? 17:53 Error SyntaxError: Unexpected token '@@'. */ diff --git a/ets2panda/test/ast/compiler/ets/returnTypeAnnotation_typeParams_nullptr.ets b/ets2panda/test/ast/compiler/ets/returnTypeAnnotation_typeParams_nullptr.ets index 6308a252818..64f4958d341 100755 --- a/ets2panda/test/ast/compiler/ets/returnTypeAnnotation_typeParams_nullptr.ets +++ b/ets2panda/test/ast/compiler/ets/returnTypeAnnotation_typeParams_nullptr.ets @@ -19,7 +19,5 @@ class Arr { /* @@? 16:11 Error TypeError: Only abstract or native methods can't have body. */ /* @@? 16:24 Error TypeError: FixedArray must have only one type parameter. */ /* @@? 16:37 Error SyntaxError: Unexpected token, expected '=>'. */ -/* @@? 16:37 Error SyntaxError: Unexpected token, expected '('. */ -/* @@? 16:37 Error SyntaxError: Invalid Type. */ /* @@? 16:37 Error SyntaxError: Unexpected token, expected '>'. */ /* @@? 16:37 Error SyntaxError: Unexpected token '>'. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/FixedArray/functionTypeParam_neg2.ets b/ets2panda/test/ast/parser/ets/FixedArray/functionTypeParam_neg2.ets index 49a08ff7e5c..3c8a14a8176 100644 --- a/ets2panda/test/ast/parser/ets/FixedArray/functionTypeParam_neg2.ets +++ b/ets2panda/test/ast/parser/ets/FixedArray/functionTypeParam_neg2.ets @@ -26,11 +26,16 @@ declare class Environment { /* @@? 17:23 Error SyntaxError: Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly! */ /* @@? 18:25 Error SyntaxError: Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly! */ /* @@? 19:25 Error SyntaxError: Invalid Type. */ +/* @@? 21:16 Error TypeError: Native and Declare methods should have explicit return type. */ /* @@? 21:20 Error SyntaxError: Invalid Type. */ /* @@? 21:28 Error SyntaxError: Invalid Type. */ /* @@? 21:37 Error SyntaxError: Unexpected token, expected '=>'. */ -/* @@? 21:37 Error SyntaxError: Unexpected token, expected '('. */ -/* @@? 21:37 Error SyntaxError: Invalid Type. */ +/* @@? 21:39 Error SyntaxError: Unexpected token '--'. */ +/* @@? 21:41 Error SyntaxError: Unexpected token '-'. */ +/* @@? 21:42 Error SyntaxError: Unexpected token ','. */ +/* @@? 21:43 Error SyntaxError: Unexpected token ')'. */ +/* @@? 21:44 Error SyntaxError: Unexpected token ':'. */ +/* @@? 21:45 Error SyntaxError: void is a predefined type, cannot be used as an identifier */ /* @@? 22:20 Error SyntaxError: Invalid Type. */ /* @@? 23:25 Error SyntaxError: Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly! */ /* @@? 23:36 Error SyntaxError: Unexpected token ','. */ diff --git a/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_38.ets b/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_38.ets index f46c984335d..311e9b26933 100644 --- a/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_38.ets +++ b/ets2panda/test/ast/parser/ets/FixedArray/unexpected_token_38.ets @@ -25,10 +25,8 @@ let func3: (f: (a: number, b: string) => FixedArray): FixedArray /* @@? 19:8 Error TypeError: 'void' used as type annotation. */ /* @@? 22:61 Error SyntaxError: Unexpected token, expected '=>'. */ -/* @@? 22:61 Error SyntaxError: Unexpected token, expected '('. */ -/* @@? 22:61 Error SyntaxError: Invalid Type. */ -/* @@? 22:61 Error SyntaxError: Unexpected token '('. */ +/* @@? 22:61 Error SyntaxError: Unexpected token '=>'. */ +/* @@? 22:63 Error SyntaxError: Unexpected token 'FixedArray'. */ /* @@? 22:63 Error TypeError: Unresolved reference FixedArray */ /* @@? 22:82 Error SyntaxError: Unexpected token, expected '('. */ /* @@? 22:116 Error SyntaxError: Unexpected token, expected ',' or ')'. */ -/* @@? 22:116 Error SyntaxError: Unexpected token, expected ')'. */ diff --git a/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_with_access_modifier03.ets b/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_with_access_modifier03.ets index c6d1285bf8a..9128014805b 100644 --- a/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_with_access_modifier03.ets +++ b/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_with_access_modifier03.ets @@ -19,4 +19,3 @@ /* @@@ label Error SyntaxError: Unexpected token 'static'. */ /* @@@ label1 Error SyntaxError: Unexpected token '@'. */ -/* @@? 15:38 Error SyntaxError: Annotations can only be declared at the top level. */ diff --git a/ets2panda/test/ast/parser/ets/functionTypeParam_neg2.ets b/ets2panda/test/ast/parser/ets/functionTypeParam_neg2.ets index 3f31cfcb8cf..e7813756374 100644 --- a/ets2panda/test/ast/parser/ets/functionTypeParam_neg2.ets +++ b/ets2panda/test/ast/parser/ets/functionTypeParam_neg2.ets @@ -26,11 +26,16 @@ declare class Environment { /* @@? 17:23 Error SyntaxError: Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly! */ /* @@? 18:25 Error SyntaxError: Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly! */ /* @@? 19:25 Error SyntaxError: Invalid Type. */ +/* @@? 21:16 Error TypeError: Native and Declare methods should have explicit return type. */ /* @@? 21:20 Error SyntaxError: Invalid Type. */ /* @@? 21:28 Error SyntaxError: Invalid Type. */ /* @@? 21:37 Error SyntaxError: Unexpected token, expected '=>'. */ -/* @@? 21:37 Error SyntaxError: Unexpected token, expected '('. */ -/* @@? 21:37 Error SyntaxError: Invalid Type. */ +/* @@? 21:39 Error SyntaxError: Unexpected token '--'. */ +/* @@? 21:41 Error SyntaxError: Unexpected token '-'. */ +/* @@? 21:42 Error SyntaxError: Unexpected token ','. */ +/* @@? 21:43 Error SyntaxError: Unexpected token ')'. */ +/* @@? 21:44 Error SyntaxError: Unexpected token ':'. */ +/* @@? 21:45 Error SyntaxError: void is a predefined type, cannot be used as an identifier */ /* @@? 22:20 Error SyntaxError: Invalid Type. */ /* @@? 23:25 Error SyntaxError: Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly! */ /* @@? 23:36 Error SyntaxError: Unexpected token ','. */ diff --git a/ets2panda/test/ast/parser/ets/function_implicit_return_type.ets b/ets2panda/test/ast/parser/ets/function_implicit_return_type.ets index 74124393616..08913b18bbb 100644 --- a/ets2panda/test/ast/parser/ets/function_implicit_return_type.ets +++ b/ets2panda/test/ast/parser/ets/function_implicit_return_type.ets @@ -24,8 +24,6 @@ let void6: (i: int ) /* @@ label */= void1 /* @@ label1 */; // CTE; need function main() {} -/* @@@ label Error SyntaxError: Unexpected token, expected '=>'. */ -/* @@@ label Error SyntaxError: Unexpected token, expected '('. */ -/* @@@ label Error SyntaxError: Invalid Type. */ -/* @@@ label Error SyntaxError: Unexpected token '('. */ -/* @@@ label1 Error SyntaxError: Unexpected token, expected ')'. */ +/* @@? 23:43 Error SyntaxError: Unexpected token, expected '=>'. */ +/* @@? 23:43 Error SyntaxError: Unexpected token '=>'. */ +/* @@? 23:45 Error SyntaxError: Unexpected token 'void1'. */ diff --git a/ets2panda/test/ast/parser/ets/invalid_decorator_usage.ets b/ets2panda/test/ast/parser/ets/invalid_decorator_usage.ets index 30b54d0ce59..ce1345d0267 100644 --- a/ets2panda/test/ast/parser/ets/invalid_decorator_usage.ets +++ b/ets2panda/test/ast/parser/ets/invalid_decorator_usage.ets @@ -20,15 +20,13 @@ /* @@? 16:5 Error TypeError: 'M' type does not exist. */ /* @@? 16:16 Error TypeError: Only abstract or native methods can't have body. */ /* @@? 16:29 Error SyntaxError: Unexpected 'this' keyword in non-receiver context. */ -/* @@? 16:44 Error SyntaxError: Unexpected token, expected '('. */ +/* @@? 16:40 Error SyntaxError: Annotations are not allowed on this type of declaration. */ /* @@? 16:44 Error SyntaxError: Unexpected token, expected ',' or ')'. */ -/* @@? 16:44 Error SyntaxError: Invalid Type. */ -/* @@? 16:44 Error SyntaxError: Unexpected token, expected '('. */ -/* @@? 16:44 Error SyntaxError: Unexpected token, expected '=>'. */ -/* @@? 16:44 Error SyntaxError: Unexpected token, expected ',' or ')'. */ -/* @@? 16:44 Error SyntaxError: Invalid Type. */ /* @@? 16:44 Error SyntaxError: Unexpected token, expected '=>'. */ /* @@? 16:46 Error SyntaxError: Unexpected token ']'. */ +/* @@? 16:46 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 16:46 Error SyntaxError: Invalid Type. */ +/* @@? 16:48 Error SyntaxError: Unexpected token ')'. */ /* @@? 16:50 Error SyntaxError: Unexpected token '=>'. */ /* @@? 16:59 Error SyntaxError: Annotations are not allowed on this type of declaration. */ /* @@? 16:61 Error SyntaxError: Unexpected token ')'. */ diff --git a/ets2panda/test/ast/parser/ets/unexpected_token_38.ets b/ets2panda/test/ast/parser/ets/unexpected_token_38.ets index ca70ddbf817..780e05d9629 100644 --- a/ets2panda/test/ast/parser/ets/unexpected_token_38.ets +++ b/ets2panda/test/ast/parser/ets/unexpected_token_38.ets @@ -25,9 +25,7 @@ let func3: (f: (a: number, b: string) => number[]): number[] => (a: number, b: b /* @@? 19:8 Error TypeError: 'void' used as type annotation. */ /* @@? 22:51 Error SyntaxError: Unexpected token, expected '=>'. */ -/* @@? 22:51 Error SyntaxError: Unexpected token, expected '('. */ -/* @@? 22:51 Error SyntaxError: Invalid Type. */ -/* @@? 22:51 Error SyntaxError: Unexpected token '('. */ +/* @@? 22:51 Error SyntaxError: Unexpected token '=>'. */ +/* @@? 22:53 Error SyntaxError: Unexpected token 'number'. */ /* @@? 22:62 Error SyntaxError: Unexpected token '=>'. */ /* @@? 22:62 Error SyntaxError: Unexpected token. */ -/* @@? 22:96 Error SyntaxError: Unexpected token, expected ')'. */ -- Gitee