diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 0304a530b6a73450f76c6191721609c703273aea..79c2c702c4c808088a46c8c59b1a3f155059e4d5 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -411,16 +411,20 @@ static bool CheckArrayElementType(ETSChecker *checker, T *newArrayInstanceExpr) checker->ValidateSignatureAccessibility(calleeObj, nullptr, newArrayInstanceExpr->Signature(), newArrayInstanceExpr->Start()); } else { - checker->LogTypeError("Cannot use array creation expression with abstract classes and interfaces.", - newArrayInstanceExpr->Start()); + checker->LogError(diagnostic::ABSTRACT_CLASS_AS_ARRAY_ELEMENT_TYPE, {}, newArrayInstanceExpr->Start()); + return false; + } + } else { + if (!checker->Relation()->IsSupertypeOf(elementType, checker->GlobalETSUndefinedType()) && + !checker->Relation()->IsIdenticalTo(checker->GetApparentType(elementType), elementType)) { + checker->LogError(diagnostic::TYPE_PARAMETER_AS_ARRAY_ELEMENT_TYPE, {}, newArrayInstanceExpr->Start()); + return false; + } + if (!checker->Relation()->IsSupertypeOf(elementType, checker->GlobalETSUndefinedType())) { + checker->LogError(diagnostic::NON_SUPERTYPE_OF_UNDEFINED_AS_ARRAY_ELEMENT_TYPE, {}, + newArrayInstanceExpr->Start()); return false; } - } else if (!checker->Relation()->IsSupertypeOf(elementType, checker->GlobalETSUndefinedType())) { - checker->LogTypeError( - "Cannot use array creation expression with non-constructable element type which is " - "non-assignable from undefined.", - newArrayInstanceExpr->Start()); - return false; } return true; } @@ -1239,7 +1243,7 @@ Type *ETSAnalyzer::GetReturnType(ir::CallExpression *expr, Type *calleeType) con if (!calleeType->IsETSFunctionType() && !expr->IsETSConstructorCall() && !calleeType->IsETSExtensionFuncHelperType()) { - checker->LogError(diagnostic::NO_CALL_SINGATURE, {calleeType}, expr->Start()); + checker->LogError(diagnostic::NO_CALL_SIGNATURE, {calleeType}, expr->Start()); return checker->GlobalTypeError(); } diff --git a/ets2panda/test/ast/compiler/ets/array_with_type_parameter.sts b/ets2panda/test/ast/compiler/ets/array_with_type_parameter.sts new file mode 100644 index 0000000000000000000000000000000000000000..467c6c8d203f2232ead8caa835b8e47abf6fa8dc --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/array_with_type_parameter.sts @@ -0,0 +1,98 @@ +/* + * 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. + */ + +// 1. T serves as the element type +function foo() { + new T[2] +} + +function foo1() { + new T[2] + new U[2][3] +} + +interface Lengthwise { + length : number +} + +function foo2() { + new T[2] +} + +function foo3() { + new T[2] +} + +// 2. the alias of T serves as the element type +function foo4() { + type U = T + new T[3] + new U[2] +} + +// 3. T is generic of a class, and this class serves as the element type +class A { + foo(){ + new A[2] + } +} + +// 4. T is a field type of a class, and this class serves as the element type +class B { + field : T + foo() { + new B[2] + } +} + +// 5. T is a subtype of an union, and this union serves as the element type +class C { + foo() { + new (T|undefined)[2] + new (T|int)[2] + } +} + +function foo5() { + new (T|undefined)[2] + new (T|Object)[2] +} + +// 6. T is a subtype of a tuple, and this tuple serves as the element type +class D { + foo() { + new [T|undefined][2] + new [T|int][2] + } +} + +function foo6() { + new [T|undefined][2] + new [T|Object][2] +} + +/* @@? 18:5 Error TypeError: Cannot use array creation expression with type parameter. */ +/* @@? 22:5 Error TypeError: Cannot use array creation expression with type parameter. */ +/* @@? 23:5 Error TypeError: Cannot use array creation expression with type parameter. */ +/* @@? 31:5 Error TypeError: Cannot use array creation expression with type parameter. */ +/* @@? 35:5 Error TypeError: Cannot use array creation expression with type parameter. */ +/* @@? 41:5 Error TypeError: Cannot use array creation expression with type parameter. */ +/* @@? 42:5 Error TypeError: Cannot use array creation expression with type parameter. */ +/* @@? 64:9 Error TypeError: Cannot use array creation expression with type parameter. */ +/* @@? 70:5 Error TypeError: Cannot use array creation expression with type parameter. */ +/* @@? 76:9 Error TypeError: Cannot use array creation expression with non-constructable element type which is non-assignable from undefined. */ +/* @@? 77:9 Error TypeError: Cannot use array creation expression with non-constructable element type which is non-assignable from undefined. */ +/* @@? 82:5 Error TypeError: Cannot use array creation expression with non-constructable element type which is non-assignable from undefined. */ +/* @@? 83:5 Error TypeError: Cannot use array creation expression with non-constructable element type which is non-assignable from undefined. */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index fc3f28f6073944d7c0f2bd9bc7ae07329e195395..491f6e2352792d8fba548cac4d42cd4a2fa23abf 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -16,7 +16,7 @@ semantic: id: 1 message: Return type of async function must be 'Promise'. -- name: NO_CALL_SINGATURE +- name: NO_CALL_SIGNATURE id: 2 message: Type '{}' has no call signatures. @@ -35,3 +35,15 @@ semantic: - name: STANDARD_ANNOTATION_REQUIRED id: 6 message: Only standard annotations are allowed to be applied on annotations. + +- name: ABSTRACT_CLASS_AS_ARRAY_ELEMENT_TYPE + id: 7 + message: Cannot use array creation expression with abstract classes and interfaces. + +- name: TYPE_PARAMETER_AS_ARRAY_ELEMENT_TYPE + id: 8 + message: Cannot use array creation expression with type parameter. + +- name: NON_SUPERTYPE_OF_UNDEFINED_AS_ARRAY_ELEMENT_TYPE + id: 9 + message: Cannot use array creation expression with non-constructable element type which is non-assignable from undefined.