diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 37e084f542d08de88f4da65cf19f7b7704734f2a..aa56941ab57ddcd5a469e019c6f80d10ace9f31d 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 447f3d60b0d04159cdb55e6fec4f6799e1184fd8..f55d979c546d31f9e31d12eb03120a54d7b3b0d2 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 2f30f11b71da592323d43eb8dcd95e2219cf8ae9..619d732787faf4c6680eb7c5fb04068cd8f4b56d 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 0000000000000000000000000000000000000000..c077a76fd0de0cd1798da4d69b10e1ffa6f67022 --- /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 04581adbe9bc262665055db4ad73b3313a10c0be..7859381ee0878b450b28fb64d5df6d0019173d66 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 ee4c76c29c6417bd9ab87822f613085e9f4bc595..7b05b2d7a85359b0dc5e0a1caded5e053e6885eb 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 1de6fd1a681f636dec326946f514533468285ca9..f712cd751ed6c51db6bca824a9cdbdf46e498b93 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 037f25aee83f19f91af386bbb8045498912a4d48..9908200d2f0b66ee0ec78e07d2bfaac1caf8e93a 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."