From 7cadc11be9dfb1a418d9d7041c5a013d04184bf7 Mon Sep 17 00:00:00 2001 From: oh-rgx Date: Wed, 9 Jul 2025 20:51:21 +0800 Subject: [PATCH] Fix checkConstructor crash Issue: #ICL88N Signed-off-by: oh-rgx --- ets2panda/checker/ets/object.cpp | 5 ++ .../compiler/ets/class_cyclic_constructor.ets | 56 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/class_cyclic_constructor.ets diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index d048894b2c..cba1662d38 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -1814,6 +1814,11 @@ void ETSChecker::CheckCyclicConstructorCall(Signature *signature) return; } + // This is a condition set up to handle error scenarios. + if (signature->Function()->Body() == nullptr) { + return; + } + auto *funcBody = signature->Function()->Body()->AsBlockStatement(); TypeStackElement tse(this, signature, {{diagnostic::RECURSIVE_CTOR}}, signature->Function()->Start()); diff --git a/ets2panda/test/ast/compiler/ets/class_cyclic_constructor.ets b/ets2panda/test/ast/compiler/ets/class_cyclic_constructor.ets new file mode 100644 index 0000000000..68b81eef70 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/class_cyclic_constructor.ets @@ -0,0 +1,56 @@ +/* + * 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 { + public x : int; + + public constructor() { + this(5); + } + + public constructor( + let tuple: {{c.to_type}} = {{caches.value}}; + let a : A = new A(...tuple); + let method_result = A.bar(...tuple); + ) { + this.x = a; + } +} + +/* @@? 23:23 Error TypeError: Only abstract or native methods can't have body. */ +/* @@? 24:9 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ +/* @@? 24:9 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 24:9 Error SyntaxError: Unexpected token 'let'. */ +/* @@? 24:9 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 24:20 Error SyntaxError: Invalid Type. */ +/* @@? 24:20 Error SyntaxError: Unexpected token '{'. */ +/* @@? 24:21 Error SyntaxError: Unexpected token '{'. */ +/* @@? 24:23 Error SyntaxError: Field type annotation expected. */ +/* @@? 24:23 Error SyntaxError: Unexpected token '.'. */ +/* @@? 24:31 Error SyntaxError: Field type annotation expected. */ +/* @@? 24:32 Error SyntaxError: Unexpected token '}'. */ +/* @@? 24:37 Error SyntaxError: Unexpected token. */ +/* @@? 24:38 Error SyntaxError: Unexpected token. */ +/* @@? 24:51 Error SyntaxError: Unexpected token '}'. */ +/* @@? 25:21 Error TypeError: Expected 0 arguments, got 1. */ +/* @@? 25:21 Error TypeError: No matching construct signature for class_cyclic_constructor.A(...tuple) */ +/* @@? 25:27 Error TypeError: Spread argument cannot be passed for ordinary parameter. */ +/* @@? 25:30 Error TypeError: Unresolved reference tuple */ +/* @@? 26:31 Error TypeError: Property 'bar' does not exist on type 'A' */ +/* @@? 27:5 Error SyntaxError: Unexpected token ')'. */ +/* @@? 27:7 Error SyntaxError: Unexpected token '{'. */ +/* @@? 28:9 Error TypeError: Cannot reference 'this' in this context. */ +/* @@? 28:14 Error TypeError: Property 'x' does not exist on type 'Error' */ +/* @@? 30:1 Error SyntaxError: Unexpected token '}'. */ -- Gitee