From 87bc50ec1c9b6a3bd1c7ca9e8597d4b4994462c5 Mon Sep 17 00:00:00 2001 From: zengzengran Date: Wed, 21 May 2025 14:26:31 +0800 Subject: [PATCH] Fix crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IC9DUJ Description: In lambdalowering phase,ConvertFunctionReference() method uses method->Parent()->Parent()->AsClassDeclaration(), requiring method belong to class. Using error codes may bypass the rest of checks. Error report need to be added here. Tested-by: ninja tests (passed) ets_testrunner (passed) Signed-off-by: zengzengran # --- .../compiler/lowering/ets/lambdaLowering.cpp | 6 +++++ .../ets/lambda_point_interface_method.ets | 26 +++++++++++++++++++ ets2panda/util/diagnostic/semantic.yaml | 4 +++ 3 files changed, 36 insertions(+) create mode 100644 ets2panda/test/ast/parser/ets/lambda_point_interface_method.ets diff --git a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp index 656abd766b..c4f4e7abbc 100644 --- a/ets2panda/compiler/lowering/ets/lambdaLowering.cpp +++ b/ets2panda/compiler/lowering/ets/lambdaLowering.cpp @@ -1060,6 +1060,12 @@ static ir::AstNode *ConvertFunctionReference(public_lib::Context *ctx, ir::Expre return ConvertLambda(ctx, lam); } + if (!method->Parent()->Parent()->IsClassDeclaration()) { + ctx->checker->AsETSChecker()->LogError(diagnostic::LAMBDA_POINT_UNIMPL_MOTHOD, {method->Id()->Name()}, + funcRef->Start()); + return funcRef; + } + LambdaInfo info; info.calleeClass = method->Parent()->Parent()->AsClassDeclaration(); info.enclosingFunction = nullptr; diff --git a/ets2panda/test/ast/parser/ets/lambda_point_interface_method.ets b/ets2panda/test/ast/parser/ets/lambda_point_interface_method.ets new file mode 100644 index 0000000000..a0fc4ae8c6 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/lambda_point_interface_method.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. + */ + +declare function g(a: string): void; +let gg: (a:string) => void = g; + +interface Obj { + foo(value: T): void; +} + +declare const o: Obj; +gg = /* @@ label */o.foo; + +/* @@@ label Error TypeError: Lambda cannot point to unimplemented method 'foo' */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index bd0244b95e..32b81133bc 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -1474,3 +1474,7 @@ semantic: - name: MERGED_DECLS id: 370 message: "Merging declarations is not supported, please keep all definitions of classes, interfaces and enums compact in the codebase!" + +- name: LAMBDA_POINT_UNIMPL_MOTHOD + id: 371 + message: "Lambda cannot point to unimplemented method '{}'" -- Gitee