From b2fd09d8ea59fe8b3519b6e2fd208a9ada3e4453 Mon Sep 17 00:00:00 2001 From: zengzengran Date: Mon, 14 Jul 2025 20:32:20 +0800 Subject: [PATCH] Fix invalid declare annotation crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICPJD0 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/ir/statements/forOfStatement.cpp | 4 ++ ets2panda/parser/ETSparserAnnotations.cpp | 3 ++ .../ets/anno_interface_invalid_error.ets | 1 + .../ast/parser/ets/declare_annotation.ets | 38 ++++++++++++++ .../test/runtime/ets/forOfCustomIterator3.ets | 52 +++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 ets2panda/test/ast/parser/ets/declare_annotation.ets create mode 100644 ets2panda/test/runtime/ets/forOfCustomIterator3.ets diff --git a/ets2panda/ir/statements/forOfStatement.cpp b/ets2panda/ir/statements/forOfStatement.cpp index 9fff61c950..0f87e8b394 100644 --- a/ets2panda/ir/statements/forOfStatement.cpp +++ b/ets2panda/ir/statements/forOfStatement.cpp @@ -230,6 +230,10 @@ bool ForOfStatement::CheckReturnTypeOfIteratorMethod(checker::ETSChecker *checke bool ForOfStatement::CheckIteratorInterfaceForObject(checker::ETSChecker *checker, checker::ETSObjectType *obj) { + if (obj->Name().Is(ITERATOR_INTERFACE_NAME)) { + return true; + } + for (auto *const it : obj->Interfaces()) { if (it->Name().Is(ITERATOR_INTERFACE_NAME)) { return true; diff --git a/ets2panda/parser/ETSparserAnnotations.cpp b/ets2panda/parser/ETSparserAnnotations.cpp index d377daed37..6473428051 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 { @@ -200,6 +201,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 = Allocator()->New(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 f8549d0093..e7656e6b36 100644 --- a/ets2panda/test/ast/parser/ets/anno_interface_invalid_error.ets +++ b/ets2panda/test/ast/parser/ets/anno_interface_invalid_error.ets @@ -47,3 +47,4 @@ class C{ /* @@? 29:4 Error SyntaxError: Missing type annotation for property 'b'. */ /* @@? 42:11 Error SyntaxError: Only constant expression is expected in the field */ +/* @@? 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'. */ diff --git a/ets2panda/test/runtime/ets/forOfCustomIterator3.ets b/ets2panda/test/runtime/ets/forOfCustomIterator3.ets new file mode 100644 index 0000000000..6d9e55bad5 --- /dev/null +++ b/ets2panda/test/runtime/ets/forOfCustomIterator3.ets @@ -0,0 +1,52 @@ +/* + * 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. + */ + +class A implements Iterable{ + data: string[] = ['a', 'b', 'c']; + $_iterator():Iterator { + return new CIterator(this); + } +} + +class CIterator implements Iterator { + index = 0; + base: A; + constructor (base: A) { + this.base = base; + } + next(): IteratorResult { + if (this.index >= this.base.data.length) { + return { + done: true, + value: undefined + } + } + return { + done: this.index >= this.base.data.length, + value: this.base.data[this.index++] + } + } +} + + +function fooIterable(a:Iterable){ + let res = ""; + for (let x of a) res += x; + arktest.assertEQ(res, "abc") +} + +function main(): void { + fooIterable(new A()) +} -- Gitee