From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 478533858D28; Thu, 6 Apr 2023 21:29:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 478533858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680816540; bh=m1hifTIZXuzh2uuHYiLTlyjYI0XqlXD3Ywrrlz9yLl0=; h=From:To:Subject:Date:From; b=bMJrMmXlpffvvuc58Cd3ZuVZzohOWtCPOUnJmcbpWp3Y7zWnbqFmxNPBlV+7lkSqM 71HQOpR9hl4H2uIqr5d9WrKclQoGgYro16ZoE/ji4dTT1csTqjsMFWy85IZH0elvI7 x71rDhQ2Ry2FSUKdOKJF888Vz01qwgsqwlL5XmNo= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] gccrs: fix ICE when closure body is not a block X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 0ddec9f44d6ce81eec7aa62c2d9b13c9094e7857 X-Git-Newrev: 89d83fd0e81825eeec4070d94851b8efc8e8c117 Message-Id: <20230406212900.478533858D28@sourceware.org> Date: Thu, 6 Apr 2023 21:29:00 +0000 (GMT) List-Id: https://gcc.gnu.org/g:89d83fd0e81825eeec4070d94851b8efc8e8c117 commit 89d83fd0e81825eeec4070d94851b8efc8e8c117 Author: Philip Herron Date: Wed Mar 29 16:14:04 2023 +0100 gccrs: fix ICE when closure body is not a block Fixes: #2052 gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::generate_closure_function): when its not a block we dont have any ribs to generate locals from gcc/testsuite/ChangeLog: * rust/execute/torture/issue-2052.rs: New test. Signed-off-by: Philip Herron Diff: --- gcc/rust/backend/rust-compile-expr.cc | 23 ++++++++++++++--------- gcc/testsuite/rust/execute/torture/issue-2052.rs | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 6a7b2eb1394..904c47b86be 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -2880,20 +2880,25 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr &expr, // lookup locals HIR::Expr *function_body = expr.get_expr ().get (); - auto body_mappings = function_body->get_mappings (); - Resolver::Rib *rib = nullptr; - bool ok - = ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (), &rib); - rust_assert (ok); + bool is_block_expr + = function_body->get_expression_type () == HIR::Expr::ExprType::Block; + + std::vector locals = {}; + if (is_block_expr) + { + auto body_mappings = function_body->get_mappings (); + Resolver::Rib *rib = nullptr; + bool ok + = ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (), + &rib); + rust_assert (ok); - std::vector locals - = compile_locals_for_block (ctx, *rib, fndecl); + locals = compile_locals_for_block (ctx, *rib, fndecl); + } tree enclosing_scope = NULL_TREE; Location start_location = function_body->get_locus (); Location end_location = function_body->get_locus (); - bool is_block_expr - = function_body->get_expression_type () == HIR::Expr::ExprType::Block; if (is_block_expr) { HIR::BlockExpr *body = static_cast (function_body); diff --git a/gcc/testsuite/rust/execute/torture/issue-2052.rs b/gcc/testsuite/rust/execute/torture/issue-2052.rs new file mode 100644 index 00000000000..6c15eb3a470 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/issue-2052.rs @@ -0,0 +1,15 @@ +#[lang = "fn_once"] +pub trait FnOnce { + #[lang = "fn_once_output"] + type Output; + + extern "rust-call" fn call_once(self, args: Args) -> Self::Output; +} + +pub fn f() -> i32 { + (|| 42)() +} + +pub fn main() -> i32 { + f() - 42 +}