From de47967437306489c2d05b441cd8a5e5658ed36a Mon Sep 17 00:00:00 2001 From: furkanocalan Date: Mon, 23 Jun 2025 16:43:27 +0300 Subject: [PATCH] Fix abstract method usage as value Issue: #ICH8OD Description: Cannot use abstract method as value Signed-off-by: furkanocalan --- ets2panda/checker/ETSAnalyzer.cpp | 5 ++++ .../ets/abstract_value_incompatible.ets | 27 +++++++++++++++++++ ets2panda/util/diagnostic/semantic.yaml | 4 +++ 3 files changed, 36 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/abstract_value_incompatible.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index ca365e2a61..a218222cd8 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1696,6 +1696,11 @@ static Type *TransformTypeForMethodReference(ETSChecker *checker, ir::Expression } } + if (signatures.empty()) { + checker->LogError(diagnostic::ABSTRACT_METHOD_AS_VALUE, getUseSite()); + return checker->GlobalTypeError(); + } + if (signatures.size() > 1U) { checker->LogError(diagnostic::OVERLOADED_METHOD_AS_VALUE, getUseSite()); return checker->GlobalTypeError(); diff --git a/ets2panda/test/ast/compiler/ets/abstract_value_incompatible.ets b/ets2panda/test/ast/compiler/ets/abstract_value_incompatible.ets new file mode 100644 index 0000000000..7b55e20d95 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/abstract_value_incompatible.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. + */ + +interface MqttClient { + publish(): void; +} + +class MockMqttClient implements MqttClient { + publish(): void {} +} + +const client = new MockMqttClient() as MqttClient; +client.publish + +/* @@? 25:8 Error TypeError: Abstract method is used as value */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 4c4ae2e28e..24a7e571dc 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -1514,3 +1514,7 @@ semantic: - name: DYMANIC_INIT_WITH_OBJEXPR id: 382 message: "Dymanic Type {} cannot be initialize with an object expression" + +- name: ABSTRACT_METHOD_AS_VALUE + id: 383 + message: "Abstract method is used as value" -- Gitee