From cc713e677a0da939e62d98b46b10ec722aeef1a7 Mon Sep 17 00:00:00 2001 From: zengzengran Date: Thu, 17 Jul 2025 10:36:41 +0800 Subject: [PATCH] Fix invalid declare annotation crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICM8X5 Description: The type annotation of annotationField is nullptr, and using the clone method on nullptr causes a compilation crash. AnnotationField must include a type annotation, if not provided, it should be placed in brokenannotation instead of nullptr. Tested-by: ninja tests (passed) ets_testrunner (passed) Signed-off-by: zengzengran # --- ets2panda/parser/ETSparserAnnotations.cpp | 3 ++ .../ets/anno_interface_invalid_error.ets | 2 +- .../ast/parser/ets/declare_annotation.ets | 38 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/parser/ets/declare_annotation.ets diff --git a/ets2panda/parser/ETSparserAnnotations.cpp b/ets2panda/parser/ETSparserAnnotations.cpp index cdf4dfb5d0..409439d701 100644 --- a/ets2panda/parser/ETSparserAnnotations.cpp +++ b/ets2panda/parser/ETSparserAnnotations.cpp @@ -19,6 +19,7 @@ #include "ir/ets/etsTuple.h" #include "ir/ets/etsUnionType.h" #include "ir/statements/annotationDeclaration.h" +#include "ir/brokenTypeNode.h" namespace ark::es2panda::parser { @@ -195,6 +196,8 @@ ir::AstNode *ETSParser::ParseAnnotationProperty(ir::Identifier *fieldName, ir::M if (typeAnnotation == nullptr && (memberModifiers & ir::ModifierFlags::ANNOTATION_DECLARATION) != 0 && !fieldName->IsErrorPlaceHolder()) { LogError(diagnostic::MISSING_TYPE_ANNOTATION, {fieldName->Name().Mutf8()}, Lexer()->GetToken().Start()); + typeAnnotation = AllocNode(Allocator()); + typeAnnotation->SetRange({endLoc, endLoc}); } if (typeAnnotation != nullptr) { diff --git a/ets2panda/test/ast/parser/ets/anno_interface_invalid_error.ets b/ets2panda/test/ast/parser/ets/anno_interface_invalid_error.ets index 69d61ed6c5..f3fefe0e9d 100644 --- a/ets2panda/test/ast/parser/ets/anno_interface_invalid_error.ets +++ b/ets2panda/test/ast/parser/ets/anno_interface_invalid_error.ets @@ -45,6 +45,6 @@ class C{ public foo() {} } -/* @@? 29:2 Error TypeError: Invalid annotation field type. Only numeric, boolean, string, enum, or arrays of these types are permitted for annotation fields. */ /* @@? 29:4 Error SyntaxError: Missing type annotation for property 'b'. */ /* @@? 42:11 Error TypeError: Invalid value for annotation field, expected a constant literal. */ +/* @@? 44:16 Error TypeError: Invalid annotation field type. Only numeric, boolean, string, enum, or arrays of these types are permitted for annotation fields. */ diff --git a/ets2panda/test/ast/parser/ets/declare_annotation.ets b/ets2panda/test/ast/parser/ets/declare_annotation.ets new file mode 100644 index 0000000000..ba73fcc9ba --- /dev/null +++ b/ets2panda/test/ast/parser/ets/declare_annotation.ets @@ -0,0 +1,38 @@ +/* + * 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 ClassAuthor { + au`horName: string = "1" +} + +class A{ + @ClassAuthor + foo1(){} + + @ClassAuthor("11") + foo2(){} + + @ClassAuthor({authorName: "22"}) + foo3() + +/* @@? 17:7 Error SyntaxError: Missing type annotation for property 'au'. */ +/* @@? 17:7 Error SyntaxError: Identifier expected, got '`'. */ +/* @@? 21:6 Error TypeError: The required field 'au' must be specified. Fields without default values cannot be omitted. */ +/* @@? 24:6 Error TypeError: Annotation 'ClassAuthor' requires multiple fields to be specified. */ +/* @@? 24:18 Error TypeError: Invalid annotation field type. Only numeric, boolean, string, enum, or arrays of these types are permitted for annotation fields. */ +/* @@? 27:6 Error TypeError: The required field 'au' must be specified. Fields without default values cannot be omitted. */ +/* @@? 27:19 Error TypeError: The parameter 'authorName' does not match any declared property in the annotation 'ClassAuthor'. */ +/* @@? 28:9 Error TypeError: Only abstract or native methods can't have body. */ +/* @@? 39:1 Error SyntaxError: Expected '}', got 'end of stream'. */ -- Gitee