From 30c276a1745702413625f84361257ce24b17b448 Mon Sep 17 00:00:00 2001 From: xuxinjie4 Date: Fri, 1 Aug 2025 11:26:52 +0800 Subject: [PATCH] Fix serval crashs Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQB4R?from=project-issue Signed-off-by: xuxinjie4 --- ets2panda/checker/ets/function.cpp | 8 +++-- ets2panda/checker/ets/utilityTypeHandlers.cpp | 6 +++- .../test/ast/compiler/ets/broken_setter.ets | 32 +++++++++++++++++++ .../ets/overload_for_named_constructor.ets | 26 +++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/broken_setter.ets create mode 100644 ets2panda/test/ast/compiler/ets/overload_for_named_constructor.ets diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 0996cf7267..31f6f7a3a5 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -1424,8 +1424,12 @@ Signature *ETSChecker::ResolveConstructExpression(ETSObjectType *type, const Are { auto *var = type->GetProperty(compiler::Signatures::CONSTRUCTOR_NAME, PropertySearchFlags::SEARCH_STATIC_METHOD); if (var != nullptr && var->TsType()->IsETSFunctionType()) { - return MatchOrderSignatures(var->TsType()->AsETSFunctionType()->CallSignatures(), nullptr, arguments, pos, - TypeRelationFlag::NONE); + auto sig = MatchOrderSignatures(var->TsType()->AsETSFunctionType()->CallSignatures(), nullptr, arguments, pos, + TypeRelationFlag::NONE); + if (sig == nullptr) { + ThrowOverloadMismatch(type->Name(), arguments, pos, "construct"); + } + return sig; } // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) return ValidateSignatures(type->ConstructSignatures(), nullptr, arguments, pos, "construct"); diff --git a/ets2panda/checker/ets/utilityTypeHandlers.cpp b/ets2panda/checker/ets/utilityTypeHandlers.cpp index 93f6b5eccc..22cd11c5f0 100644 --- a/ets2panda/checker/ets/utilityTypeHandlers.cpp +++ b/ets2panda/checker/ets/utilityTypeHandlers.cpp @@ -503,8 +503,12 @@ void ETSChecker::CreatePartialClassDeclaration(ir::ClassDefinition *const newCla prop->AsMethodDefinition()->Function()->IsSetter())) { auto *method = prop->AsMethodDefinition(); ES2PANDA_ASSERT(method->Id() != nullptr); + auto isBrokenSetter = method->Function()->IsSetter() && method->Function()->Params().size() != 1; + auto isBrokenGetter = method->Function()->IsGetter() && !method->Function()->Params().empty(); if (newClassDefinition->Scope()->FindLocal(method->Id()->Name(), - varbinder::ResolveBindingOptions::VARIABLES) != nullptr) { + varbinder::ResolveBindingOptions::VARIABLES) != nullptr || + isBrokenSetter || isBrokenGetter) { + ES2PANDA_ASSERT(IsAnyError()); continue; } diff --git a/ets2panda/test/ast/compiler/ets/broken_setter.ets b/ets2panda/test/ast/compiler/ets/broken_setter.ets new file mode 100644 index 0000000000..9330afc173 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/broken_setter.ets @@ -0,0 +1,32 @@ +/* + * 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 i = 0 + +class A{ + set p(){ + i+=1 + } + + get p():number { + return this.p + } +} + +function foo(p:Partial){} + +/* @@? 23:5 Error SyntaxError: Setter must have exactly one formal parameter. */ +/* @@? 23:5 Error TypeError: Function p is already declared. */ +/* @@? 24:21 Warning Warning: Reading the value of the property inside its getter may lead to an endless loop. */ \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/overload_for_named_constructor.ets b/ets2panda/test/ast/compiler/ets/overload_for_named_constructor.ets new file mode 100644 index 0000000000..0575959c2f --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/overload_for_named_constructor.ets @@ -0,0 +1,26 @@ +/** + * 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{ + overload constructor {foo, bar}; + constructor foo(a:string, b?:string){} + constructor bar(a:number, b?:number){} +} + +function main(){ + new A() +} + +/* @@? 23:5 Error TypeError: No matching construct signature for A() */ \ No newline at end of file -- Gitee