From c366c9bde5d24cc8266690c1c3aef62fdbaab6cd Mon Sep 17 00:00:00 2001 From: Boglarka Date: Thu, 29 May 2025 10:14:32 +0200 Subject: [PATCH] Add error for 'as const' Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICBFV0 Reason: Typeof keyword in type annotation and instanceof with a type on the left side is invalid. Description: Added checks for these cases and error logging. Signed-off-by: Haag Boglarka --- ets2panda/parser/ETSparserExpressions.cpp | 5 ++++- ets2panda/test/ast/parser/ets/string_literal_const_cast.ets | 2 +- ets2panda/util/diagnostic/syntax.yaml | 6 +++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/ets2panda/parser/ETSparserExpressions.cpp b/ets2panda/parser/ETSparserExpressions.cpp index 7c0bd342e9..9763da93e6 100644 --- a/ets2panda/parser/ETSparserExpressions.cpp +++ b/ets2panda/parser/ETSparserExpressions.cpp @@ -584,7 +584,10 @@ ir::Expression *ETSParser::ParsePotentialAsExpression(ir::Expression *primaryExp { ES2PANDA_ASSERT(Lexer()->GetToken().Type() == lexer::TokenType::KEYW_AS); Lexer()->NextToken(); - + if (Lexer()->GetToken().Type() == lexer::TokenType::KEYW_CONST) { + LogError(diagnostic::AS_CONST_USAGE); + return nullptr; + } TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::REPORT_ERROR | TypeAnnotationParsingOptions::ANNOTATION_NOT_ALLOW; ir::TypeNode *type = ParseTypeAnnotation(&options); diff --git a/ets2panda/test/ast/parser/ets/string_literal_const_cast.ets b/ets2panda/test/ast/parser/ets/string_literal_const_cast.ets index 899f906944..d265e85557 100644 --- a/ets2panda/test/ast/parser/ets/string_literal_const_cast.ets +++ b/ets2panda/test/ast/parser/ets/string_literal_const_cast.ets @@ -15,7 +15,7 @@ let x = "literal str" as const -/* @@? 16:26 Error SyntaxError: Invalid Type. */ +/* @@? 16:26 Error SyntaxError: 'as const' assertion is not supported. */ /* @@? 16:26 Error SyntaxError: Unexpected token 'const'. */ /* @@? 23:1 Error SyntaxError: Identifier expected, got 'end of stream'. */ /* @@? 23:1 Error SyntaxError: Variable must be initialized or it's type must be declared. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index a412c35ab5..864602db43 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -1230,4 +1230,8 @@ syntax: - name: DEEP_NESTING id: 305 - message: "Maximum allowed nesting level exceeded." \ No newline at end of file + message: "Maximum allowed nesting level exceeded." + +- name: AS_CONST_USAGE + id: 306 + message: "'as const' assertion is not supported." \ No newline at end of file -- Gitee