From d2cb8d90cd4d1737ab10c537092f6cafa1931562 Mon Sep 17 00:00:00 2001 From: Peter Pronai Date: Tue, 1 Jul 2025 08:04:08 +0000 Subject: [PATCH 1/3] Error when accessing prototype field This feature is not in the spec and is explicitly listed as not supported by the cookbook. Add an error when code tries to assign or read from it. Fixes #23743 internal issue. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICJA0M Testing: ast, astchecker, parser Change-Id: I87f4d5466c748c090f827bfd53b9e302b5dc889b Signed-off-by: Peter Pronai --- ets2panda/parser/expressionParser.cpp | 6 ++++ .../test/ast/parser/ets/no_prototype.ets | 36 +++++++++++++++++++ ets2panda/util/diagnostic/syntax.yaml | 4 +++ 3 files changed, 46 insertions(+) create mode 100644 ets2panda/test/ast/parser/ets/no_prototype.ets diff --git a/ets2panda/parser/expressionParser.cpp b/ets2panda/parser/expressionParser.cpp index 596a70e086..6c329cba1a 100644 --- a/ets2panda/parser/expressionParser.cpp +++ b/ets2panda/parser/expressionParser.cpp @@ -1599,6 +1599,9 @@ ir::MemberExpression *ParserImpl::ParsePrivatePropertyAccess(ir::Expression *pri auto *privateIdent = AllocNode(lexer_->GetToken().Ident(), Allocator()); ES2PANDA_ASSERT(privateIdent != nullptr); + if (program_->Extension() == util::gen::extension::ETS && privateIdent->Name().Is("prototype")) { + LogError(diagnostic::PROTOTYPE_ACCESS); + } privateIdent->SetRange({memberStart, lexer_->GetToken().End()}); privateIdent->SetPrivate(true); lexer_->NextToken(); @@ -1613,6 +1616,9 @@ ir::MemberExpression *ParserImpl::ParsePrivatePropertyAccess(ir::Expression *pri ir::MemberExpression *ParserImpl::ParsePropertyAccess(ir::Expression *primaryExpr, bool isOptional) { ir::Identifier *ident = ExpectIdentifier(true); + if (program_->Extension() == util::gen::extension::ETS && ident->Name().Is("prototype")) { + LogError(diagnostic::PROTOTYPE_ACCESS); + } auto *memberExpr = AllocNode(primaryExpr, ident, ir::MemberExpressionKind::PROPERTY_ACCESS, false, isOptional); ES2PANDA_ASSERT(memberExpr != nullptr); diff --git a/ets2panda/test/ast/parser/ets/no_prototype.ets b/ets2panda/test/ast/parser/ets/no_prototype.ets new file mode 100644 index 0000000000..2f70cb7112 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/no_prototype.ets @@ -0,0 +1,36 @@ +/* + * 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 C {} + +C.prototype = Object.prototype + +C.prototype = { + m() { + console.log("C.m()") + } +} + +/* @@? 18:3 Error TypeError: Property 'prototype' does not exist on type 'C' */ +/* @@? 18:13 Error SyntaxError: Runtime prototype assignment is not supported because of static typing */ +/* @@? 18:22 Error TypeError: Property 'prototype' does not exist on type 'Object' */ +/* @@? 20:1 Error SyntaxError: Runtime prototype assignment is not supported because of static typing */ +/* @@? 20:3 Error TypeError: Property 'prototype' does not exist on type 'C' */ +/* @@? 20:13 Error SyntaxError: Runtime prototype assignment is not supported because of static typing */ +/* @@? 21:4 Error SyntaxError: Unexpected token, expected ':'. */ +/* @@? 21:5 Error SyntaxError: Unexpected token ')'. */ +/* @@? 21:7 Error SyntaxError: Unexpected token. */ +/* @@? 22:12 Error SyntaxError: Unexpected token, expected ':'. */ +/* @@? 24:1 Error SyntaxError: Unexpected token '}'. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index a998b7eacc..272b7ccc50 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -941,6 +941,10 @@ syntax: id: 238 message: "Methods or fields should not be decorated with {} in ambient class." +- name: PROTOTYPE_ACCESS + id: 30818 + message: "Runtime prototype assignment is not supported because of static typing" + - name: QUANTIFIER_OUT_OF_ORDER id: 78 message: "Quantifier range out of order." -- Gitee From 550262356e1bf2bf015e0344735ba4fd12281802 Mon Sep 17 00:00:00 2001 From: Gabor Aron Takacs Date: Tue, 3 Jun 2025 16:43:38 +0200 Subject: [PATCH 2/3] Add diagnostics for import for side effects Fixes #24689 internal issue. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICCBQN Change-Id: If1def850d9efd6c049cd896674824766c9c17243 Signed-off-by: Gabor Aron Takacs --- ets2panda/parser/ETSparser.cpp | 14 +++++++++++++ ets2panda/test/ast/parser/ets/export_as.ets | 20 +++++++++++++++++++ .../ets/import_no_empty_binding_list.ets | 20 +++++++++++++++++++ .../ast/parser/ets/import_no_side_effect.ets | 18 +++++++++++++++++ ets2panda/util/diagnostic/syntax.yaml | 9 +++++++++ 5 files changed, 81 insertions(+) create mode 100644 ets2panda/test/ast/parser/ets/export_as.ets create mode 100644 ets2panda/test/ast/parser/ets/import_no_empty_binding_list.ets create mode 100644 ets2panda/test/ast/parser/ets/import_no_side_effect.ets diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 5541ac57d5..5ca7f365e1 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1366,6 +1366,10 @@ ir::ExportNamedDeclaration *ETSParser::ParseSingleExport(ir::ModifierFlags modif return ParseSingleExportForAnonymousConst(modifiers); } + if (token.KeywordType() == lexer::TokenType::KEYW_AS) { + LogError(diagnostic::ERROR_ARKTS_NO_UMD, {}, token.Start()); + return nullptr; + } if (token.Type() != lexer::TokenType::LITERAL_IDENT) { LogError(diagnostic::EXPORT_NON_DECLARATION, {}, token.Start()); return nullptr; @@ -1498,6 +1502,12 @@ SpecifiersInfo ETSParser::ParseNamedSpecifiers() ArenaVector resultDefault(Allocator()->Adapter()); ArenaVector resultExportDefault(Allocator()->Adapter()); + auto token = Lexer()->GetToken(); + if (token.Ident() == (lexer::TokenToString(lexer::TokenType::KEYW_IMPORT)) && + token.Type() == lexer::TokenType::PUNCTUATOR_RIGHT_BRACE) { + LogError(diagnostic::ERROR_ARKTS_NO_SIDE_EFFECT_IMPORT); + } + ParseList( lexer::TokenType::PUNCTUATOR_RIGHT_BRACE, lexer::NextTokenFlags::KEYWORD_TO_IDENT, [this, &result, &resultDefault, &resultExportDefault, &fileName]() { @@ -1552,6 +1562,10 @@ void ETSParser::ParseNameSpaceSpecifier(ArenaVector *specifiers, ir::AstNode *ETSParser::ParseImportDefaultSpecifier(ArenaVector *specifiers) { + if (Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_STRING) { + LogError(diagnostic::ERROR_ARKTS_NO_SIDE_EFFECT_IMPORT); + return nullptr; + } if (Lexer()->GetToken().Type() != lexer::TokenType::LITERAL_IDENT) { LogExpectedToken(lexer::TokenType::LITERAL_IDENT); } diff --git a/ets2panda/test/ast/parser/ets/export_as.ets b/ets2panda/test/ast/parser/ets/export_as.ets new file mode 100644 index 0000000000..f2c130fbe8 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/export_as.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. + */ + +export /* @@ label1 */as /* @@ label2 */namespace mathLib {} + +/* @@@ label1 Error SyntaxError: Universal module definitions are not supported, please use ordinary import/export syntax instead. */ +/* @@@ label1 Error SyntaxError: Unexpected token 'as'. */ +/* @@@ label2 Error SyntaxError: Unexpected token 'namespace'. */ diff --git a/ets2panda/test/ast/parser/ets/import_no_empty_binding_list.ets b/ets2panda/test/ast/parser/ets/import_no_empty_binding_list.ets new file mode 100644 index 0000000000..dd2ff3124f --- /dev/null +++ b/ets2panda/test/ast/parser/ets/import_no_empty_binding_list.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. + */ + +import {/* @@ label1 */} /* @@ label2 */from /* @@ label3 */'module' + +/* @@@ label1 Error SyntaxError: Importing for side-effect only is prohibited! Please provide objects to be imported explicitly or use * to import all objects declared in the module! */ +/* @@@ label2 Error TypeError: Unresolved reference from */ +/* @@@ label3 Error SyntaxError: Unexpected token 'module'. */ diff --git a/ets2panda/test/ast/parser/ets/import_no_side_effect.ets b/ets2panda/test/ast/parser/ets/import_no_side_effect.ets new file mode 100644 index 0000000000..cd4d80cab0 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/import_no_side_effect.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +import /* @@ label1 */'module' + +/* @@@ label1 Error SyntaxError: Importing for side-effect only is prohibited! Please provide objects to be imported explicitly or use * to import all objects declared in the module! */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index 272b7ccc50..9a9440342d 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -225,6 +225,15 @@ syntax: id: 312 message: "Importing by 'require' and 'import' assignment is not supported, use 'import * as ... from ...' form instead!" +- name: ERROR_ARKTS_NO_SIDE_EFFECT_IMPORT + id: 73297 + message: "Importing for side-effect only is prohibited! Please provide objects to be imported explicitly or use * to import + all objects declared in the module!" + +- name: ERROR_ARKTS_NO_UMD + id: 114179 + message: "Universal module definitions are not supported, please use ordinary import/export syntax instead." + - name: ERROR_ARKTS_NO_VAR id: 297 message: "'var' keyword is not supported. Use 'let' instead." -- Gitee From 9753c9102c7af80e2a52632bf38e83e54d7b2f3c Mon Sep 17 00:00:00 2001 From: Gabor Aron Takacs Date: Sun, 22 Jun 2025 20:12:55 +0200 Subject: [PATCH 3/3] Export parser should work on object literal Reason: Default export from specification 13.5.1 Exported Declarations doesn't work correctly on object literals Description: Adding parser implementation for specific cornercase Fixes #26901 internal issue. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICJTPP Signed-off-by: Gabor Aron Takacs Signed-off-by: Torok Gergo --- ets2panda/ir/module/exportSpecifier.cpp | 8 +++++- ets2panda/parser/ETSparser.cpp | 27 ++++++++++++++++--- ets2panda/parser/ETSparser.h | 1 + .../ets/defaultExportObjectLiteral_exp.ets | 26 ++++++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 ets2panda/test/runtime/ets/defaultExportObjectLiteral_exp.ets diff --git a/ets2panda/ir/module/exportSpecifier.cpp b/ets2panda/ir/module/exportSpecifier.cpp index 1133414d19..1c28d57fe5 100644 --- a/ets2panda/ir/module/exportSpecifier.cpp +++ b/ets2panda/ir/module/exportSpecifier.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -48,6 +48,12 @@ void ExportSpecifier::Dump(ir::AstDumper *dumper) const void ExportSpecifier::Dump(ir::SrcDumper *dumper) const { + if (GetConstantExpression() != nullptr) { + GetConstantExpression()->Dump(dumper); + dumper->Add("as default"); + return; + } + exported_->Dump(dumper); if (local_ != nullptr) { diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 5ca7f365e1..c6fc097575 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1123,17 +1123,36 @@ ir::Statement *ETSParser::CreateReExportDeclarationNode(ir::ETSImportDeclaration return reExport; } +ir::Statement *ETSParser::ParseDefaultIfSingleExport(ir::ModifierFlags modifiers) +{ + auto tokenType = Lexer()->GetToken().Type(); + if (tokenType != lexer::TokenType::PUNCTUATOR_LEFT_BRACE) { + return ParseSingleExport(modifiers); + } + auto savePos = Lexer()->Save(); + Lexer()->NextToken(); + auto isSelectiveExport = Lexer()->TryEatTokenType(lexer::TokenType::LITERAL_IDENT) && + (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_COMMA || + Lexer()->GetToken().Type() == lexer::TokenType::KEYW_AS || + Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_RIGHT_BRACE); + Lexer()->Rewind(savePos); + return !isSelectiveExport ? ParseSingleExport(modifiers) : nullptr; +} + ir::Statement *ETSParser::ParseExport(lexer::SourcePosition startLoc, ir::ModifierFlags modifiers) { const size_t exportDefaultMaxSize = 1; if (!InAmbientContext() && (GetContext().Status() & ParserStatus::IN_NAMESPACE) != 0) { LogError(diagnostic::EXPORT_IN_NAMESPACE); } - [[maybe_unused]] auto tokenType = Lexer()->GetToken().Type(); // export a constant variable anonymously, as export default new A() - if ((modifiers & ir::ModifierFlags::DEFAULT_EXPORT) != 0U && tokenType != lexer::TokenType::PUNCTUATOR_MULTIPLY && - tokenType != lexer::TokenType::PUNCTUATOR_LEFT_BRACE && tokenType != lexer::TokenType::LITERAL_IDENT) { - return ParseSingleExport(modifiers); + if ((modifiers & ir::ModifierFlags::DEFAULT_EXPORT) != 0U && + Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_MULTIPLY && + Lexer()->GetToken().Type() != lexer::TokenType::LITERAL_IDENT) { + auto exportedExpression = ParseDefaultIfSingleExport(modifiers); + if (exportedExpression != nullptr) { + return exportedExpression; + } } ArenaVector specifiers(Allocator()->Adapter()); diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index 8dddff53b5..c92657f139 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -302,6 +302,7 @@ private: ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override; ir::Statement *ParseTryStatement() override; ir::Statement *ParseDebuggerStatement() override; + ir::Statement *ParseDefaultIfSingleExport(ir::ModifierFlags modifiers); ir::Statement *ParseExport(lexer::SourcePosition startLoc, ir::ModifierFlags modifiers); ir::Statement *ParseImportDeclaration(StatementParsingFlags flags) override; ir::Statement *ParseExportDeclaration(StatementParsingFlags flags) override; diff --git a/ets2panda/test/runtime/ets/defaultExportObjectLiteral_exp.ets b/ets2panda/test/runtime/ets/defaultExportObjectLiteral_exp.ets new file mode 100644 index 0000000000..885d31856d --- /dev/null +++ b/ets2panda/test/runtime/ets/defaultExportObjectLiteral_exp.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 User { + name: string; + age: number; + email: string; +}; + +export default { + name: 'John Doe', + age: 30, + email: 'johndoe@example.com' +} as User; -- Gitee