From 4c42edd26a901061a0a4634d29d7df45033a1fe8 Mon Sep 17 00:00:00 2001 From: zmw Date: Wed, 16 Jul 2025 11:42:30 +0800 Subject: [PATCH] Fix anno decl dot empty assert fail Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICMN6D Description: Fix anno decl dot empty assert fail Signed-off-by: zmw Change-Id: I8f72911038b127ef6eb4408e760605a601a45168 --- ets2panda/checker/ETSAnalyzer.cpp | 9 ++++--- .../ir/statements/annotationDeclaration.cpp | 3 +-- ets2panda/parser/ETSparserAnnotations.cpp | 26 ++++++++++++------- .../ets/annotation_decl_dot_empty.ets | 21 +++++++++++++++ .../ets/annotation_dot_with_empty.ets | 1 + .../ast/compiler/ets/binary_operator_neg.ets | 7 ++--- .../parser/ets/interface_parser_error_1.ets | 11 ++++---- ets2panda/util/diagnostic/syntax.yaml | 4 +++ 8 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/annotation_decl_dot_empty.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 37e084f542..aa56941ab5 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -2340,9 +2340,12 @@ checker::Type *ETSAnalyzer::Check(ir::AnnotationDeclaration *st) const } } - auto *annoDecl = st->GetBaseName()->Variable()->Declaration()->Node()->AsAnnotationDeclaration(); - if (annoDecl != st && annoDecl->IsDeclare()) { - checker->CheckAmbientAnnotation(st, annoDecl); + auto baseName = st->GetBaseName(); + if (!baseName->IsErrorPlaceHolder()) { + auto *annoDecl = baseName->Variable()->Declaration()->Node()->AsAnnotationDeclaration(); + if (annoDecl != st && annoDecl->IsDeclare()) { + checker->CheckAmbientAnnotation(st, annoDecl); + } } return ReturnTypeForStatement(st); diff --git a/ets2panda/ir/statements/annotationDeclaration.cpp b/ets2panda/ir/statements/annotationDeclaration.cpp index 447f3d60b0..f55d979c54 100644 --- a/ets2panda/ir/statements/annotationDeclaration.cpp +++ b/ets2panda/ir/statements/annotationDeclaration.cpp @@ -111,7 +111,6 @@ Identifier *AnnotationDeclaration::GetBaseName() const if (expr_->IsIdentifier()) { return expr_->AsIdentifier(); } - auto *part = expr_->AsETSTypeReference()->Part(); - return part->Name()->AsTSQualifiedName()->Right(); + return expr_->AsETSTypeReference()->Part()->GetIdent(); } } // namespace ark::es2panda::ir \ No newline at end of file diff --git a/ets2panda/parser/ETSparserAnnotations.cpp b/ets2panda/parser/ETSparserAnnotations.cpp index 2f30f11b71..619d732787 100644 --- a/ets2panda/parser/ETSparserAnnotations.cpp +++ b/ets2panda/parser/ETSparserAnnotations.cpp @@ -56,23 +56,29 @@ ir::Expression *ETSParser::ParseAnnotationName() } }; auto save = Lexer()->Save(); + ir::Identifier *ident = nullptr; Lexer()->NextToken(); if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_PERIOD_PERIOD_PERIOD) { Lexer()->Rewind(save); expr = ExpectIdentifier(); - setAnnotation(expr->AsIdentifier()); - return expr; - } - Lexer()->Rewind(save); - if (Lexer()->Lookahead() == '.') { - auto opt = TypeAnnotationParsingOptions::NO_OPTS; - expr = ParseTypeReference(&opt); - setAnnotation(expr->AsETSTypeReference()->Part()->GetIdent()); + ident = expr->AsIdentifier(); } else { - expr = ExpectIdentifier(); - setAnnotation(expr->AsIdentifier()); + Lexer()->Rewind(save); + if (Lexer()->Lookahead() == '.') { + auto opt = TypeAnnotationParsingOptions::NO_OPTS; + expr = ParseTypeReference(&opt); + ident = expr->AsETSTypeReference()->Part()->GetIdent(); + } else { + expr = ExpectIdentifier(); + ident = expr->AsIdentifier(); + } + } + + if (ident->IsBrokenExpression()) { + LogError(diagnostic::INVALID_ANNOTATION_NAME, {}, expr->Start()); } + setAnnotation(ident); return expr; } diff --git a/ets2panda/test/ast/compiler/ets/annotation_decl_dot_empty.ets b/ets2panda/test/ast/compiler/ets/annotation_decl_dot_empty.ets new file mode 100644 index 0000000000..c077a76fd0 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/annotation_decl_dot_empty.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +@interface Message. +{ +} + +/* @@? 16:12 Error SyntaxError: Invalid annotation name. */ +/* @@? 17:1 Error SyntaxError: Identifier expected. */ diff --git a/ets2panda/test/ast/compiler/ets/annotation_dot_with_empty.ets b/ets2panda/test/ast/compiler/ets/annotation_dot_with_empty.ets index 04581adbe9..7859381ee0 100644 --- a/ets2panda/test/ast/compiler/ets/annotation_dot_with_empty.ets +++ b/ets2panda/test/ast/compiler/ets/annotation_dot_with_empty.ets @@ -18,6 +18,7 @@ class MyTest { private property1; } +/* @@? 17:6 Error SyntaxError: Invalid annotation name. */ /* @@? 18:5 Error SyntaxError: Identifier expected. */ /* @@? 18:5 Error TypeError: '*ERROR_LITERAL*' is not an annotation. */ /* @@? 18:22 Error SyntaxError: Field type annotation expected. */ diff --git a/ets2panda/test/ast/compiler/ets/binary_operator_neg.ets b/ets2panda/test/ast/compiler/ets/binary_operator_neg.ets index ee4c76c29c..7b05b2d7a8 100644 --- a/ets2panda/test/ast/compiler/ets/binary_operator_neg.ets +++ b/ets2panda/test/ast/compiler/ets/binary_operator_neg.ets @@ -16,6 +16,7 @@ /* @@ label1 */@@/@ /* @@@ label1 Error SyntaxError: Unexpected token '@@'. */ -/* @@? 21:93 Error SyntaxError: Identifier expected, got 'eos'. */ -/* @@? 21:93 Error SyntaxError: Unexpected token 'eos'. */ -/* @@? 21:93 Error SyntaxError: Annotations are not allowed on this type of declaration. */ \ No newline at end of file +/* @@? 22:93 Error SyntaxError: Identifier expected, got 'eos'. */ +/* @@? 22:93 Error SyntaxError: Invalid annotation name. */ +/* @@? 22:93 Error SyntaxError: Unexpected token 'eos'. */ +/* @@? 22:93 Error SyntaxError: Annotations are not allowed on this type of declaration. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/interface_parser_error_1.ets b/ets2panda/test/ast/parser/ets/interface_parser_error_1.ets index 1de6fd1a68..f712cd751e 100644 --- a/ets2panda/test/ast/parser/ets/interface_parser_error_1.ets +++ b/ets2panda/test/ast/parser/ets/interface_parser_error_1.ets @@ -67,13 +67,14 @@ function mdin() { let a = new A(); /* @@? 28:5 Error SyntaxError: Unexpected token 'A'. */ /* @@? 28:7 Error SyntaxError: Unexpected token '{'. */ /* @@? 29:5 Error SyntaxError: Identifier expected, got 'constructor'. */ +/* @@? 29:16 Error SyntaxError: Invalid annotation name. */ /* @@? 29:19 Error SyntaxError: Annotations are not allowed on this type of declaration. */ /* @@? 30:18 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 32:2 Error SyntaxError: Unexpected token '*'. */ /* @@? 33:1 Error SyntaxError: Nested functions are not allowed. */ /* @@? 33:1 Error SyntaxError: Unexpected token 'function'. */ -/* @@? 80:1 Error SyntaxError: Expected '}', got 'eos'. */ -/* @@? 80:1 Error SyntaxError: Expected '}', got 'eos'. */ -/* @@? 80:1 Error SyntaxError: Expected '}', got 'eos'. */ -/* @@? 80:1 Error SyntaxError: Expected '}', got 'eos'. */ -/* @@? 80:1 Error SyntaxError: Expected '}', got 'eos'. */ +/* @@? 81:1 Error SyntaxError: Expected '}', got 'eos'. */ +/* @@? 81:1 Error SyntaxError: Expected '}', got 'eos'. */ +/* @@? 81:1 Error SyntaxError: Expected '}', got 'eos'. */ +/* @@? 81:1 Error SyntaxError: Expected '}', got 'eos'. */ +/* @@? 81:1 Error SyntaxError: Expected '}', got 'eos'. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index 037f25aee8..9908200d2f 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -648,6 +648,10 @@ syntax: id: 159 message: "Invalid accessor." +- name: INVALID_ANNOTATION_NAME + id: 329 + message: "Invalid annotation name." + - name: UNEXPECTED_TOKEN_IN_PRIVATE id: 160 message: "Unexpected token in private field." -- Gitee