From 6f0ec8ab0d4e0901cf8afea3922e76ca7959a022 Mon Sep 17 00:00:00 2001 From: fcc Date: Thu, 10 Jul 2025 16:16:56 +0800 Subject: [PATCH] fix crash when param's type annotation is null Check nullptr before use it to create new node. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICLGH0 Signed-off-by: fcc Change-Id: Ic083b33358ef526dfd1866c04bacd578e3613a02 --- ets2panda/checker/ets/function.cpp | 2 +- ets2panda/checker/ets/utilityTypeHandlers.cpp | 51 +++++++++++-------- .../param_typeannotation_null.ets | 25 +++++++++ .../astchecker/astchecker-ets-ignored.txt | 4 ++ 4 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/type_error_processing/param_typeannotation_null.ets diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index adeea4b940..55f265ba5c 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -1317,7 +1317,7 @@ static bool AppendSignatureInfoParam(ETSChecker *checker, SignatureInfo *sigInfo if (!param->IsOptional()) { ++sigInfo->minArgCount; } - ES2PANDA_ASSERT(!param->IsOptional() || + ES2PANDA_ASSERT(!param->IsOptional() || param->Ident()->TsType()->IsTypeError() || checker->Relation()->IsSupertypeOf(param->Ident()->TsType(), checker->GlobalETSUndefinedType())); return true; } diff --git a/ets2panda/checker/ets/utilityTypeHandlers.cpp b/ets2panda/checker/ets/utilityTypeHandlers.cpp index 94f186f970..256f917fe5 100644 --- a/ets2panda/checker/ets/utilityTypeHandlers.cpp +++ b/ets2panda/checker/ets/utilityTypeHandlers.cpp @@ -487,6 +487,34 @@ void ETSChecker::CreatePartialClassDeclaration(ir::ClassDefinition *const newCla newClassDefinition->Variable()->SetTsType(nullptr); } +static void SetupFunctionParams(ir::ScriptFunction *function, varbinder::FunctionParamScope *paramScope, + checker::ETSChecker *checker) +{ + for (auto *params : function->Params()) { + auto *paramExpr = params->AsETSParameterExpression(); + if (paramExpr->Ident()->TypeAnnotation() == nullptr) { + paramExpr->Ident()->SetTsTypeAnnotation(nullptr); + } else { + auto *unionType = + // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) + checker->AllocNode( + ArenaVector({paramExpr->Ident()->TypeAnnotation(), + // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) + checker->AllocNode(checker->Allocator())}, + checker->Allocator()->Adapter()), + checker->Allocator()); + paramExpr->Ident()->SetTsTypeAnnotation(unionType); + unionType->SetParent(paramExpr->Ident()); + } + auto [paramVar, node] = paramScope->AddParamDecl(checker->Allocator(), checker->VarBinder(), paramExpr); + if (node != nullptr) { + checker->VarBinder()->ThrowRedeclaration(node->Start(), paramVar->Name()); + } + + paramExpr->SetVariable(paramVar); + } +} + ir::MethodDefinition *ETSChecker::CreateNullishAccessor(ir::MethodDefinition *const accessor, ir::TSInterfaceDeclaration *interface) { @@ -531,27 +559,8 @@ ir::MethodDefinition *ETSChecker::CreateNullishAccessor(ir::MethodDefinition *co Allocator()->Adapter()), Allocator())); } else { - for (auto *params : function->Params()) { - auto *paramExpr = params->AsETSParameterExpression(); - - auto *unionType = - // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) - AllocNode( - ArenaVector({paramExpr->Ident()->TypeAnnotation(), - // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) - AllocNode(Allocator())}, - Allocator()->Adapter()), - Allocator()); - paramExpr->Ident()->SetTsTypeAnnotation(unionType); - unionType->SetParent(paramExpr->Ident()); - - auto [paramVar, node] = paramScope->AddParamDecl(Allocator(), VarBinder(), paramExpr); - if (node != nullptr) { - VarBinder()->ThrowRedeclaration(node->Start(), paramVar->Name()); - } - - paramExpr->SetVariable(paramVar); - } + // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) + SetupFunctionParams(function, paramScope, this); } nullishAccessor->SetOverloads(ArenaVector(Allocator()->Adapter())); diff --git a/ets2panda/test/ast/compiler/ets/type_error_processing/param_typeannotation_null.ets b/ets2panda/test/ast/compiler/ets/type_error_processing/param_typeannotation_null.ets new file mode 100644 index 0000000000..a630a094e6 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/type_error_processing/param_typeannotation_null.ets @@ -0,0 +1,25 @@ +/* + * 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 inter { + get value(): Double; + set value(A = Int.MAX_VALUE + 1); +} + +function foo(i: Partial>): void {} + +/* @@? 18:15 Error TypeError: The type of parameter 'A' cannot be inferred */ +/* @@? 18:15 Error TypeError: The type of parameter 'A' cannot be inferred */ +/* @@? 18:17 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ diff --git a/ets2panda/test/test-lists/astchecker/astchecker-ets-ignored.txt b/ets2panda/test/test-lists/astchecker/astchecker-ets-ignored.txt index bb991b21cc..0f620ba2ca 100644 --- a/ets2panda/test/test-lists/astchecker/astchecker-ets-ignored.txt +++ b/ets2panda/test/test-lists/astchecker/astchecker-ets-ignored.txt @@ -57,6 +57,10 @@ ast/compiler/ets/implicit_this_method_trailing.ets ast/parser/ets/import_tests/export_and_import_class.ets ast/parser/ets/import_tests/export_and_import_top_level.ets +# This case will emit different error messages on debug and release builds. +# It behaves different because `Phase::Apply` contains `#ifndef NDEBUG`. +ast/compiler/ets/type_error_processing/param_typeannotation_null.ets + # Issue: #22951 ast/compiler/ets/extension_accessor_tests/getterAsFunctionCall.ets ast/compiler/ets/extension_accessor_tests/getterAsMethodCall.ets -- Gitee