From 3ac9d9b14efeccdcf0c15596d5365e8cd7ecf768 Mon Sep 17 00:00:00 2001 From: Boglarka Haag Date: Wed, 9 Jul 2025 14:01:16 +0200 Subject: [PATCH] Error for any-unknown and intersection types Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICBIGZ Reason: Possible violation of rules of arkts-no-any-unknown and arkts-no-intersection-types Description: Added errors. Change-Id: I94ae7050880fb057ef4aa0cdcc995e1c84106891 Signed-off-by: Haag Boglarka --- ets2panda/parser/ETSparserTypes.cpp | 7 +++++ ...ility_type_can_not_found_etsobjecttype.ets | 2 +- .../ets/constructor_type_inference_crash.ets | 4 +-- .../ast/parser/ets/intersection_types.ets | 28 +++++++++++++++++++ .../ets/recursive_exported_structure.ets | 7 ++--- .../parser/ets/typenode_clone_brokentype.ets | 4 +-- ets2panda/util/diagnostic/syntax.yaml | 8 ++++++ ets2panda/varbinder/ETSBinder.cpp | 11 +++++++- 8 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 ets2panda/test/ast/parser/ets/intersection_types.ets diff --git a/ets2panda/parser/ETSparserTypes.cpp b/ets2panda/parser/ETSparserTypes.cpp index 3b0f7ea5c6..de8a5133ad 100644 --- a/ets2panda/parser/ETSparserTypes.cpp +++ b/ets2panda/parser/ETSparserTypes.cpp @@ -527,6 +527,13 @@ ir::TypeNode *ETSParser::ParseTypeAnnotationNoPreferParam(TypeAnnotationParsingO return nullptr; } + if (((*options) & TypeAnnotationParsingOptions::DISALLOW_UNION) == 0 && + Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_BITWISE_AND)) { + LogError(diagnostic::INTERSECTION_TYPES); + Lexer()->TryEatTokenType(lexer::TokenType::LITERAL_IDENT); + return AllocBrokenType({Lexer()->GetToken().Start(), Lexer()->GetToken().End()}); + } + if (!needFurtherProcessing) { return typeAnnotation; } diff --git a/ets2panda/test/ast/compiler/ets/utility_type_can_not_found_etsobjecttype.ets b/ets2panda/test/ast/compiler/ets/utility_type_can_not_found_etsobjecttype.ets index f549a860b8..b9f4698841 100644 --- a/ets2panda/test/ast/compiler/ets/utility_type_can_not_found_etsobjecttype.ets +++ b/ets2panda/test/ast/compiler/ets/utility_type_can_not_found_etsobjecttype.ets @@ -25,5 +25,5 @@ class X { } } -/* @@? 16:34 Error TypeError: Cannot find type 'any'. */ +/* @@? 16:34 Error SyntaxError: 'any' and 'unknown' types are not supported. Specify types explicitly. */ /* @@? 23:14 Error TypeError: Target type for class composite needs to be an object type, found 'T' */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/constructor_type_inference_crash.ets b/ets2panda/test/ast/parser/ets/constructor_type_inference_crash.ets index d31d7c3e7a..278b4d106f 100644 --- a/ets2panda/test/ast/parser/ets/constructor_type_inference_crash.ets +++ b/ets2panda/test/ast/parser/ets/constructor_type_inference_crash.ets @@ -22,6 +22,6 @@ let c = new C(10, 'foo'); /* @@? 17:14 Error TypeError: Only abstract or native methods can't have body. */ /* @@? 19:3 Error TypeError: No matching call signature for constructor */ -/* @@? 19:19 Error TypeError: Cannot find type 'any'. */ -/* @@? 19:28 Error TypeError: Cannot find type 'any'. */ +/* @@? 19:19 Error SyntaxError: 'any' and 'unknown' types are not supported. Specify types explicitly. */ +/* @@? 19:28 Error SyntaxError: 'any' and 'unknown' types are not supported. Specify types explicitly. */ diff --git a/ets2panda/test/ast/parser/ets/intersection_types.ets b/ets2panda/test/ast/parser/ets/intersection_types.ets new file mode 100644 index 0000000000..8b7cdb919c --- /dev/null +++ b/ets2panda/test/ast/parser/ets/intersection_types.ets @@ -0,0 +1,28 @@ +/* + * 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 Identity { + id: number + name: string +} + +interface Contact { + email: string + phoneNumber: string +} + +type Employee = Identity & Contact + +/* @@? 26:28 Error SyntaxError: Intersection types are not supported, use inheritance instead! */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets b/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets index 90fd2ccf79..a7c30be7f8 100644 --- a/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets +++ b/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets @@ -93,10 +93,9 @@ export default _exported; /* @@? 41:23 Error TypeError: need to specify target type for class composite */ /* @@? 54:20 Error TypeError: No matching call signature for (...) */ /* @@? 54:31 Error TypeError: need to specify target type for class composite */ -/* @@? 61:64 Error SyntaxError: Unexpected token '&'. */ -/* @@? 61:66 Error SyntaxError: Unexpected token '{'. */ +/* @@? 61:66 Error SyntaxError: Intersection types are not supported, use inheritance instead! */ /* @@? 62:12 Error SyntaxError: Label must be followed by a loop statement. */ /* @@? 62:12 Error TypeError: Type name 'IndexableType' used in the wrong context */ -/* @@? 66:36 Error TypeError: Interfaces cannot extend classes, only other interfaces. */ +/* @@? 66:36 Error TypeError: Interface expected here. */ /* @@? 73:6 Error TypeError: Indexed signatures are not allowed. Use arrays instead! */ -/* @@? 76:16 Error TypeError: Cannot cast type 'ExportedStructure' to 'Record Double|Record Double>>' */ +/* @@? 85:1 Error TypeError: Indexed access is not supported for such expression type. */ diff --git a/ets2panda/test/ast/parser/ets/typenode_clone_brokentype.ets b/ets2panda/test/ast/parser/ets/typenode_clone_brokentype.ets index f955112a64..2d27ad2f7e 100644 --- a/ets2panda/test/ast/parser/ets/typenode_clone_brokentype.ets +++ b/ets2panda/test/ast/parser/ets/typenode_clone_brokentype.ets @@ -55,5 +55,5 @@ declare const broken2: AnotherBroken; /* @@? 32:12 Error SyntaxError: Label must be followed by a loop statement. */ /* @@? 32:12 Error TypeError: Object type doesn't have proper index access method. */ /* @@? 36:27 Error TypeError: Cannot find type 'UndefinedInterface'. */ -/* @@? 39:35 Error TypeError: Cannot find type 'any'. */ -/* @@? 40:38 Error TypeError: Cannot find type 'any'. */ \ No newline at end of file +/* @@? 39:35 Error SyntaxError: 'any' and 'unknown' types are not supported. Specify types explicitly. */ +/* @@? 40:38 Error SyntaxError: 'any' and 'unknown' types are not supported. Specify types explicitly. */ \ No newline at end of file diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index 6e77a84dfe..da1a051e04 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -57,6 +57,10 @@ syntax: id: 28 message: "Annotations are not allowed on this type of declaration." +- name: ANY_UNKNOWN_TYPES + id: 102460 + message: "'any' and 'unknown' types are not supported. Specify types explicitly." + - name: ASSIGN_TO_ARGS_INVALID id: 57 message: "Assigning to 'arguments' in strict mode is invalid." @@ -497,6 +501,10 @@ syntax: id: 180 message: "Interface member initialization is prohibited." +- name: INTERSECTION_TYPES + id: 145527 + message: "Intersection types are not supported, use inheritance instead!" + - name: INVALIDE_CHAR_CLASS id: 79 message: "Invalid character class." diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index d9cb8a738b..e6e90b20b6 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -62,6 +62,15 @@ static bool IsSpecialName(const util::StringView &name) name == compiler::Signatures::FIXED_ARRAY_TYPE_NAME; } +static bool IsAnyOrUnknown(ETSBinder *binder, const util::StringView &name, const lexer::SourcePosition &pos) +{ + if (name.Is("any") || name.Is("unknown")) { + binder->ThrowError(pos, diagnostic::ANY_UNKNOWN_TYPES); + return true; + } + return false; +} + bool ETSBinder::LookupInDebugInfoPlugin(ir::Identifier *ident) { auto *checker = GetContext()->GetChecker()->AsETSChecker(); @@ -101,7 +110,7 @@ void ETSBinder::LookupTypeReference(ir::Identifier *ident) return; } - if (ident->IsErrorPlaceHolder()) { + if (ident->IsErrorPlaceHolder() || IsAnyOrUnknown(this, name, ident->Start())) { CreateDummyVariable(this, ident); return; } -- Gitee