diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 23ea6d54f47de5750c908bb76ec1f756b8547234..6c20a43acb6361578708ec7aabdbc3c8cf3c09c1 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -419,8 +419,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 765c1a3aade945fbaa2738ab2dda010d17c5425a..0db6bb0039fdb7289cbe1b72185d2e418f02b02f 100644 --- a/ets2panda/checker/ets/validateHelpers.cpp +++ b/ets2panda/checker/ets/validateHelpers.cpp @@ -82,10 +82,10 @@ void ETSChecker::ValidateCallExpressionIdentifier(ir::Identifier *const ident, T return; } - 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..edbbc38497724efa99d4a7ea63d37069c6744b80 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/enum_not_as_obj.ets @@ -0,0 +1,26 @@ +/* + * 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: Expected 2 arguments, got 1. */ +/* @@? 16:16 Error TypeError: No matching call signature for stringify(Colorset) */ +/* @@? 16:31 Error TypeError: Class name can't be the argument of function or method. */ +/* @@? 16:31 Error TypeError: Class or interface or enum 'Colorset' cannot be used as object */ +/* @@? 16:31 Error TypeError: Class name can't be the argument of function or method. */ +/* @@? 16:31 Error TypeError: Class or interface or enum 'Colorset' cannot be used as object */ +/* @@? 16:31 Error TypeError: Class name can't be the argument of function or method. */ \ 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/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 753294dc520da923de825aa8c3ac300ccc2ba152..5d663722425154b4fd1bd960eecd55d153f815aa 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -296,9 +296,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