From 9b6766e6addd4ba23e67e24332a81c1bfa2de57d Mon Sep 17 00:00:00 2001 From: xuxinjie4 Date: Wed, 30 Jul 2025 17:50:05 +0800 Subject: [PATCH] Fix type use as value bug Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICPVH4?from=project-issue Signed-off-by: xuxinjie4 --- ets2panda/checker/ets/helpers.cpp | 4 ++++ ets2panda/checker/ets/validateHelpers.cpp | 8 ++++--- .../test/ast/compiler/ets/type_as_value.ets | 18 +++++++++++++++ .../ast/compiler/ets/typealias_as_value.ets | 22 +++++++++++++++++++ ets2panda/util/diagnostic/semantic.yaml | 4 ++++ 5 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/type_as_value.ets create mode 100644 ets2panda/test/ast/compiler/ets/typealias_as_value.ets diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index b7d1a386e70..8848cbfaa43 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -27,6 +27,7 @@ #include "evaluate/scopedDebugInfoPlugin.h" #include "compiler/lowering/scopesInit/scopesInitPhase.h" #include "compiler/lowering/util.h" +#include "generated/diagnostic.h" #include "util/es2pandaMacros.h" #include "util/helpers.h" #include "util/nameMangler.h" @@ -715,6 +716,9 @@ static bool SetPreferredTypeForExpression(ETSChecker *checker, ir::Identifier *i bool ETSChecker::CheckInit(ir::Identifier *ident, ir::TypeNode *typeAnnotation, ir::Expression *init, checker::Type *annotationType, varbinder::Variable *const bindingVar) { + if (init->IsETSTypeReference()) { + LogError(diagnostic::INVALID_TYPE_AS_VALUE, {init->DumpEtsSrc()}, init->Start()); + } if (typeAnnotation == nullptr) { if (init->IsArrayExpression()) { annotationType = CheckArrayElements(init->AsArrayExpression()); diff --git a/ets2panda/checker/ets/validateHelpers.cpp b/ets2panda/checker/ets/validateHelpers.cpp index a0bf21d4d27..765c1a3aade 100644 --- a/ets2panda/checker/ets/validateHelpers.cpp +++ b/ets2panda/checker/ets/validateHelpers.cpp @@ -196,7 +196,9 @@ void ETSChecker::ValidateResolvedIdentifier(ir::Identifier *const ident) auto *smartType = Context().GetSmartCast(resolved); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) auto *const resolvedType = GetApparentType(smartType != nullptr ? smartType : GetTypeOfVariable(resolved)); - + bool isValidResolved = + resolved != nullptr && !resolved->Declaration()->PossibleTDZ() && + !(resolvedType->IsETSFunctionType() && !resolved->Declaration()->Node()->IsTSTypeAliasDeclaration()); switch (ident->Parent()->Type()) { case ir::AstNodeType::CALL_EXPRESSION: // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) @@ -212,7 +214,7 @@ void ETSChecker::ValidateResolvedIdentifier(ir::Identifier *const ident) if (ValidateBinaryExpressionIdentifier(ident, resolvedType)) { return; } - if (resolved != nullptr && !resolved->Declaration()->PossibleTDZ() && !resolvedType->IsETSFunctionType()) { + if (isValidResolved) { WrongContextErrorClassifyByType(ident); } break; @@ -226,7 +228,7 @@ void ETSChecker::ValidateResolvedIdentifier(ir::Identifier *const ident) ValidateAssignmentIdentifier(ident, resolvedType); break; default: - if (resolved != nullptr && !resolved->Declaration()->PossibleTDZ() && !resolvedType->IsETSFunctionType()) { + if (isValidResolved) { WrongContextErrorClassifyByType(ident); } } diff --git a/ets2panda/test/ast/compiler/ets/type_as_value.ets b/ets2panda/test/ast/compiler/ets/type_as_value.ets new file mode 100644 index 00000000000..2fdb848aaef --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/type_as_value.ets @@ -0,0 +1,18 @@ +/* + * 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. + */ + +let a = number[][] + +/* @@? 16:17 Error TypeError: 'Array>' only refers to a type, but is being used as a value here. */ \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/typealias_as_value.ets b/ets2panda/test/ast/compiler/ets/typealias_as_value.ets new file mode 100644 index 00000000000..e6f3bb13a73 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/typealias_as_value.ets @@ -0,0 +1,22 @@ +/* + * 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. + */ + +type RF = (p:RF) => RF + +function main(){ + let v1 = () => {return RF} +} + +/* @@? 19:28 Error TypeError: Type name 'RF' used in the wrong context */ \ No newline at end of file diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 1df48358ebd..ce4b7642864 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -817,6 +817,10 @@ semantic: id: 55 message: "'{}' cannot be spread in tuple." +- name: INVALID_TYPE_AS_VALUE + id: 134683 + message: "'{}' only refers to a type, but is being used as a value here. " + - name: INVALID_TYPE_PARAM id: 168 message: "Type '{}' is not valid for generic type arguments" -- Gitee