From a48a406a5f1f809edab5d13b45c4a47b16088ab5 Mon Sep 17 00:00:00 2001 From: zengzengran Date: Fri, 25 Jul 2025 10:38:46 +0800 Subject: [PATCH] Fixing local interface crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICLKC7 Description: Referencing the local class, parse the local interface as allobrokenstatement. Tested-by: ninja tests (passed) ets_testrunner (passed) Signed-off-by: zengzengran # --- ets2panda/parser/ETSparser.h | 3 + ets2panda/parser/ETSparserStatements.cpp | 24 + ets2panda/parser/TypedParser.cpp | 7 +- ets2panda/parser/TypedParser.h | 1 + ets2panda/parser/parserImpl.h | 2 + ets2panda/parser/statementParser.cpp | 22 +- .../test/ast/parser/ets/InvalidClasses.ets | 3 +- .../annotationDecl_with_access_modifier03.ets | 1 + .../parser/ets/interface_parser_error_1.ets | 5 +- ...erface-member-access-modifier-private1.ets | 7 +- ...erface-member-access-modifier-private2.ets | 10 +- ...face-member-access-modifier-protected1.ets | 7 +- .../ets/local_class_already_interface.ets | 1 + .../parser/ets/local_enum.ets} | 37 +- .../test/ast/parser/ets/local_interface.ets | 81 +++ .../ets/local_interface_already_class.ets | 1 + .../ets/local_interface_already_interface.ets | 4 +- .../ets/local_interface_already_variable..ets | 3 +- .../parser/ets/local_type_alias.ets} | 44 +- ...terface_enum_only_top_level_4-expected.txt | 498 ------------------ .../parser/ets/local-interface-expected.txt | 375 ------------- 21 files changed, 232 insertions(+), 904 deletions(-) rename ets2panda/test/{parser/ets/class_interface_enum_only_top_level_4.ets => ast/parser/ets/local_enum.ets} (37%) create mode 100644 ets2panda/test/ast/parser/ets/local_interface.ets rename ets2panda/test/{parser/ets/local-interface.ets => ast/parser/ets/local_type_alias.ets} (34%) delete mode 100644 ets2panda/test/parser/ets/class_interface_enum_only_top_level_4-expected.txt delete mode 100644 ets2panda/test/parser/ets/local-interface-expected.txt diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index a62879f8b9..28055b0107 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -351,6 +351,9 @@ private: // NOLINTNEXTLINE(google-default-arguments) ir::Statement *ParseStructStatement(StatementParsingFlags flags, ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags modFlags = ir::ModifierFlags::NONE) override; + ir::Statement *ParseInterfaceStatement(StatementParsingFlags flags) override; + ir::Statement *ParseEnumStatement(StatementParsingFlags flags) override; + ir::Statement *ParseTypeAliasStatement() override; ir::AstNode *ParseClassElement(const ArenaVector &properties, ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags flags) override; std::tuple HandleClassElementModifiers(ir::ModifierFlags &memberModifiers); diff --git a/ets2panda/parser/ETSparserStatements.cpp b/ets2panda/parser/ETSparserStatements.cpp index 1ca8f39129..f3d0c19bcd 100644 --- a/ets2panda/parser/ETSparserStatements.cpp +++ b/ets2panda/parser/ETSparserStatements.cpp @@ -390,4 +390,28 @@ ir::Statement *ETSParser::ParseStructStatement([[maybe_unused]] StatementParsing return AllocBrokenStatement(rangeStruct); } +ir::Statement *ETSParser::ParseInterfaceStatement([[maybe_unused]] StatementParsingFlags flags) +{ + auto &rangeClass = Lexer()->GetToken().Loc(); + LogError(diagnostic::ILLEGAL_START_STRUCT_CLASS, {"INTERFACE"}, Lexer()->GetToken().Start()); + ParseInterfaceDeclaration(false); + return AllocBrokenStatement(rangeClass); +} + +ir::Statement *ETSParser::ParseEnumStatement([[maybe_unused]] StatementParsingFlags flags) +{ + auto &rangeClass = Lexer()->GetToken().Loc(); + LogError(diagnostic::ILLEGAL_START_STRUCT_CLASS, {"ENUM"}, Lexer()->GetToken().Start()); + ParseEnumDeclaration(); + return AllocBrokenStatement(rangeClass); +} + +ir::Statement *ETSParser::ParseTypeAliasStatement() +{ + auto &rangeClass = Lexer()->GetToken().Loc(); + LogError(diagnostic::ILLEGAL_START_STRUCT_CLASS, {"Type Alias"}, Lexer()->GetToken().Start()); + ParseTypeAliasDeclaration(); + return AllocBrokenStatement(rangeClass); +} + } // namespace ark::es2panda::parser diff --git a/ets2panda/parser/TypedParser.cpp b/ets2panda/parser/TypedParser.cpp index 90150e77c6..486c2663ef 100644 --- a/ets2panda/parser/TypedParser.cpp +++ b/ets2panda/parser/TypedParser.cpp @@ -145,7 +145,7 @@ ir::Statement *TypedParser::ParsePotentialExpressionStatement(StatementParsingFl switch (Lexer()->GetToken().KeywordType()) { case lexer::TokenType::KEYW_TYPE: { - const auto maybeAlias = ParseTypeAliasDeclaration(); + const auto maybeAlias = ParseTypeAliasStatement(); if (maybeAlias != nullptr) { return maybeAlias; } @@ -1551,4 +1551,9 @@ ParserStatus TypedParser::ValidateArrowParameter(ir::Expression *expr, bool *see return ParserStatus::NO_OPTS; } +ir::Statement *TypedParser::ParseTypeAliasStatement() +{ + return ParseTypeAliasDeclaration(); +} + } // namespace ark::es2panda::parser diff --git a/ets2panda/parser/TypedParser.h b/ets2panda/parser/TypedParser.h index ec50263d42..0880a6d30a 100644 --- a/ets2panda/parser/TypedParser.h +++ b/ets2panda/parser/TypedParser.h @@ -107,6 +107,7 @@ protected: { return false; } + virtual ir::Statement *ParseTypeAliasStatement(); virtual ir::TSTypeAliasDeclaration *ParseTypeAliasDeclaration() { return nullptr; diff --git a/ets2panda/parser/parserImpl.h b/ets2panda/parser/parserImpl.h index e7a99ab7c3..5010fbb975 100644 --- a/ets2panda/parser/parserImpl.h +++ b/ets2panda/parser/parserImpl.h @@ -311,6 +311,8 @@ protected: // NOLINTNEXTLINE(google-default-arguments) virtual ir::Statement *ParseStructStatement(StatementParsingFlags flags, ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags modFlags = ir::ModifierFlags::NONE); + virtual ir::Statement *ParseInterfaceStatement(StatementParsingFlags flags); + virtual ir::Statement *ParseEnumStatement(StatementParsingFlags flags); ir::Statement *ParseStatementBasedOnTokenType(StatementParsingFlags flags); ir::Statement *ParseVarStatement(); ir::Statement *ParseLetStatement(StatementParsingFlags flags); diff --git a/ets2panda/parser/statementParser.cpp b/ets2panda/parser/statementParser.cpp index e45ca21c2d..0055225932 100644 --- a/ets2panda/parser/statementParser.cpp +++ b/ets2panda/parser/statementParser.cpp @@ -197,9 +197,9 @@ ir::Statement *ParserImpl::ParseStatementBasedOnTokenType(StatementParsingFlags lexer_->NextToken(); return nullptr; case lexer::TokenType::KEYW_ENUM: - return ParseEnumDeclaration(); + return ParseEnumStatement(flags); case lexer::TokenType::KEYW_INTERFACE: - return ParseInterfaceDeclaration(false); + return ParseInterfaceStatement(flags); case lexer::TokenType::PUNCTUATOR_AT: if (IsETSParser()) { return ParseAnnotationsInStatement(flags); @@ -326,6 +326,24 @@ ir::Statement *ParserImpl::ParseClassStatement(StatementParsingFlags flags, ir:: return ParseClassDeclaration(modifiers, modFlags); } +ir::Statement *ParserImpl::ParseInterfaceStatement(StatementParsingFlags flags) +{ + if ((flags & StatementParsingFlags::ALLOW_LEXICAL) == 0) { + LogError(diagnostic::LEXICAL_DEC_NOT_ALLOWED_IN_SINGLE_STATEMENT_CONTEXT); + } + + return ParseInterfaceDeclaration(false); +} + +ir::Statement *ParserImpl::ParseEnumStatement(StatementParsingFlags flags) +{ + if ((flags & StatementParsingFlags::ALLOW_LEXICAL) == 0) { + LogError(diagnostic::LEXICAL_DEC_NOT_ALLOWED_IN_SINGLE_STATEMENT_CONTEXT); + } + + return ParseEnumDeclaration(); +} + ir::Statement *ParserImpl::ParseStructDeclaration(ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags flags) { const lexer::SourcePosition startLoc = lexer_->GetToken().Start(); diff --git a/ets2panda/test/ast/parser/ets/InvalidClasses.ets b/ets2panda/test/ast/parser/ets/InvalidClasses.ets index b73dc9af4c..44236605f8 100644 --- a/ets2panda/test/ast/parser/ets/InvalidClasses.ets +++ b/ets2panda/test/ast/parser/ets/InvalidClasses.ets @@ -114,11 +114,12 @@ interface I1 { /* @@? 58:25 Error SyntaxError: Unexpected token '}'. */ /* @@? 61:5 Error SyntaxError: Illegal start of CLASS expression. */ /* @@? 62:9 Error SyntaxError: Local class or interface declaration members can not have access modifies. */ +/* @@? 65:5 Error SyntaxError: Illegal start of INTERFACE expression. */ /* @@? 66:9 Error SyntaxError: Local class or interface declaration members can not have access modifies. */ /* @@? 66:18 Error SyntaxError: Private interface methods must have body. */ /* @@? 67:9 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ -/* @@? 67:16 Error SyntaxError: Unexpected token, expected ','. */ /* @@? 67:16 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 67:16 Error SyntaxError: Unexpected token, expected ','. */ /* @@? 67:16 Error SyntaxError: Identifier expected. */ /* @@? 67:24 Error SyntaxError: Private interface methods must have body. */ /* @@? 72:5 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ diff --git a/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_with_access_modifier03.ets b/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_with_access_modifier03.ets index c6d1285bf8..5d02e26982 100644 --- a/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_with_access_modifier03.ets +++ b/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_with_access_modifier03.ets @@ -20,3 +20,4 @@ /* @@@ label Error SyntaxError: Unexpected token 'static'. */ /* @@@ label1 Error SyntaxError: Unexpected token '@'. */ /* @@? 15:38 Error SyntaxError: Annotations can only be declared at the top level. */ +/* @@? 15:38 Error SyntaxError: Illegal start of INTERFACE expression. */ 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 589da49e76..050fbd6b1c 100644 --- a/ets2panda/test/ast/parser/ets/interface_parser_error_1.ets +++ b/ets2panda/test/ast/parser/ets/interface_parser_error_1.ets @@ -39,6 +39,7 @@ function mdin() { let a = new A(); /* @@? 17:4 Error SyntaxError: Unexpected token '{'. */ /* @@? 17:5 Error TypeError: Unresolved reference name */ /* @@? 17:11 Error SyntaxError: Unexpected token ':'. */ +/* @@? 19:1 Error SyntaxError: Illegal start of INTERFACE expression. */ /* @@? 20:5 Error SyntaxError: Identifier expected. */ /* @@? 20:14 Error SyntaxError: Unexpected token, expected ','. */ /* @@? 20:14 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ @@ -52,7 +53,7 @@ function mdin() { let a = new A(); /* @@? 24:7 Error TypeError: Unresolved reference A */ /* @@? 24:9 Error SyntaxError: Unexpected token 'implements'. */ /* @@? 24:20 Error SyntaxError: Unexpected token 'I'. */ -/* @@? 24:20 Error TypeError: Interface name 'I' used in the wrong context */ +/* @@? 24:20 Error TypeError: Unresolved reference I */ /* @@? 24:22 Error SyntaxError: Unexpected token '{'. */ /* @@? 25:4 Error TypeError: Unresolved reference reanstructor */ /* @@? 26:19 Error SyntaxError: Unexpected token, expected an identifier. */ @@ -66,4 +67,4 @@ function mdin() { let a = new A(); /* @@? 29:18 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 31:2 Error SyntaxError: Unexpected token '*'. */ /* @@? 32:1 Error SyntaxError: Nested functions are not allowed. */ -/* @@? 70:1 Error SyntaxError: Expected '}', got 'end of stream'. */ +/* @@? 71:1 Error SyntaxError: Expected '}', got 'end of stream'. */ diff --git a/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-private1.ets b/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-private1.ets index 6a6186266b..490354a619 100644 --- a/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-private1.ets +++ b/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-private1.ets @@ -15,10 +15,11 @@ function foo() { - interface LocalInterface + /* @@ label1 */interface LocalInterface { - /* @@ label */private property : int; + /* @@ label2 */private property : int; } } -/* @@@ label Error SyntaxError: Local class or interface declaration members can not have access modifies. */ +/* @@@ label1 Error SyntaxError: Illegal start of INTERFACE expression. */ +/* @@@ label2 Error SyntaxError: Local class or interface declaration members can not have access modifies. */ diff --git a/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-private2.ets b/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-private2.ets index 1083684e90..7f76e7b990 100644 --- a/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-private2.ets +++ b/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-private2.ets @@ -15,10 +15,12 @@ function foo() { - interface LocalInterface + /* @@ label1 */interface LocalInterface { - /* @@ label */private method/* @@ label1 */() : void; + /* @@ label2 */private method/* @@ label3 */() : void; } } -/* @@@ label Error SyntaxError: Local class or interface declaration members can not have access modifies. */ -/* @@@ label1 Error SyntaxError: Private interface methods must have body. */ + +/* @@@ label1 Error SyntaxError: Illegal start of INTERFACE expression. */ +/* @@@ label2 Error SyntaxError: Local class or interface declaration members can not have access modifies. */ +/* @@@ label3 Error SyntaxError: Private interface methods must have body. */ diff --git a/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-protected1.ets b/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-protected1.ets index 915df139ae..af6da8fe3b 100644 --- a/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-protected1.ets +++ b/ets2panda/test/ast/parser/ets/local-interface-member-access-modifier-protected1.ets @@ -15,10 +15,11 @@ function foo() { - interface LocalInterface + /* @@ label1 */interface LocalInterface { - /* @@ label */protected property : int; + /* @@ label2 */protected property : int; } } -/* @@@ label Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@@ label1 Error SyntaxError: Illegal start of INTERFACE expression. */ +/* @@@ label2 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ diff --git a/ets2panda/test/ast/parser/ets/local_class_already_interface.ets b/ets2panda/test/ast/parser/ets/local_class_already_interface.ets index e9fea16e52..64e86ea35f 100644 --- a/ets2panda/test/ast/parser/ets/local_class_already_interface.ets +++ b/ets2panda/test/ast/parser/ets/local_class_already_interface.ets @@ -22,4 +22,5 @@ function bar(): void { } } +/* @@? 17:3 Error SyntaxError: Illegal start of INTERFACE expression. */ /* @@? 20:3 Error SyntaxError: Illegal start of CLASS expression. */ diff --git a/ets2panda/test/parser/ets/class_interface_enum_only_top_level_4.ets b/ets2panda/test/ast/parser/ets/local_enum.ets similarity index 37% rename from ets2panda/test/parser/ets/class_interface_enum_only_top_level_4.ets rename to ets2panda/test/ast/parser/ets/local_enum.ets index 75d8981a52..73d642975b 100644 --- a/ets2panda/test/parser/ets/class_interface_enum_only_top_level_4.ets +++ b/ets2panda/test/ast/parser/ets/local_enum.ets @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * 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 @@ -13,9 +13,36 @@ * limitations under the License. */ -function main() : void -{ - interface C { - foo(a: int): int; +let myAssertEqual = (actualValue: boolean, expectValue: boolean) => { + enum E {E1} +} + +function test() { + enum E {E1} + + if (true) { + enum E {E1} + } +} + +class C { + enum E {E1} + + foo(){ + enum E {E1} } } + +interface I{ + enum E {E1} +} + +/* @@? 17:3 Error SyntaxError: Illegal start of ENUM expression. */ +/* @@? 21:3 Error SyntaxError: Illegal start of ENUM expression. */ +/* @@? 24:5 Error SyntaxError: Illegal start of ENUM expression. */ +/* @@? 29:3 Error SyntaxError: Unexpected token. A constructor, method, accessor, or property was expected. */ +/* @@? 32:5 Error SyntaxError: Illegal start of ENUM expression. */ +/* @@? 37:4 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 37:11 Error SyntaxError: Interface fields must have type annotation. */ +/* @@? 37:12 Error TypeError: Cannot find type 'E1'. */ +/* @@? 38:1 Error SyntaxError: Unexpected token '}'. */ diff --git a/ets2panda/test/ast/parser/ets/local_interface.ets b/ets2panda/test/ast/parser/ets/local_interface.ets new file mode 100644 index 0000000000..e645260fca --- /dev/null +++ b/ets2panda/test/ast/parser/ets/local_interface.ets @@ -0,0 +1,81 @@ +/* + * 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. + */ + +let myAssertEqual = (actualValue: boolean, expectValue: boolean) => { + /* @@ label1 */interface LocalI { + pass: boolean + message: string + } + + /* @@ label2 */class LocalC { + pass: boolean = true + message: string = "123" + } +} + +function test() { + /* @@ label3 */interface LocalI { + pass: boolean + message: string + } + /* @@ label4 */class LocalC { + pass: boolean = true + message: string = "123" + } + + if (true) { + /* @@ label5 */interface LocalI { + pass: boolean + message: string + } + /* @@ label6 */class LocalC { + pass: boolean = true + message: string = "123" + } + } +} + +class C { + /* @@ label7 */interface LocalI { + pass: boolean + message: string + } + /* @@ label8 */class LocalC { + pass: boolean = true + message: string = "123" + } + + foo(){ + /* @@ label9 */interface LocalI { + pass: boolean + message: string + } + /* @@ label10 */class LocalC { + pass: boolean = true + message: string = "123" + } + } +} + +/* @@@ label1 Error SyntaxError: Illegal start of INTERFACE expression. */ +/* @@@ label2 Error SyntaxError: Illegal start of CLASS expression. */ +/* @@@ label3 Error SyntaxError: Illegal start of INTERFACE expression. */ +/* @@@ label4 Error SyntaxError: Illegal start of CLASS expression. */ +/* @@@ label5 Error SyntaxError: Illegal start of INTERFACE expression. */ +/* @@@ label6 Error SyntaxError: Illegal start of CLASS expression. */ +/* @@@ label7 Error SyntaxError: Unexpected token. A constructor, method, accessor, or property was expected. */ +/* @@@ label8 Error SyntaxError: Unexpected token. A constructor, method, accessor, or property was expected. */ +/* @@@ label9 Error SyntaxError: Illegal start of INTERFACE expression. */ +/* @@@ label10 Error SyntaxError: Illegal start of CLASS expression. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/local_interface_already_class.ets b/ets2panda/test/ast/parser/ets/local_interface_already_class.ets index a1458dd771..df8b96452f 100644 --- a/ets2panda/test/ast/parser/ets/local_interface_already_class.ets +++ b/ets2panda/test/ast/parser/ets/local_interface_already_class.ets @@ -23,3 +23,4 @@ function bar(): void { } /* @@? 17:3 Error SyntaxError: Illegal start of CLASS expression. */ +/* @@? 20:3 Error SyntaxError: Illegal start of INTERFACE expression. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/local_interface_already_interface.ets b/ets2panda/test/ast/parser/ets/local_interface_already_interface.ets index ad3bd01bac..67952a2acf 100644 --- a/ets2panda/test/ast/parser/ets/local_interface_already_interface.ets +++ b/ets2panda/test/ast/parser/ets/local_interface_already_interface.ets @@ -22,5 +22,5 @@ function bar(): void { } } -/* @@? 20:3 Error TypeError: Variable 'BC' has already been declared. */ -/* @@? 20:3 Error TypeError: Merging declarations is not supported, please keep all definitions of classes, interfaces and enums compact in the codebase! */ +/* @@? 17:3 Error SyntaxError: Illegal start of INTERFACE expression. */ +/* @@? 20:3 Error SyntaxError: Illegal start of INTERFACE expression. */ diff --git a/ets2panda/test/ast/parser/ets/local_interface_already_variable..ets b/ets2panda/test/ast/parser/ets/local_interface_already_variable..ets index 0c4c33dce6..0660c505d4 100644 --- a/ets2panda/test/ast/parser/ets/local_interface_already_variable..ets +++ b/ets2panda/test/ast/parser/ets/local_interface_already_variable..ets @@ -20,5 +20,4 @@ function bar(): void { } } -/* @@? 18:3 Error TypeError: Variable 'BC' has already been declared. */ -/* @@? 18:3 Error TypeError: Merging declarations is not supported, please keep all definitions of classes, interfaces and enums compact in the codebase! */ +/* @@? 18:3 Error SyntaxError: Illegal start of INTERFACE expression. */ diff --git a/ets2panda/test/parser/ets/local-interface.ets b/ets2panda/test/ast/parser/ets/local_type_alias.ets similarity index 34% rename from ets2panda/test/parser/ets/local-interface.ets rename to ets2panda/test/ast/parser/ets/local_type_alias.ets index 9aeed4261a..9060686ac9 100644 --- a/ets2panda/test/parser/ets/local-interface.ets +++ b/ets2panda/test/ast/parser/ets/local_type_alias.ets @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * 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 @@ -13,8 +13,40 @@ * limitations under the License. */ -function main() : int -{ - interface LocalInterface { } - return 0; -} \ No newline at end of file +class A{} + +let myAssertEqual = (actualValue: boolean, expectValue: boolean) => { + type a = A; +} + +function test() { + type a = A; + + if (true) { + type a = A; + } +} + +class C { + type a = A; + + foo(){ + type a = A; + } +} + +interface I{ + type a = A; +} + +/* @@? 19:3 Error SyntaxError: Illegal start of Type Alias expression. */ +/* @@? 23:3 Error SyntaxError: Illegal start of Type Alias expression. */ +/* @@? 26:5 Error SyntaxError: Illegal start of Type Alias expression. */ +/* @@? 31:7 Error SyntaxError: Field type annotation expected. */ +/* @@? 31:12 Error SyntaxError: Class cannot be used as object. */ +/* @@? 34:5 Error SyntaxError: Illegal start of Type Alias expression. */ +/* @@? 39:9 Error TypeError: Cannot find type 'a'. */ +/* @@? 39:11 Error SyntaxError: Interface member initialization is prohibited. */ +/* @@? 39:13 Error SyntaxError: Unexpected token, expected ','. */ +/* @@? 39:13 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ +/* @@? 39:14 Error SyntaxError: Identifier expected. */ diff --git a/ets2panda/test/parser/ets/class_interface_enum_only_top_level_4-expected.txt b/ets2panda/test/parser/ets/class_interface_enum_only_top_level_4-expected.txt deleted file mode 100644 index 57a2f46f99..0000000000 --- a/ets2panda/test/parser/ets/class_interface_enum_only_top_level_4-expected.txt +++ /dev/null @@ -1,498 +0,0 @@ -{ - "type": "Program", - "statements": [ - { - "type": "ClassDeclaration", - "definition": { - "id": { - "type": "Identifier", - "name": "ETSGLOBAL", - "decorators": [], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "superClass": null, - "implements": [], - "body": [ - { - "type": "MethodDefinition", - "key": { - "type": "Identifier", - "name": "_$init$_", - "decorators": [], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": null - }, - "end": { - "line": 1, - "column": 1, - "program": null - } - } - }, - "kind": "method", - "accessibility": "public", - "static": true, - "optional": false, - "computed": false, - "value": { - "type": "FunctionExpression", - "function": { - "type": "ScriptFunction", - "id": { - "type": "Identifier", - "name": "_$init$_", - "decorators": [], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": null - }, - "end": { - "line": 1, - "column": 1, - "program": null - } - } - }, - "generator": false, - "async": false, - "expression": false, - "params": [], - "body": { - "type": "BlockStatement", - "statements": [], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "overloads": [], - "decorators": [], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - { - "type": "MethodDefinition", - "key": { - "type": "Identifier", - "name": "main", - "decorators": [], - "loc": { - "start": { - "line": 16, - "column": 10, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 16, - "column": 14, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "kind": "method", - "accessibility": "public", - "static": true, - "optional": false, - "computed": false, - "value": { - "type": "FunctionExpression", - "function": { - "type": "ScriptFunction", - "id": { - "type": "Identifier", - "name": "main", - "decorators": [], - "loc": { - "start": { - "line": 16, - "column": 10, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 16, - "column": 14, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "generator": false, - "async": false, - "expression": false, - "params": [], - "returnType": { - "type": "ETSPrimitiveType", - "loc": { - "start": { - "line": 16, - "column": 19, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 16, - "column": 23, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "body": { - "type": "BlockStatement", - "statements": [ - { - "type": "TSInterfaceDeclaration", - "body": { - "type": "TSInterfaceBody", - "body": [ - { - "type": "MethodDefinition", - "key": { - "type": "Identifier", - "name": "foo", - "decorators": [], - "loc": { - "start": { - "line": 19, - "column": 5, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 19, - "column": 8, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "kind": "method", - "accessibility": "public", - "static": false, - "optional": false, - "computed": false, - "value": { - "type": "FunctionExpression", - "function": { - "type": "ScriptFunction", - "id": { - "type": "Identifier", - "name": "foo", - "decorators": [], - "loc": { - "start": { - "line": 19, - "column": 5, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 19, - "column": 8, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "generator": false, - "async": false, - "expression": false, - "params": [ - { - "type": "ETSParameterExpression", - "name": { - "type": "Identifier", - "name": "a", - "typeAnnotation": { - "type": "ETSPrimitiveType", - "loc": { - "start": { - "line": 19, - "column": 12, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 19, - "column": 15, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "decorators": [], - "loc": { - "start": { - "line": 19, - "column": 9, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 19, - "column": 15, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "loc": { - "start": { - "line": 19, - "column": 9, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 19, - "column": 15, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - } - ], - "returnType": { - "type": "ETSPrimitiveType", - "loc": { - "start": { - "line": 19, - "column": 18, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 19, - "column": 21, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "loc": { - "start": { - "line": 19, - "column": 8, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 19, - "column": 21, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "loc": { - "start": { - "line": 19, - "column": 8, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 19, - "column": 21, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "overloads": [], - "decorators": [], - "loc": { - "start": { - "line": 19, - "column": 5, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 19, - "column": 22, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - } - ], - "loc": { - "start": { - "line": 18, - "column": 15, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 20, - "column": 4, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "id": { - "type": "Identifier", - "name": "C", - "decorators": [], - "loc": { - "start": { - "line": 18, - "column": 13, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 18, - "column": 14, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "extends": [], - "loc": { - "start": { - "line": 18, - "column": 3, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 21, - "column": 2, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - } - ], - "loc": { - "start": { - "line": 17, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 21, - "column": 2, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "loc": { - "start": { - "line": 16, - "column": 10, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 21, - "column": 2, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "loc": { - "start": { - "line": 16, - "column": 10, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 21, - "column": 2, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "overloads": [], - "decorators": [], - "loc": { - "start": { - "line": 16, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 21, - "column": 2, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - } - ], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - }, - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - } - } - } - ], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - }, - "end": { - "line": 22, - "column": 1, - "program": "class_interface_enum_only_top_level_4.ets" - } - } -} diff --git a/ets2panda/test/parser/ets/local-interface-expected.txt b/ets2panda/test/parser/ets/local-interface-expected.txt deleted file mode 100644 index f9c416f770..0000000000 --- a/ets2panda/test/parser/ets/local-interface-expected.txt +++ /dev/null @@ -1,375 +0,0 @@ -{ - "type": "Program", - "statements": [ - { - "type": "ClassDeclaration", - "definition": { - "id": { - "type": "Identifier", - "name": "ETSGLOBAL", - "decorators": [], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - } - } - }, - "superClass": null, - "implements": [], - "body": [ - { - "type": "MethodDefinition", - "key": { - "type": "Identifier", - "name": "_$init$_", - "decorators": [], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": null - }, - "end": { - "line": 1, - "column": 1, - "program": null - } - } - }, - "kind": "method", - "accessibility": "public", - "static": true, - "optional": false, - "computed": false, - "value": { - "type": "FunctionExpression", - "function": { - "type": "ScriptFunction", - "id": { - "type": "Identifier", - "name": "_$init$_", - "decorators": [], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": null - }, - "end": { - "line": 1, - "column": 1, - "program": null - } - } - }, - "generator": false, - "async": false, - "expression": false, - "params": [], - "body": { - "type": "BlockStatement", - "statements": [], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - } - } - }, - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - } - } - }, - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - } - } - }, - "overloads": [], - "decorators": [], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - } - } - }, - { - "type": "MethodDefinition", - "key": { - "type": "Identifier", - "name": "main", - "decorators": [], - "loc": { - "start": { - "line": 16, - "column": 10, - "program": "local-interface.ets" - }, - "end": { - "line": 16, - "column": 14, - "program": "local-interface.ets" - } - } - }, - "kind": "method", - "accessibility": "public", - "static": true, - "optional": false, - "computed": false, - "value": { - "type": "FunctionExpression", - "function": { - "type": "ScriptFunction", - "id": { - "type": "Identifier", - "name": "main", - "decorators": [], - "loc": { - "start": { - "line": 16, - "column": 10, - "program": "local-interface.ets" - }, - "end": { - "line": 16, - "column": 14, - "program": "local-interface.ets" - } - } - }, - "generator": false, - "async": false, - "expression": false, - "params": [], - "returnType": { - "type": "ETSPrimitiveType", - "loc": { - "start": { - "line": 16, - "column": 19, - "program": "local-interface.ets" - }, - "end": { - "line": 16, - "column": 22, - "program": "local-interface.ets" - } - } - }, - "body": { - "type": "BlockStatement", - "statements": [ - { - "type": "TSInterfaceDeclaration", - "body": { - "type": "TSInterfaceBody", - "body": [], - "loc": { - "start": { - "line": 18, - "column": 30, - "program": "local-interface.ets" - }, - "end": { - "line": 18, - "column": 33, - "program": "local-interface.ets" - } - } - }, - "id": { - "type": "Identifier", - "name": "LocalInterface", - "decorators": [], - "loc": { - "start": { - "line": 18, - "column": 15, - "program": "local-interface.ets" - }, - "end": { - "line": 18, - "column": 29, - "program": "local-interface.ets" - } - } - }, - "extends": [], - "loc": { - "start": { - "line": 18, - "column": 5, - "program": "local-interface.ets" - }, - "end": { - "line": 19, - "column": 11, - "program": "local-interface.ets" - } - } - }, - { - "type": "ReturnStatement", - "argument": { - "type": "NumberLiteral", - "value": 0, - "loc": { - "start": { - "line": 19, - "column": 12, - "program": "local-interface.ets" - }, - "end": { - "line": 19, - "column": 13, - "program": "local-interface.ets" - } - } - }, - "loc": { - "start": { - "line": 19, - "column": 5, - "program": "local-interface.ets" - }, - "end": { - "line": 19, - "column": 14, - "program": "local-interface.ets" - } - } - } - ], - "loc": { - "start": { - "line": 17, - "column": 1, - "program": "local-interface.ets" - }, - "end": { - "line": 20, - "column": 2, - "program": "local-interface.ets" - } - } - }, - "loc": { - "start": { - "line": 16, - "column": 10, - "program": "local-interface.ets" - }, - "end": { - "line": 20, - "column": 2, - "program": "local-interface.ets" - } - } - }, - "loc": { - "start": { - "line": 16, - "column": 10, - "program": "local-interface.ets" - }, - "end": { - "line": 20, - "column": 2, - "program": "local-interface.ets" - } - } - }, - "overloads": [], - "decorators": [], - "loc": { - "start": { - "line": 16, - "column": 1, - "program": "local-interface.ets" - }, - "end": { - "line": 20, - "column": 2, - "program": "local-interface.ets" - } - } - } - ], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - } - } - }, - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - }, - "end": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - } - } - } - ], - "loc": { - "start": { - "line": 1, - "column": 1, - "program": "local-interface.ets" - }, - "end": { - "line": 20, - "column": 2, - "program": "local-interface.ets" - } - } -} -- Gitee