diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 1fd04a4a9e88ed3d4b7489ecfbe585db545fbe43..f4e415d09f6b52e36e2c8c15a45296f98cbac160 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -464,8 +464,9 @@ bool ETSChecker::CheckOptionalLambdaFunction(ir::Expression *argument, Signature static bool IsInvalidArgumentAsIdentifier(varbinder::Scope *scope, const ir::Identifier *identifier) { auto result = scope->Find(identifier->Name()); - return result.variable != nullptr && (result.variable->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE | - varbinder::VariableFlags::TYPE_ALIAS)); + return result.variable != nullptr && + (result.variable->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE_OR_ENUM | + varbinder::VariableFlags::TYPE_ALIAS)); } static void ClearPreferredTypeForArray(checker::ETSChecker *checker, ir::Expression *argument, Type *paramType, diff --git a/ets2panda/checker/ets/validateHelpers.cpp b/ets2panda/checker/ets/validateHelpers.cpp index ead157339ea626fbba740c0fda764617e5ce8ff7..2e1632446ccd2a265df631071fd53ee30d801bd7 100644 --- a/ets2panda/checker/ets/validateHelpers.cpp +++ b/ets2panda/checker/ets/validateHelpers.cpp @@ -88,10 +88,10 @@ void ETSChecker::ValidateCallExpressionIdentifier(ir::Identifier *const ident, T } } - if (ident->Variable()->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE) && callExpr->Callee() != ident && - callExpr->Callee() != ident->Parent()) { - std::ignore = - TypeError(ident->Variable(), diagnostic::CLASS_OR_IFACE_AS_OBJ, {ident->ToString()}, ident->Start()); + if (ident->Variable()->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE_OR_ENUM) && + callExpr->Callee() != ident && callExpr->Callee() != ident->Parent()) { + std::ignore = TypeError(ident->Variable(), diagnostic::CLASS_OR_IFACE_OR_ENUM_AS_OBJ, {ident->ToString()}, + ident->Start()); } if (callExpr->Callee() != ident && callExpr->Callee() != ident->Parent()) { diff --git a/ets2panda/test/ast/compiler/ets/enum_not_as_obj.ets b/ets2panda/test/ast/compiler/ets/enum_not_as_obj.ets new file mode 100644 index 0000000000000000000000000000000000000000..ed77ff5a5ddb70013dc9bce37be2b69242c7129b --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/enum_not_as_obj.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. + */ +enum Colorset {Red, Green,Blue}; +const result = JSON.stringify(Colorset); +console.log("Hello",result); + +/* @@? 15:1 Error TypeError: Indexed access is not supported for such expression type. */ +/* @@? 16:16 Error TypeError: No matching call signature for stringify(Colorset) */ +/* @@? 16:31 Error TypeError: Class or interface or enum 'Colorset' cannot be used as object */ \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/invalid_object.ets b/ets2panda/test/ast/compiler/ets/invalid_object.ets index 6d2e5fb2a110538949a686b1fc8e2bd36a626bf3..72a416d9babb12f1ed778e81d35da164dbd11fda 100644 --- a/ets2panda/test/ast/compiler/ets/invalid_object.ets +++ b/ets2panda/test/ast/compiler/ets/invalid_object.ets @@ -19,7 +19,7 @@ const object1: Obj = {}; console.log(Objˆct.keys(object1)); /* @@? 19:13 Error TypeError: Class name can't be the argument of function or method. */ -/* @@? 19:13 Error TypeError: Class or interface 'Obj' cannot be used as object */ +/* @@? 19:13 Error TypeError: Class or interface or enum 'Obj' cannot be used as object */ /* @@? 19:16 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 19:16 Error SyntaxError: Unexpected token 'ˆct'. */ /* @@? 19:16 Error SyntaxError: Unexpected token, expected ',' or ')'. */ diff --git a/ets2panda/test/ast/compiler/ets/param_wrong_identifier.ets b/ets2panda/test/ast/compiler/ets/param_wrong_identifier.ets index 22b996e74db89a8fba96d28baee3d2e739883d4a..89ec5d49e45dcb1830554ba1a41edec2efccdb67 100644 --- a/ets2panda/test/ast/compiler/ets/param_wrong_identifier.ets +++ b/ets2panda/test/ast/compiler/ets/param_wrong_identifier.ets @@ -44,13 +44,13 @@ function main() { /* @@? 29:10 Error TypeError: Class name can't be the argument of function or method. */ /* @@? 30:3 Error TypeError: No matching call signature for foo3(Int) */ /* @@? 30:10 Error TypeError: Class name can't be the argument of function or method. */ -/* @@? 30:10 Error TypeError: Class or interface 'Int' cannot be used as object */ +/* @@? 30:10 Error TypeError: Class or interface or enum 'Int' cannot be used as object */ /* @@? 31:3 Error TypeError: No matching call signature for foo4(String) */ /* @@? 31:10 Error TypeError: Class name can't be the argument of function or method. */ -/* @@? 31:10 Error TypeError: Class or interface 'String' cannot be used as object */ +/* @@? 31:10 Error TypeError: Class or interface or enum 'String' cannot be used as object */ /* @@? 32:3 Error TypeError: No matching call signature for foo5(Boolean) */ /* @@? 32:10 Error TypeError: Class name can't be the argument of function or method. */ -/* @@? 32:10 Error TypeError: Class or interface 'Boolean' cannot be used as object */ +/* @@? 32:10 Error TypeError: Class or interface or enum 'Boolean' cannot be used as object */ /* @@? 33:3 Error TypeError: No matching call signature for foo6(Double) */ /* @@? 33:10 Error TypeError: Class name can't be the argument of function or method. */ /* @@? 34:3 Error TypeError: No matching call signature for foo7(Double) */ diff --git a/ets2panda/test/ast/compiler/ets/with-statement.ets b/ets2panda/test/ast/compiler/ets/with-statement.ets index 98519d8934800b5a5a94a939d7f5b57f52d1cc20..8276ce39ab28433f69f0d18c611c842e5e5f264e 100644 --- a/ets2panda/test/ast/compiler/ets/with-statement.ets +++ b/ets2panda/test/ast/compiler/ets/with-statement.ets @@ -26,4 +26,4 @@ function main(): void { /* @@@ label1 Error TypeError: Expected 3 arguments, got 1. */ /* @@@ label1 Error TypeError: No matching call signature for with(Math) */ /* @@@ label1 Error TypeError: Expected 3 arguments, got 1. */ -/* @@@ label2 Error TypeError: Class or interface 'Math' cannot be used as object */ \ No newline at end of file +/* @@@ label2 Error TypeError: Class or interface or enum 'Math' cannot be used as object */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/classAsFunctionParam.ets b/ets2panda/test/ast/parser/ets/classAsFunctionParam.ets index d7118829e0c1ffd7227a1dfff28021bbac38b6c7..a3c567456ef411d7be8c4380206d77b4d870ba18 100644 --- a/ets2panda/test/ast/parser/ets/classAsFunctionParam.ets +++ b/ets2panda/test/ast/parser/ets/classAsFunctionParam.ets @@ -23,6 +23,6 @@ function main(): void { alma(C); } -/* @@? 23:10 Error TypeError: Class or interface 'C' cannot be used as object */ +/* @@? 23:10 Error TypeError: Class or interface or enum 'C' cannot be used as object */ /* @@? 23:10 Error TypeError: Class name can't be the argument of function or method. */ /* @@? 23:5 Error TypeError: No matching call signature for alma(C) */ 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..2adb2ac00ff1f27b077154722b6a676a9596be05 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,5 @@ console.log(Object) -/* @@? 16:13 Error TypeError: Class or interface 'Object' cannot be used as object */ +/* @@? 16:13 Error TypeError: Class or interface or enum '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/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index e9cb0fd8967c764ffc0cde95f312e467fc1939a4..265d476e7abbe720324265509134e7ebbf535d40 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -272,9 +272,9 @@ semantic: id: 62 message: "need to specify target type for class composite" -- name: CLASS_OR_IFACE_AS_OBJ +- name: CLASS_OR_IFACE_OR_ENUM_AS_OBJ id: 294 - message: "Class or interface '{}' cannot be used as object" + message: "Class or interface or enum '{}' cannot be used as object" - name: COALESCE_NOT_REF id: 117