diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 655a54e91b94636ec09877a28918932a445b903b..2fe6c6a4a9e5c2de34726992c18d1cbdf7db804b 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -2901,9 +2901,11 @@ checker::Type *ETSAnalyzer::Check(ir::AnnotationDeclaration *st) const auto baseName = st->GetBaseName(); if (!baseName->IsErrorPlaceHolder()) { - auto *annoDecl = baseName->Variable()->Declaration()->Node()->AsAnnotationDeclaration(); - if (annoDecl != st && annoDecl->IsDeclare()) { - checker->CheckAmbientAnnotation(st, annoDecl); + if (baseName->Variable()->Declaration()->Node()->IsAnnotationDeclaration()) { + auto *annoDecl = baseName->Variable()->Declaration()->Node()->AsAnnotationDeclaration(); + if (annoDecl != st && annoDecl->IsDeclare()) { + checker->CheckAmbientAnnotation(st, annoDecl); + } } } diff --git a/ets2panda/ir/statements/annotationDeclaration.cpp b/ets2panda/ir/statements/annotationDeclaration.cpp index 6444bc9758021d6099910af5b5bc2d204e8c043c..2011ac161a5cd7b5f218530f4357d71ff18f0cf6 100644 --- a/ets2panda/ir/statements/annotationDeclaration.cpp +++ b/ets2panda/ir/statements/annotationDeclaration.cpp @@ -111,7 +111,14 @@ Identifier *AnnotationDeclaration::GetBaseName() const if (expr_->IsIdentifier()) { return expr_->AsIdentifier(); } - return expr_->AsETSTypeReference()->Part()->GetIdent(); + auto *part = Expr()->AsETSTypeReference()->Part(); + if (part->Name()->IsIdentifier()) { + return part->Name()->AsIdentifier(); + } + if (part->Name()->IsTSQualifiedName()) { + return part->Name()->AsTSQualifiedName()->Right(); + } + return nullptr; } AstNode *AnnotationDeclaration::Construct(ArenaAllocator *allocator) diff --git a/ets2panda/test/ast/compiler/ets/annotation_tests/annotation_for_bad_target.ets b/ets2panda/test/ast/compiler/ets/annotation_tests/annotation_for_bad_target.ets new file mode 100644 index 0000000000000000000000000000000000000000..e3deff1d62d3ad1055db607884d901f6d128a532 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/annotation_tests/annotation_for_bad_target.ets @@ -0,0 +1,23 @@ +/* + * 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 +this.context; + +/* @@? 17:1 Error SyntaxError: Identifier expected. */ +/* @@? 17:1 Error SyntaxError: Invalid annotation name. */ +/* @@? 17:1 Error SyntaxError: Expected '{', got 'this'. */ +/* @@? 17:5 Error SyntaxError: Identifier expected, got '.'. */ +/* @@? 17:13 Error SyntaxError: Missing type annotation for property 'context'. */