From: Utkarsh Bahuguna <[email protected]>

In a const fn, a continue inside loop, or a labeled break or continue,
was either rejected as a goto or ran into the loop iteration limit.
Treat loop labels as compiler labels even when the loop is named, put
the continue label of loop at the start of its body like for while,
and let a loop pass on jumps to labels outside of it.

Fixes Rust-GCC/gccrs#5014

Assisted-by: Claude Opus 5.5 (Anthropic)

gcc/rust/ChangeLog:

        * backend/rust-compile-expr.cc (CompileExpr::visit (LoopExpr)): Put
        the continue label at the start of the loop body.
        (CompileExpr::construct_loop_labels): Mark the labels artificial.
        * backend/rust-constexpr.cc (find_label_expr_r): New function.
        (eval_loop_expr): Leave the loop on a jump to a label outside of it.

gcc/testsuite/ChangeLog:

        * rust/execute/torture/const-fn-loop-jumps.rs: New test.

Signed-off-by: Utkarsh Bahuguna <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/5a902905fdb37f6cb0e41a8e119eb2966552f393

The commit has been mentioned in the following issue(s):
 - Rust-GCC/gccrs#5014: https://github.com/Rust-GCC/gccrs/issues/5014

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/5016

 gcc/rust/backend/rust-compile-expr.cc         | 18 ++++-
 gcc/rust/backend/rust-constexpr.cc            | 20 ++++++
 .../execute/torture/const-fn-loop-jumps.rs    | 69 +++++++++++++++++++
 3 files changed, 105 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/rust/execute/torture/const-fn-loop-jumps.rs

diff --git a/gcc/rust/backend/rust-compile-expr.cc 
b/gcc/rust/backend/rust-compile-expr.cc
index 9798a9b30..101ca198c 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -993,13 +993,23 @@ CompileExpr::visit (HIR::LoopExpr &expr)
   tree loop_begin_label = loop_labels.first;
   tree loop_end_label = loop_labels.second;
 
-  // label before the loop - continue should goto here
+  std::vector<Bvariable *> locals;
+  location_t start_location = expr.get_loop_block ().get_locus ();
+  tree loop_block = Backend::block (fnctx.fndecl, enclosing_scope, locals,
+                                   start_location, start_location);
+  ctx->push_block (loop_block);
+
+  // label at the start of the loop body - continue should goto here, like in
+  // while loops
   ctx->add_statement (loop_begin_label);
 
   // loop body
   tree code_block
     = CompileBlock::compile (expr.get_loop_block (), ctx, nullptr);
-  tree loop_expr = Backend::loop_expression (code_block, expr.get_locus ());
+  ctx->add_statement (code_block);
+  ctx->pop_block ();
+
+  tree loop_expr = Backend::loop_expression (loop_block, expr.get_locus ());
   ctx->add_statement (loop_expr);
 
   ctx->pop_loop_context ();
@@ -3486,6 +3496,10 @@ CompileExpr::construct_loop_labels 
(tl::optional<HIR::LoopLabel> opt_loop_label)
   break_label_decl
     = Backend::label (fnctx.fndecl, break_label_name, label_locus);
   break_label_expr = Backend::label_definition_statement (break_label_decl);
+  // these are compiler-made jump targets even when the loop has a name, so
+  // const eval can treat the gotos to them like any other loop jump
+  DECL_ARTIFICIAL (continue_label_decl) = 1;
+  DECL_ARTIFICIAL (break_label_decl) = 1;
   if (label_hirid.has_value ())
     {
       ctx->insert_continue_label (label_hirid.value (), continue_label_decl);
diff --git a/gcc/rust/backend/rust-constexpr.cc 
b/gcc/rust/backend/rust-constexpr.cc
index 15b705e3b..3cdbf62cc 100644
--- a/gcc/rust/backend/rust-constexpr.cc
+++ b/gcc/rust/backend/rust-constexpr.cc
@@ -5120,6 +5120,18 @@ switches (tree *jump_target)
   return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
 }
 
+/* walk_tree callback: is DATA the label of the LABEL_EXPR *TP?  */
+
+static tree
+find_label_expr_r (tree *tp, int *walk_subtrees, void *data)
+{
+  if (TREE_CODE (*tp) == LABEL_EXPR && LABEL_EXPR_LABEL (*tp) == (tree) data)
+    return *tp;
+  if (TYPE_P (*tp))
+    *walk_subtrees = 0;
+  return NULL_TREE;
+}
+
 // forked from gcc/cp/constexpr.cc cxx_eval_loop_expr
 
 /* Evaluate a LOOP_EXPR for side-effects.  Handles break and return
@@ -5178,6 +5190,14 @@ eval_loop_expr (const constexpr_ctx *ctx, tree t, bool 
*non_constant_p,
              break;
            }
 
+         /* A jump to a label outside of the loop leaves it, e.g. a labeled
+            break or continue of an enclosing loop.  */
+         if (*jump_target && TREE_CODE (*jump_target) == LABEL_DECL
+             && !continues (jump_target)
+             && !walk_tree_without_duplicates (&body, find_label_expr_r,
+                                               *jump_target))
+           break;
+
          if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
            *jump_target = NULL_TREE;
 
diff --git a/gcc/testsuite/rust/execute/torture/const-fn-loop-jumps.rs 
b/gcc/testsuite/rust/execute/torture/const-fn-loop-jumps.rs
new file mode 100644
index 000000000..846aa96c3
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/const-fn-loop-jumps.rs
@@ -0,0 +1,69 @@
+// { dg-additional-options "-w" }
+#![feature(no_core, lang_items)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+const fn loop_continue() -> i32 {
+    let mut i = 0;
+    let mut n = 0;
+    loop {
+        i += 1;
+        if i > 5 {
+            break;
+        }
+        if i == 2 {
+            continue;
+        }
+        n += 1;
+    }
+    n
+}
+
+const fn labeled_break() -> i32 {
+    let mut i = 0;
+    'outer: loop {
+        loop {
+            i += 1;
+            if i == 3 {
+                break 'outer;
+            }
+        }
+    }
+    i
+}
+
+const fn labeled_continue() -> i32 {
+    let mut i = 0;
+    let mut n = 0;
+    'outer: while i < 5 {
+        i += 1;
+        let mut j = 0;
+        while j < 5 {
+            j += 1;
+            if j == 2 {
+                continue 'outer;
+            }
+            n += 1;
+        }
+    }
+    n
+}
+
+const A: i32 = loop_continue();
+const B: i32 = labeled_break();
+const C: i32 = labeled_continue();
+
+fn main() -> i32 {
+    if A != 4 {
+        return 1;
+    }
+    if B != 3 {
+        return 2;
+    }
+    if C != 5 {
+        return 3;
+    }
+    0
+}
-- 
2.56.1

Reply via email to