diff --git a/ets2panda/lexer/scripts/keywords.yaml b/ets2panda/lexer/scripts/keywords.yaml index 58cffbda4fbd75b568a2fadefcd37caf34866b09..8ea08e4ece393196d4fea1b71a35277c3eb9bd8d 100644 --- a/ets2panda/lexer/scripts/keywords.yaml +++ b/ets2panda/lexer/scripts/keywords.yaml @@ -487,7 +487,7 @@ keywords: - name: 'string' token: KEYW_STRING keyword_like: [ets, ts] - flags: [definable_type_name] + flags: [definable_type_name, predefined_type] - name: 'String' token: KEYW_BUILTIN_STRING diff --git a/ets2panda/parser/expressionParser.cpp b/ets2panda/parser/expressionParser.cpp index 67500b59ac9755039e36b368c1b0694f8051881f..5b651c2a17df9ab2a035f6d6a5f45ddbdb990930 100644 --- a/ets2panda/parser/expressionParser.cpp +++ b/ets2panda/parser/expressionParser.cpp @@ -1372,7 +1372,12 @@ ArenaVector ParserImpl::ParseCallExpressionArguments(bool &tra if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_PERIOD_PERIOD_PERIOD) { argument = ParseSpreadElement(); } else { + lexer::Token token = lexer_->GetToken(); argument = ParseExpression(); + if (token.IsPredefinedType() && !util::Helpers::IsStdLib(program_) && + !argument->IsMemberExpression() && !argument->IsCallExpression()) { + LogError(diagnostic::PREDEFINED_TYPE_AS_VALUE, {token.Ident()}, token.Start()); + } } arguments.push_back(argument); diff --git a/ets2panda/test/ast/parser/ets/ambient_indexer_2.ets b/ets2panda/test/ast/parser/ets/ambient_indexer_2.ets index 424271f881dadb502a5284e2e69a327e3b2ff1f3..778b22720aaffe58752d1a13aeb837dd2e2cdfd3 100644 --- a/ets2panda/test/ast/parser/ets/ambient_indexer_2.ets +++ b/ets2panda/test/ast/parser/ets/ambient_indexer_2.ets @@ -27,4 +27,5 @@ function main() { /* @@? 17:20 Error SyntaxError: Field type annotation expected. */ /* @@? 17:20 Error SyntaxError: Unexpected token ']'. */ /* @@? 17:22 Error SyntaxError: Unexpected token ':'. */ -/* @@? 17:30 Error SyntaxError: Field type annotation expected. */ +/* @@? 17:24 Error SyntaxError: string is a predefined type, cannot be used as an identifier */ +/* @@? 18:1 Error SyntaxError: Field type annotation expected. */ diff --git a/ets2panda/test/ast/parser/ets/class_as_object_1.ets b/ets2panda/test/ast/parser/ets/class_as_object_1.ets index e9d0cfa6ea03dee3f006ba2f56dda00e68c5e0e9..1bb8c7fc3fca88d66a6d531b0a1b253f560fc0bd 100644 --- a/ets2panda/test/ast/parser/ets/class_as_object_1.ets +++ b/ets2panda/test/ast/parser/ets/class_as_object_1.ets @@ -15,5 +15,6 @@ console.log(Object) +/* @@? 16:13 Error SyntaxError: Object is a predefined type, cannot be used as a value */ /* @@? 16:13 Error TypeError: Class or interface 'Object' cannot be used as object */ /* @@? 16:13 Error TypeError: Class name can't be the argument of function or method. */ diff --git a/ets2panda/test/ast/parser/ets/predefined_type_as_value.ets b/ets2panda/test/ast/parser/ets/predefined_type_as_value.ets new file mode 100644 index 0000000000000000000000000000000000000000..45651e33971f4a3a53a4e32795d7c31d0a28384b --- /dev/null +++ b/ets2panda/test/ast/parser/ets/predefined_type_as_value.ets @@ -0,0 +1,43 @@ +/* + * 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. + */ + +function fNumber(a: number) { } + +function fString(a: string) { } + +fNumber(number) +fString(string) + +function fNumberArray(a: number[]) { } + +function fStringArray(a: string[]) { } + +fNumberArray(number[]) +fStringArray(string[]) + +new Promise((resolve)=>{ + resolve(number[]); +}) + +new Promise((resolve)=>{ + resolve(string[]); +}) + +/* @@? 20:9 Error SyntaxError: number is a predefined type, cannot be used as a value */ +/* @@? 21:9 Error SyntaxError: string is a predefined type, cannot be used as a value */ +/* @@? 27:14 Error SyntaxError: number is a predefined type, cannot be used as a value */ +/* @@? 28:14 Error SyntaxError: string is a predefined type, cannot be used as a value */ +/* @@? 31:13 Error SyntaxError: number is a predefined type, cannot be used as a value */ +/* @@? 35:13 Error SyntaxError: string is a predefined type, cannot be used as a value */ diff --git a/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets b/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets index 2785b592bd2a8d11e51700e3342938a2369ca745..edd05d036018a0124bfbdcb8905284b73e49e5e1 100644 --- a/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets +++ b/ets2panda/test/ast/parser/ets/recursive_exported_structure.ets @@ -100,6 +100,7 @@ export default _exported; /* @@? 66:36 Error TypeError: Interfaces cannot extend classes, only other interfaces. */ /* @@? 73:6 Error SyntaxError: Unexpected token 'key'. */ /* @@? 73:9 Error SyntaxError: Unexpected token ':'. */ +/* @@? 73:11 Error SyntaxError: string is a predefined type, cannot be used as an identifier */ /* @@? 73:17 Error SyntaxError: Field type annotation expected. */ /* @@? 73:17 Error SyntaxError: Unexpected token ']'. */ /* @@? 73:18 Error SyntaxError: Unexpected token ':'. */ diff --git a/ets2panda/test/ast/parser/ets/user_defined_8.ets b/ets2panda/test/ast/parser/ets/user_defined_8.ets index 95556fc08294ee82fbe20c09051086b5d7fad317..ca6e92bd169a4bab59b95af73b7d88432e1e0f59 100644 --- a/ets2panda/test/ast/parser/ets/user_defined_8.ets +++ b/ets2panda/test/ast/parser/ets/user_defined_8.ets @@ -17,7 +17,5 @@ struct string{ a : string = "15"; } -/* @@? 16:8 Error SyntaxError: Cannot be used as user-defined type. */ /* @@? 16:1 Error TypeError: Structs are only used to define UI components, it should be translated at 'plugin after parser' phase. */ -/* @@? 1:3 Error TypeError: Variable 'string' is already defined with different type. */ -/* @@? 17:16 Error TypeError: Type '"15"' cannot be assigned to type 'string' */ +/* @@? 16:8 Error SyntaxError: string is a predefined type, cannot be used as an identifier */ diff --git a/ets2panda/test/ast/parser/ets/user_defined_9.ets b/ets2panda/test/ast/parser/ets/user_defined_9.ets index fecbee124f116921c19898efd6baa1ed741fd7bf..1170c80f98c44b47ee10471aed582e5cd53019a1 100644 --- a/ets2panda/test/ast/parser/ets/user_defined_9.ets +++ b/ets2panda/test/ast/parser/ets/user_defined_9.ets @@ -17,6 +17,4 @@ class /* @@ label */string{ a : string = "15"; } -/* @@@ label Error SyntaxError: Cannot be used as user-defined type. */ -/* @@? 1:3 Error TypeError: Variable 'string' is already defined with different type. */ -/* @@? 17:16 Error TypeError: Type '"15"' cannot be assigned to type 'string' */ +/* @@@ label Error SyntaxError: string is a predefined type, cannot be used as an identifier */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index 707be9029a43f0bd6a67ca4cd8ba14828cd99914..7bd2229ba7e1d6abb51a40fdfa127a6679bb43b6 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -1306,4 +1306,8 @@ syntax: - name: FUNCTION_OVERLOADED_NAME_MUST_QUALIFIED_NAME id: 325 - message: "The overloaded method name in function overload declaration must be qualified name." \ No newline at end of file + message: "The overloaded method name in function overload declaration must be qualified name." + +- name: PREDEFINED_TYPE_AS_VALUE + id: 326 + message: "{} is a predefined type, cannot be used as a value"