diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 8216c753cdaba25610609691cbdcd42cbec13d33..a14db8776927437877751363f34f4564e150fb92 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -1807,6 +1807,10 @@ Type *ETSChecker::GetReferencedTypeBase(ir::Expression *name) return name->Check(this); } + if (name->IsLiteral()) { + return name->Check(this); + } + ES2PANDA_ASSERT(name->IsIdentifier()); if (name->AsIdentifier()->Variable() == nullptr) { // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) diff --git a/ets2panda/checker/ets/typeCheckingHelpers.cpp b/ets2panda/checker/ets/typeCheckingHelpers.cpp index 60298e8aebafa4f4e8e38054fc6856f5f422a131..534203ded5bac9a87d4b97ff82fd894fedc66d40 100644 --- a/ets2panda/checker/ets/typeCheckingHelpers.cpp +++ b/ets2panda/checker/ets/typeCheckingHelpers.cpp @@ -1197,7 +1197,8 @@ static auto IsNonArrayLiteral(ir::Expression *init) return true; } - if (init->TsType()->IsETSEnumType() && init->TsType()->AsETSEnumType()->NodeIsEnumLiteral(init)) { + if (init->TsType() != nullptr && init->TsType()->IsETSEnumType() && + init->TsType()->AsETSEnumType()->NodeIsEnumLiteral(init)) { return true; } return false; diff --git a/ets2panda/compiler/lowering/ets/defaultParametersInConstructorLowering.cpp b/ets2panda/compiler/lowering/ets/defaultParametersInConstructorLowering.cpp index 1362d01997d8bcfe69a5c0aa6c894405b4124a70..5f71a7becde5865645818d97b9dcffbbf2035a3b 100644 --- a/ets2panda/compiler/lowering/ets/defaultParametersInConstructorLowering.cpp +++ b/ets2panda/compiler/lowering/ets/defaultParametersInConstructorLowering.cpp @@ -216,11 +216,13 @@ static void ExpandOptionalParameterAnnotationsToUnions(public_lib::Context *ctx, for (auto p : function->Params()) { auto param = p->AsETSParameterExpression(); if (param->IsOptional() && param->Initializer() == nullptr) { - param->SetTypeAnnotation(util::NodeAllocator::ForceSetParent( - allocator, - ArenaVector({param->TypeAnnotation(), allocator->New(allocator)}, - allocator->Adapter()), - allocator)); + ArenaVector typeNodes(allocator->Adapter()); + if (param->TypeAnnotation() != nullptr) { + typeNodes.emplace_back(param->TypeAnnotation()); + } + typeNodes.emplace_back(allocator->New(allocator)); + param->SetTypeAnnotation( + util::NodeAllocator::ForceSetParent(allocator, std::move(typeNodes), allocator)); param->TypeAnnotation()->SetParent(param->Ident()); } } diff --git a/ets2panda/ir/ets/etsTypeReference.cpp b/ets2panda/ir/ets/etsTypeReference.cpp index e659e054749883d7336b57ff580217943c49d5bd..b8d7c80ee796ff494e09ab28c19f0f1be1cc69bc 100644 --- a/ets2panda/ir/ets/etsTypeReference.cpp +++ b/ets2panda/ir/ets/etsTypeReference.cpp @@ -64,6 +64,7 @@ ir::Identifier *ETSTypeReference::BaseName() const if (baseName->IsIdentifier()) { return baseName->AsIdentifier(); } + if (baseName->IsTSQualifiedName()) { ir::TSQualifiedName *iter = baseName->AsTSQualifiedName(); @@ -72,12 +73,23 @@ ir::Identifier *ETSTypeReference::BaseName() const } return iter->Left()->AsIdentifier(); } - ir::MemberExpression *iter = baseName->AsMemberExpression(); - while (iter->Property()->IsMemberExpression()) { - iter = iter->Property()->AsMemberExpression(); + if (baseName->IsMemberExpression()) { + ir::MemberExpression *iter = baseName->AsMemberExpression(); + + while (iter->Property()->IsMemberExpression()) { + iter = iter->Property()->AsMemberExpression(); + } + return iter->Property()->AsIdentifier(); + } + + if (baseName->IsLiteral()) { + ES2PANDA_ASSERT(baseName->OriginalNode() != nullptr && baseName->OriginalNode()->IsIdentifier()); + return baseName->OriginalNode()->AsIdentifier(); } - return iter->Property()->AsIdentifier(); + + ES2PANDA_UNREACHABLE(); + return nullptr; } void ETSTypeReference::Dump(ir::AstDumper *dumper) const diff --git a/ets2panda/test/ast/compiler/ets/Const_Literal_As_TypeAnnotation_1.ets b/ets2panda/test/ast/compiler/ets/Const_Literal_As_TypeAnnotation_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..6bcecc08b108c82e890c26cca09adede03514cfd --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/Const_Literal_As_TypeAnnotation_1.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +const e = 123 +@interface Ajno { + b:e: string[][X (c + d)],% + +/* @@? 18:7 Error TypeError: Cannot find type 'e'. */ +/* @@? 18:17 Error SyntaxError: Unexpected token ']'. */ +/* @@? 18:29 Error SyntaxError: Unexpected token, expected ']'. */ +/* @@? 25:1 Error SyntaxError: Unexpected token 'end of stream'. */ + diff --git a/ets2panda/test/ast/compiler/ets/Const_Literal_As_TypeAnnotation_2.ets b/ets2panda/test/ast/compiler/ets/Const_Literal_As_TypeAnnotation_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..733c4a5ea39287c5feff86ad9570d5792ccd1d8f --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/Const_Literal_As_TypeAnnotation_2.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +const e = true +@interface Ajno { + b:e: string[][X (c + d)],% + +/* @@? 18:7 Error TypeError: Cannot find type 'e'. */ +/* @@? 18:17 Error SyntaxError: Unexpected token ']'. */ +/* @@? 18:29 Error SyntaxError: Unexpected token, expected ']'. */ +/* @@? 25:1 Error SyntaxError: Unexpected token 'end of stream'. */ + diff --git a/ets2panda/test/ast/compiler/ets/negative_optional_constructor.ets b/ets2panda/test/ast/compiler/ets/negative_optional_constructor.ets new file mode 100644 index 0000000000000000000000000000000000000000..f38f0c8d9deab4aa9c5463a0313c20dbb497607f --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/negative_optional_constructor.ets @@ -0,0 +1,27 @@ +/* + * 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. + */ + +class A { + constructor(buffer: ArrayBuffer, byteOffset?: number, length?V number); + +/* @@? 17:3 Error TypeError: No matching call signature for constructor */ +/* @@? 17:14 Error TypeError: Only abstract or native methods can't have body. */ +/* @@? 17:64 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ +/* @@? 17:64 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 17:65 Error SyntaxError: Field type annotation expected. */ +/* @@? 17:66 Error SyntaxError: number is a predefined type, cannot be used as an identifier */ +/* @@? 17:72 Error SyntaxError: Unexpected token ')'. */ +/* @@? 28:1 Error SyntaxError: Expected '}', got 'end of stream'. */ + diff --git a/ets2panda/test/ast/compiler/ets/negative_typo_1.ets b/ets2panda/test/ast/compiler/ets/negative_typo_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..946b6b4974729224417dbc0a781eda7c5d92fd7f --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/negative_typo_1.ets @@ -0,0 +1,23 @@ +/* + * 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. + */ + +@interface Anno { + e: @ring[][] = [["a", (c +ue)]] +} + +/* @@? 17:3 Error TypeError: Invalid annotation field type. Only numeric, boolean, string, enum, or arrays of these types are permitted for annotation fields. */ +/* @@? 17:13 Error TypeError: Cannot find type 'ring'. */ +/* @@? 17:18 Error TypeError: Invalid value for annotation field, expected a constant literal. */ +/* @@? 17:19 Error TypeError: Initializer has 2 elements, but tuple requires 0 */