diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index b7d1a386e70f2c2b8537470e0e73d55134783d2c..8848cbfaa436031bac0abfa0866a1915484653d5 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 a0bf21d4d27999d9a2028f848dae48b167269201..765c1a3aade945fbaa2738ab2dda010d17c5425a 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 0000000000000000000000000000000000000000..2fdb848aaef88b463c0cb23c3ad397fee714d7a1 --- /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 0000000000000000000000000000000000000000..e6f3bb13a735fddfbc53d9b1ef91d04416d4b067 --- /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 1df48358ebd838e6f18fc9f7af59cbda229a2834..ce4b7642864e2e9df0feef8f4abe1de279d87ae4 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"