From bac0aaecb7851c10e456b7cd734a5f5b0f9f3fa9 Mon Sep 17 00:00:00 2001 From: zengzengran Date: Mon, 28 Jul 2025 15:22:52 +0800 Subject: [PATCH] Fix extension function parameter crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICPT05 Description: ensure that signature is not nullptr before calling the checkExtensionMethod() method for inspection. Tested-by: ninja tests (passed) ets_testrunner (passed) Signed-off-by: zengzengran # --- ets2panda/checker/ETSAnalyzerHelpers.cpp | 2 +- .../extension_function_error1.ets | 25 ++++++++++++++ .../extension_function_error2.ets | 33 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/parser/ets/extension_function_tests/extension_function_error1.ets create mode 100644 ets2panda/test/ast/parser/ets/extension_function_tests/extension_function_error2.ets diff --git a/ets2panda/checker/ETSAnalyzerHelpers.cpp b/ets2panda/checker/ETSAnalyzerHelpers.cpp index 644bdb35c6..d6d7255b0a 100644 --- a/ets2panda/checker/ETSAnalyzerHelpers.cpp +++ b/ets2panda/checker/ETSAnalyzerHelpers.cpp @@ -213,7 +213,7 @@ void DoBodyTypeChecking(ETSChecker *checker, ir::MethodDefinition *node, ir::Scr checker->AddStatus(checker::CheckerStatus::IN_CONSTRUCTOR); } - if (node->IsExtensionMethod()) { + if (node->IsExtensionMethod() && scriptFunc->Signature() != nullptr) { CheckExtensionMethod(checker, scriptFunc, node); } diff --git a/ets2panda/test/ast/parser/ets/extension_function_tests/extension_function_error1.ets b/ets2panda/test/ast/parser/ets/extension_function_tests/extension_function_error1.ets new file mode 100644 index 0000000000..47d3887c4c --- /dev/null +++ b/ets2panda/test/ast/parser/ets/extension_function_tests/extension_function_error1.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. + */ + +@AnimatableExtend +function animatableWidth(this: TextAttribute, this { this.width(width); return this; +} + +/* @@? 16:2 Error TypeError: Cannot find type 'AnimatableExtend'. */ +/* @@? 17:32 Error TypeError: Cannot find type 'TextAttribute'. */ +/* @@? 17:52 Error SyntaxError: The function parameter 'this' must explicitly specify the typeAnnotation. */ +/* @@? 17:52 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 17:54 Error TypeError: Cannot reference 'this' in this context. */ +/* @@? 17:59 Error TypeError: Property 'width' does not exist on type 'Error' */ diff --git a/ets2panda/test/ast/parser/ets/extension_function_tests/extension_function_error2.ets b/ets2panda/test/ast/parser/ets/extension_function_tests/extension_function_error2.ets new file mode 100644 index 0000000000..752a06d4cc --- /dev/null +++ b/ets2panda/test/ast/parser/ets/extension_function_tests/extension_function_error2.ets @@ -0,0 +1,33 @@ +/* + * 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 a = (this: TextAttribute, this { this.width(width); return this; +} + +/* @@? 16:9 Error TypeError: 'this' cannot be referenced from a static context */ +/* @@? 16:9 Error TypeError: Cannot reference 'this' in this context. */ +/* @@? 16:9 Error TypeError: Type 'Error' cannot be assigned to type 'ETSGLOBAL' */ +/* @@? 16:14 Error SyntaxError: Unexpected token, expected ')'. */ +/* @@? 16:16 Error SyntaxError: Unexpected token 'TextAttribute'. */ +/* @@? 16:16 Error TypeError: Unresolved reference TextAttribute */ +/* @@? 16:29 Error SyntaxError: Unexpected token ','. */ +/* @@? 16:31 Error SyntaxError: Unexpected token 'this'. */ +/* @@? 16:31 Error TypeError: Cannot reference 'this' in this context. */ +/* @@? 16:36 Error SyntaxError: Unexpected token '{'. */ +/* @@? 16:38 Error TypeError: Cannot reference 'this' in this context. */ +/* @@? 16:43 Error TypeError: Property 'width' does not exist on type 'Error' */ +/* @@? 16:57 Error SyntaxError: return keyword should be used in function body. */ +/* @@? 16:64 Error TypeError: Cannot reference 'this' in this context. */ +/* @@? 16:64 Error TypeError: All return statements in the function should be empty or have a value. */ -- Gitee