From 31ec0e2c45405d1d1109a4c8291192bb704e8ed1 Mon Sep 17 00:00:00 2001 From: Robert Sipka Date: Tue, 17 Jun 2025 13:53:33 +0200 Subject: [PATCH] Abort on declare namespace with 'this' Issue: #ICFVIY Internal issue: 26215 Change-Id: I307d597c63c4647a862c6ef2273f709d34e24c63 Signed-off-by: Robert Sipka --- ets2panda/parser/TypedParser.cpp | 6 +++++- .../keywords/issue26215_restricted_super.ets | 20 +++++++++++++++++++ .../keywords/issue26215_restricted_this.ets | 20 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/keywords/issue26215_restricted_super.ets create mode 100644 ets2panda/test/ast/compiler/ets/keywords/issue26215_restricted_this.ets diff --git a/ets2panda/parser/TypedParser.cpp b/ets2panda/parser/TypedParser.cpp index 268455be78..02dd76cabd 100644 --- a/ets2panda/parser/TypedParser.cpp +++ b/ets2panda/parser/TypedParser.cpp @@ -130,7 +130,11 @@ bool TypedParser::IsNamespaceDecl() } auto savedPos = Lexer()->Save(); Lexer()->NextToken(); - bool isNamespaceDecl = Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_IDENT; + // namespace is a soft keyword, so it can be used as an identifier outside declarations + // If followed by an identifier (literal or keyword), it's treated as a declaration + // Using keywords as identifiers is invalid, but that error is handled later during parsing + bool isNamespaceDecl = + Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_IDENT || Lexer()->GetToken().IsKeyword(); Lexer()->Rewind(savedPos); return isNamespaceDecl; } diff --git a/ets2panda/test/ast/compiler/ets/keywords/issue26215_restricted_super.ets b/ets2panda/test/ast/compiler/ets/keywords/issue26215_restricted_super.ets new file mode 100644 index 0000000000..fb422ff9cc --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/keywords/issue26215_restricted_super.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +declare namespace super { + +} + +/* @@? 16:19 Error SyntaxError: Identifier expected, got 'super'. */ diff --git a/ets2panda/test/ast/compiler/ets/keywords/issue26215_restricted_this.ets b/ets2panda/test/ast/compiler/ets/keywords/issue26215_restricted_this.ets new file mode 100644 index 0000000000..cd9e3c4376 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/keywords/issue26215_restricted_this.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +declare namespace this { + +} + +/* @@? 16:19 Error SyntaxError: Identifier expected, got 'this'. */ -- Gitee