diff --git a/ets2panda/parser/ETSparserClasses.cpp b/ets2panda/parser/ETSparserClasses.cpp index d72d2f3febe1f58017b26253c06190235d45b217..513242b3e4afd2f69dd0838b4d73dc46d2d34543 100644 --- a/ets2panda/parser/ETSparserClasses.cpp +++ b/ets2panda/parser/ETSparserClasses.cpp @@ -28,6 +28,7 @@ #include "lexer/lexer.h" #include "lexer/ETSLexer.h" #include "ir/astNode.h" +#include "ir/brokenTypeNode.h" #include "ir/base/classDefinition.h" #include "ir/base/decorator.h" #include "ir/base/catchClause.h" @@ -474,14 +475,20 @@ void ETSParser::ParseClassFieldDefinition(ir::Identifier *fieldName, ir::Modifie bool optionalField = false; auto start = Lexer()->GetToken().Start(); - if (Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_EXCLAMATION_MARK)) { + if (Lexer()->GetToken().Type() == (lexer::TokenType::PUNCTUATOR_EXCLAMATION_MARK)) { + endLoc = Lexer()->GetToken().End(); modifiers |= ir::ModifierFlags::DEFINITE; + Lexer()->NextToken(); } - if (Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_QUESTION_MARK)) { + if (Lexer()->GetToken().Type() == (lexer::TokenType::PUNCTUATOR_QUESTION_MARK)) { + endLoc = Lexer()->GetToken().End(); optionalField = true; + Lexer()->NextToken(); } - if (Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_EXCLAMATION_MARK)) { + if (Lexer()->GetToken().Type() == (lexer::TokenType::PUNCTUATOR_EXCLAMATION_MARK)) { + endLoc = Lexer()->GetToken().End(); modifiers |= ir::ModifierFlags::DEFINITE; + Lexer()->NextToken(); } if (Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_COLON)) { typeAnnotation = ParseTypeAnnotation(&options); @@ -499,7 +506,9 @@ void ETSParser::ParseClassFieldDefinition(ir::Identifier *fieldName, ir::Modifie if (Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_SUBSTITUTION)) { initializer = ParseExpression(); } else if (typeAnnotation == nullptr) { - LogError(diagnostic::FIELD_TPYE_ANNOTATION_MISSING); + typeAnnotation = AllocNode(Allocator()); + typeAnnotation->SetRange({endLoc, endLoc}); + LogError(diagnostic::FIELD_TPYE_ANNOTATION_MISSING, {}, endLoc); } ValidateFieldModifiers(modifiers, optionalField, initializer, start); diff --git a/ets2panda/test/ast/compiler/ets/emptyOptionalTypeAnnotation.ets b/ets2panda/test/ast/compiler/ets/emptyOptionalTypeAnnotation.ets new file mode 100644 index 0000000000000000000000000000000000000000..09828e793277d00ece590dd6d438a2dd971ec0b9 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/emptyOptionalTypeAnnotation.ets @@ -0,0 +1,26 @@ +/* + * 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 Dep { + someMethod?; +} + +class Src implements Dep { + someMethod? +} + +/* @@? 17:14 Error SyntaxError: Interface fields must have type annotation. */ +/* @@? 18:1 Error SyntaxError: Invalid Type. */ +/* @@? 21:14 Error SyntaxError: Field type annotation expected. */ diff --git a/ets2panda/test/ast/compiler/ets/first_match/invalid_declaration.ets b/ets2panda/test/ast/compiler/ets/first_match/invalid_declaration.ets index 0d0f58498831da5732defc72809a40ff82d61dbd..8f7d7c607a85577679c28294c606616c275fd2bf 100644 --- a/ets2panda/test/ast/compiler/ets/first_match/invalid_declaration.ets +++ b/ets2panda/test/ast/compiler/ets/first_match/invalid_declaration.ets @@ -29,5 +29,5 @@ class Test { /* @@? 19:24 Error SyntaxError: Field type annotation expected. */ /* @@? 19:24 Error SyntaxError: Unexpected token ','. */ /* @@? 19:26 Error TypeError: Variable 'foo2' has already been declared. */ -/* @@? 19:31 Error SyntaxError: Field type annotation expected. */ +/* @@? 19:30 Error SyntaxError: Field type annotation expected. */ /* @@? 20:1 Error SyntaxError: Unexpected token '}'. */ diff --git a/ets2panda/test/ast/parser/ets/ambient_indexer_2.ets b/ets2panda/test/ast/parser/ets/ambient_indexer_2.ets index 7b5c41ba881ae1ae8068e8473f0fd3e9d8cc432a..424271f881dadb502a5284e2e69a327e3b2ff1f3 100644 --- a/ets2panda/test/ast/parser/ets/ambient_indexer_2.ets +++ b/ets2panda/test/ast/parser/ets/ambient_indexer_2.ets @@ -27,4 +27,4 @@ function main() { /* @@? 17:20 Error SyntaxError: Field type annotation expected. */ /* @@? 17:20 Error SyntaxError: Unexpected token ']'. */ /* @@? 17:22 Error SyntaxError: Unexpected token ':'. */ -/* @@? 18:1 Error SyntaxError: Field type annotation expected. */ +/* @@? 17:30 Error SyntaxError: Field type annotation expected. */ diff --git a/ets2panda/test/ast/parser/ets/declare_class_neg.ets b/ets2panda/test/ast/parser/ets/declare_class_neg.ets index 442c851340a95ca4b4d110a3074a09b2ad1324dc..9426627656abb696ad89feccf9518d102251c1c2 100644 --- a/ets2panda/test/ast/parser/ets/declare_class_neg.ets +++ b/ets2panda/test/ast/parser/ets/declare_class_neg.ets @@ -14,11 +14,11 @@ */ declare class A { - declare /* @@ label */constructor() + declare/* @@ label */ constructor() } class B { - declare /* @@ label1 */constructor() + declare/* @@ label1 */ constructor() } -/* @@@ label Error SyntaxError: Field type annotation expected. */ -/* @@@ label1 Error SyntaxError: Field type annotation expected. */ +/* @@? 17:10 Error SyntaxError: Field type annotation expected. */ +/* @@? 20:10 Error SyntaxError: Field type annotation expected. */ /* @@? 20:37 Error TypeError: Only abstract or native methods can't have body. */ diff --git a/ets2panda/test/ast/parser/ets/declare_namespace_5.ets b/ets2panda/test/ast/parser/ets/declare_namespace_5.ets index d51789195011e4f5a76ea22c01128aa41778f768..0b586d7dafcea2a6f641907604702a9f9197c101 100644 --- a/ets2panda/test/ast/parser/ets/declare_namespace_5.ets +++ b/ets2panda/test/ast/parser/ets/declare_namespace_5.ets @@ -54,6 +54,6 @@ class B { /* @@? 23:20 Error TypeError: Unresolved reference foo */ /* @@? 27:11 Error TypeError: Property 'getInstance' does not exist on type 'B' */ /* @@? 36:5 Error SyntaxError: Namespace is allowed only at the top level or inside a namespace. */ -/* @@? 39:5 Error SyntaxError: Field type annotation expected. */ +/* @@? 38:13 Error SyntaxError: Field type annotation expected. */ /* @@? 39:20 Error TypeError: This expression is not callable. */ /* @@? 43:11 Error TypeError: Property 'getInstance' does not exist on type 'B' */ diff --git a/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets b/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets index 5922720cdfc2e99ca054987b1da04b9b70d78b4e..2785b592bd2a8d11e51700e3342938a2369ca745 100644 --- a/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets +++ b/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets @@ -103,8 +103,7 @@ export default _exported; /* @@? 73:17 Error SyntaxError: Field type annotation expected. */ /* @@? 73:17 Error SyntaxError: Unexpected token ']'. */ /* @@? 73:18 Error SyntaxError: Unexpected token ':'. */ -/* @@? 73:31 Error SyntaxError: Field type annotation expected. */ +/* @@? 73:30 Error SyntaxError: Field type annotation expected. */ /* @@? 73:31 Error SyntaxError: Unexpected token '|'. */ /* @@? 73:46 Error SyntaxError: Field type annotation expected. */ /* @@? 76:16 Error TypeError: Cannot cast type 'ExportedStructure' to 'Record Double|Record Double>>' */ - diff --git a/ets2panda/test/ast/parser/ets/unexpected_token_33.ets b/ets2panda/test/ast/parser/ets/unexpected_token_33.ets index 7962f1d583dacf19f693a693de78de91ad621067..4041748031eb3917e2e8a0a0333a1d1d1c8a3a27 100644 --- a/ets2panda/test/ast/parser/ets/unexpected_token_33.ets +++ b/ets2panda/test/ast/parser/ets/unexpected_token_33.ets @@ -17,8 +17,8 @@ class G /* @@ label */T/* @@ label1 */, S /* @@ label2 */extends T/* @@ label3 * /* @@@ label Error SyntaxError: Expected '{', got 'identification literal'. */ /* @@@ label1 Error SyntaxError: Unexpected token ','. */ -/* @@@ label2 Error SyntaxError: Field type annotation expected. */ +/* @@? 16:42 Error SyntaxError: Field type annotation expected. */ /* @@@ label2 Error SyntaxError: Unexpected token 'extends'. */ -/* @@@ label3 Error SyntaxError: Field type annotation expected. */ +/* @@? 16:67 Error SyntaxError: Field type annotation expected. */ /* @@@ label3 Error SyntaxError: Unexpected token '>'. */ /* @@@ label4 Error SyntaxError: Unexpected token '{'. */