From b55fb1a92e9ac005c39c4de38d2777c05dfc6edd Mon Sep 17 00:00:00 2001 From: tengtengh Date: Thu, 24 Jul 2025 11:10:51 +0800 Subject: [PATCH] fix es2panda aborts Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICOCMD Signed-off-by: tengtengh --- ets2panda/checker/ets/helpers.cpp | 4 +++ ets2panda/checker/ets/typeCheckingHelpers.cpp | 3 ++- ...defaultParametersInConstructorLowering.cpp | 12 +++++---- ets2panda/ir/ets/etsTypeReference.cpp | 20 +++++++++++--- .../ets/Const_Literal_As_TypeAnnotation_1.ets | 24 +++++++++++++++++ .../ets/Const_Literal_As_TypeAnnotation_2.ets | 24 +++++++++++++++++ .../ets/negative_optional_constructor.ets | 27 +++++++++++++++++++ .../test/ast/compiler/ets/negative_typo_1.ets | 23 ++++++++++++++++ 8 files changed, 127 insertions(+), 10 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/Const_Literal_As_TypeAnnotation_1.ets create mode 100644 ets2panda/test/ast/compiler/ets/Const_Literal_As_TypeAnnotation_2.ets create mode 100644 ets2panda/test/ast/compiler/ets/negative_optional_constructor.ets create mode 100644 ets2panda/test/ast/compiler/ets/negative_typo_1.ets diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 8216c753cd..a14db87769 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 60298e8aeb..534203ded5 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 1362d01997..5f71a7becd 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 e659e05474..b8d7c80ee7 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 0000000000..6bcecc08b1 --- /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 0000000000..733c4a5ea3 --- /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 0000000000..f38f0c8d9d --- /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 0000000000..946b6b4974 --- /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 */ -- Gitee