From f72978a3af1ff8a70aa7688a179472266d9444dd Mon Sep 17 00:00:00 2001 From: Gabor Aron Takacs Date: Thu, 29 May 2025 13:29:08 +0200 Subject: [PATCH] Add diagnostics for require and import Fixes #24594 internal issue. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICBHKQ Change-Id: I0dfa611d3a829b3ece8423cc4b6aa16f25fe595c Signed-off-by: Gabor Aron Takacs --- ets2panda/checker/ets/object.cpp | 4 ++++ ets2panda/parser/ETSparser.cpp | 8 +++++++ .../test/ast/parser/ets/import_assertion.ets | 22 +++++++++++++++++++ .../test/ast/parser/ets/import_require.ets | 21 ++++++++++++++++++ .../parser/ets/interface_extends_class.ets | 20 +++++++++++++++++ ets2panda/util/diagnostic/semantic.yaml | 4 ++++ ets2panda/util/diagnostic/syntax.yaml | 8 +++++++ 7 files changed, 87 insertions(+) create mode 100644 ets2panda/test/ast/parser/ets/import_assertion.ets create mode 100644 ets2panda/test/ast/parser/ets/import_require.ets create mode 100644 ets2panda/test/ast/parser/ets/interface_extends_class.ets diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index ce34a1924a..1649138262 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -187,6 +187,10 @@ bool ETSChecker::ComputeSuperType(ETSObjectType *type) void ETSChecker::ValidateImplementedInterface(ETSObjectType *type, Type *interface, std::unordered_set *extendsSet, const lexer::SourcePosition &pos) { + if (interface->IsETSObjectType() && interface->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::CLASS)) { + LogError(diagnostic::INTERFACE_EXTENDS_CLASS, {}, pos); + return; + } if (!interface->IsETSObjectType() || !interface->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::INTERFACE)) { LogError(diagnostic::NOT_INTERFACE, {}, pos); return; diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 36001a7946..9a591d8e19 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1191,6 +1191,10 @@ ir::ETSImportDeclaration *ETSParser::ParseImportPathBuildImport(ArenaVector(GetContext().GetProgram()), importFlags); importDeclaration->SetRange({startLoc, importPathStringLiteral->End()}); + if (Lexer()->GetToken().Ident().Is("assert")) { + LogError(diagnostic::ERROR_ARKTS_NO_IMPORT_ASSERTIONS); + return importDeclaration; + } ConsumeSemicolon(importDeclaration); return importDeclaration; } @@ -1494,6 +1498,10 @@ ir::AstNode *ETSParser::ParseImportDefaultSpecifier(ArenaVector * } } + if (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_SUBSTITUTION) { + LogError(diagnostic::ERROR_ARKTS_NO_REQUIRE); + return nullptr; + } if (Lexer()->GetToken().KeywordType() != lexer::TokenType::KEYW_FROM) { LogExpectedToken(lexer::TokenType::KEYW_FROM); Lexer()->NextToken(); // eat 'from' diff --git a/ets2panda/test/ast/parser/ets/import_assertion.ets b/ets2panda/test/ast/parser/ets/import_assertion.ets new file mode 100644 index 0000000000..bbf4ed4a28 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/import_assertion.ets @@ -0,0 +1,22 @@ +/* + * 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 * as Pt from "mod" /* @@ label1 */assert /* @@ label2 */{ type: /* @@ label3 */"json" } + + +/* @@@ label1 Error SyntaxError: Import assertion is not supported, please use the ordinary import syntax instead! */ +/* @@@ label1 Error TypeError: Unresolved reference assert */ +/* @@@ label2 Error SyntaxError: Unexpected token '{'. */ +/* @@@ label3 Error SyntaxError: Label must be followed by a loop statement. */ diff --git a/ets2panda/test/ast/parser/ets/import_require.ets b/ets2panda/test/ast/parser/ets/import_require.ets new file mode 100644 index 0000000000..ad8766f57b --- /dev/null +++ b/ets2panda/test/ast/parser/ets/import_require.ets @@ -0,0 +1,21 @@ +/* + * 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 m /* @@ label1 */= /* @@ label2 */require("mod") + +/* @@@ label1 Error SyntaxError: Importing by 'require' and 'import' assignment is not supported, use 'import * as ... from ...' form instead! */ +/* @@@ label1 Error SyntaxError: Unexpected token '='. */ +/* @@@ label2 Error SyntaxError: Unexpected token 'require'. */ +/* @@@ label2 Error TypeError: Unresolved reference require */ diff --git a/ets2panda/test/ast/parser/ets/interface_extends_class.ets b/ets2panda/test/ast/parser/ets/interface_extends_class.ets new file mode 100644 index 0000000000..6e88716f0c --- /dev/null +++ b/ets2panda/test/ast/parser/ets/interface_extends_class.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. + */ + +class A {} + +interface BInterface extends /* @@ label */A {} + +/* @@@ label Error TypeError: Interfaces cannot extend classes, only other interfaces. */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 95946196dc..568a3b7312 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -1494,3 +1494,7 @@ semantic: - name: SUPER_NOT_ACCESSIBLE id: 377 message: "Class field '{}' defined by the parent class is not accessible in the child class via super." + +- name: INTERFACE_EXTENDS_CLASS + id: 373 + message: "Interfaces cannot extend classes, only other interfaces." diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index c6a4d4e840..524f1a3112 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -1243,3 +1243,11 @@ syntax: - name: ERROR_ARKTS_NO_AMBIENT_DECLS id: 308 message: "Ambient module declaration is not supported!" + +- name: ERROR_ARKTS_NO_REQUIRE + id: 309 + message: "Importing by 'require' and 'import' assignment is not supported, use 'import * as ... from ...' form instead!" + +- name: ERROR_ARKTS_NO_IMPORT_ASSERTIONS + id: 310 + message: "Import assertion is not supported, please use the ordinary import syntax instead!" -- Gitee