]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: fix ICE when closure body is not a block
authorPhilip Herron <herron.philip@googlemail.com>
Wed, 29 Mar 2023 15:14:04 +0000 (16:14 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:39 +0000 (18:28 +0100)
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 <herron.philip@googlemail.com>
gcc/rust/backend/rust-compile-expr.cc
gcc/testsuite/rust/execute/torture/issue-2052.rs [new file with mode: 0644]

index 962135bad49edbbec6ed1eb5f04e12cc1aee9857..e3b012c152716867ca9949622a9bc8f723740864 100644 (file)
@@ -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<Bvariable *> 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<Bvariable *> 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<HIR::BlockExpr *> (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 (file)
index 0000000..6c15eb3
--- /dev/null
@@ -0,0 +1,15 @@
+#[lang = "fn_once"]
+pub trait FnOnce<Args> {
+    #[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
+}