From cbdeeeacfcccc1dcee1300fd64d7c96c903d82f5 Mon Sep 17 00:00:00 2001 From: yuanchaoxuan Date: Thu, 4 Sep 2025 10:54:15 +0800 Subject: [PATCH] Fix TypeError: Enum cannot be used as an object Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICW8KV Signed-off-by: yuanchaoxuan --- ets2panda/checker/ets/function.cpp | 5 +++-- ets2panda/checker/ets/validateHelpers.cpp | 8 +++---- .../test/ast/compiler/ets/enum_not_as_obj.ets | 21 +++++++++++++++++++ .../test/ast/compiler/ets/invalid_object.ets | 2 +- .../compiler/ets/param_wrong_identifier.ets | 6 +++--- .../test/ast/compiler/ets/with-statement.ets | 2 +- .../ast/parser/ets/classAsFunctionParam.ets | 2 +- .../test/ast/parser/ets/class_as_object_1.ets | 2 +- ets2panda/util/diagnostic/semantic.yaml | 4 ++-- 9 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/enum_not_as_obj.ets diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 1fd04a4a9e..f4e415d09f 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 ead157339e..2e1632446c 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 0000000000..ed77ff5a5d --- /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 6d2e5fb2a1..72a416d9ba 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 22b996e74d..89ec5d49e4 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 98519d8934..8276ce39ab 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 d7118829e0..a3c567456e 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 e9d0cfa6ea..2adb2ac00f 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 e9cb0fd896..265d476e7a 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 -- Gitee