From 56092a663f04888b2a488d51b48c0d3cec0ef5f1 Mon Sep 17 00:00:00 2001 From: Tamas Toth Date: Tue, 3 Jun 2025 12:55:24 +0200 Subject: [PATCH] Throw error when setter return type is void Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICCAJQ Fixes #25831 internal issue Change-Id: I185d3df441f95b639d335c198d12b6b6f4ec1ebf Signed-off-by: Tamas Toth --- ets2panda/parser/ETSparserClasses.cpp | 5 ++++ .../parser/ets/setter_with_return_type.ets | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 ets2panda/test/ast/parser/ets/setter_with_return_type.ets diff --git a/ets2panda/parser/ETSparserClasses.cpp b/ets2panda/parser/ETSparserClasses.cpp index c1dc7c92e0..fcfad7919b 100644 --- a/ets2panda/parser/ETSparserClasses.cpp +++ b/ets2panda/parser/ETSparserClasses.cpp @@ -834,6 +834,11 @@ ir::MethodDefinition *ETSParser::ParseInterfaceGetterSetterMethod(const ir::Modi method->Function()->SetIdent(method->Id()->Clone(Allocator(), nullptr)); method->Function()->AddModifier(method->Modifiers()); + bool hasReturn = method->Function()->ReturnTypeAnnotation() != nullptr; + if (hasReturn && methodKind == ir::MethodDefinitionKind::SET) { + LogError(diagnostic::SETTER_NO_RETURN_TYPE, {}, method->Function()->Range().start); + } + return method; } diff --git a/ets2panda/test/ast/parser/ets/setter_with_return_type.ets b/ets2panda/test/ast/parser/ets/setter_with_return_type.ets new file mode 100644 index 0000000000..6764147803 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/setter_with_return_type.ets @@ -0,0 +1,28 @@ +/* + * 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 C{ + n: number = 42 +} + +interface I{ + get n(): number + set n(v:number):void +} + +class D extends C implements I{ +} + +/* @@? 22:10 Error SyntaxError: Setter must not have return type even if it is void. */ -- Gitee