From: Utkarsh Bahuguna <[email protected]>

A let with a wildcard, tuple or struct pattern in a const fn used a
temporary that isn't in any block, so const eval lost track of it and
silently left the constant uninitialized.  Declare it in the enclosing
block instead.

Fixes Rust-GCC/gccrs#5009

Assisted-by: Claude Opus 5.5 (Anthropic)

gcc/rust/ChangeLog:

        * rust-backend.h (temporary_variable): Say that a bind_tree scopes
        the variable.
        * rust-gcc.cc (temporary_variable): Declare the variable in
        bind_tree whenever one is given.
        (array_initializer): Keep the array temporary function-wide, it is
        read after the block.
        * backend/rust-compile-pattern.cc
        (CompilePatternLet::visit (WildcardPattern)): Pass the enclosing
        scope.
        (CompilePatternLet::visit (TuplePattern)): Likewise.
        (CompilePatternLet::visit (StructPattern)): Likewise.

gcc/testsuite/ChangeLog:

        * rust/compile/const-fn-let-pattern.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/828828a90de4f76cc2faa433f188fbf0d2799c9b

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

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

 gcc/rust/backend/rust-compile-pattern.cc      | 13 ++++---
 gcc/rust/rust-backend.h                       |  4 ++-
 gcc/rust/rust-gcc.cc                          | 12 ++++---
 .../rust/compile/const-fn-let-pattern.rs      | 36 +++++++++++++++++++
 4 files changed, 54 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/const-fn-let-pattern.rs

diff --git a/gcc/rust/backend/rust-compile-pattern.cc 
b/gcc/rust/backend/rust-compile-pattern.cc
index 8ba8b300b..d0e6e38ef 100644
--- a/gcc/rust/backend/rust-compile-pattern.cc
+++ b/gcc/rust/backend/rust-compile-pattern.cc
@@ -1418,7 +1418,8 @@ CompilePatternLet::visit (HIR::WildcardPattern &pattern)
   tree init_stmt = NULL;
   tree stmt_type = TyTyResolveCompile::compile (ctx, ty);
 
-  Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE, stmt_type,
+  Backend::temporary_variable (ctx->peek_fn ().fndecl,
+                              ctx->peek_enclosing_scope (), stmt_type,
                               init_expr, false, pattern.get_locus (),
                               &init_stmt);
 
@@ -1476,9 +1477,10 @@ CompilePatternLet::visit (HIR::TuplePattern &pattern)
   tree rhs_tuple_type = TYPE_MAIN_VARIANT (TREE_TYPE (init_expr));
   tree init_stmt;
   Bvariable *tmp_var
-    = Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE,
-                                  rhs_tuple_type, init_expr, has_by_ref,
-                                  pattern.get_locus (), &init_stmt);
+    = Backend::temporary_variable (ctx->peek_fn ().fndecl,
+                                  ctx->peek_enclosing_scope (), rhs_tuple_type,
+                                  init_expr, has_by_ref, pattern.get_locus (),
+                                  &init_stmt);
   tree access_expr = Backend::var_expression (tmp_var, pattern.get_locus ());
   ctx->add_statement (init_stmt);
 
@@ -1609,7 +1611,8 @@ CompilePatternLet::visit (HIR::StructPattern &pattern)
   tree rhs_type = TYPE_MAIN_VARIANT (TREE_TYPE (init_expr));
   tree init_stmt;
   Bvariable *tmp_var
-    = Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE, rhs_type,
+    = Backend::temporary_variable (ctx->peek_fn ().fndecl,
+                                  ctx->peek_enclosing_scope (), rhs_type,
                                   init_expr, has_by_ref, pattern.get_locus (),
                                   &init_stmt);
   ctx->add_statement (init_stmt);
diff --git a/gcc/rust/rust-backend.h b/gcc/rust/rust-backend.h
index e3013cbb3..2a26f65e1 100644
--- a/gcc/rust/rust-backend.h
+++ b/gcc/rust/rust-backend.h
@@ -372,7 +372,9 @@ LocalVariable static_chain_variable (tree function, 
GGC::Ident name, tree type,
 // the statement or expression which requires creating the temporary
 // variable, and may not be very useful.  This function should
 // return a variable which can be referenced later and should set
-// *PSTATEMENT to a statement which initializes the variable.
+// *PSTATEMENT to a statement which initializes the variable.  With a
+// BIND_TREE the variable is declared in it like a named local, without
+// one it is a function-wide temporary.
 LocalVariable temporary_variable (tree fndecl, tree bind_tree, tree type,
                                  tree init, bool address_is_taken,
                                  location_t location, tree *pstatement);
diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc
index 00df8b3b9..e02b674a4 100644
--- a/gcc/rust/rust-gcc.cc
+++ b/gcc/rust/rust-gcc.cc
@@ -1525,9 +1525,10 @@ array_initializer (tree fndecl, tree block, tree 
array_type, tree length,
 {
   std::vector<tree> stmts;
 
-  // Temporary array we initialize with the desired value.
+  // Temporary array we initialize with the desired value.  It is read
+  // after the block ends, so it cannot live in the block.
   tree t = NULL_TREE;
-  Bvariable *tmp_array = temporary_variable (fndecl, block, array_type,
+  Bvariable *tmp_array = temporary_variable (fndecl, NULL_TREE, array_type,
                                             NULL_TREE, true, locus, &t);
   tree arr = tmp_array->get_tree (locus);
   stmts.push_back (t);
@@ -2243,9 +2244,11 @@ temporary_variable (tree fndecl, tree bind_tree, tree 
type_tree, tree init_tree,
     }
 
   tree var;
-  // We can only use create_tmp_var if the type is not addressable.
-  if (!TREE_ADDRESSABLE (type_tree))
+  // Without a scope to live in the temporary is function-wide, which
+  // create_tmp_var can only do for a type that is not addressable.
+  if (bind_tree == NULL_TREE)
     {
+      gcc_assert (!TREE_ADDRESSABLE (type_tree));
       if (DECL_STRUCT_FUNCTION (fndecl) == NULL)
        push_struct_function (fndecl);
       else
@@ -2256,7 +2259,6 @@ temporary_variable (tree fndecl, tree bind_tree, tree 
type_tree, tree init_tree,
     }
   else
     {
-      gcc_assert (bind_tree != NULL_TREE);
       var = build_decl (location, VAR_DECL, create_tmp_var_name ("RUSTTMP"),
                        type_tree);
       DECL_ARTIFICIAL (var) = 1;
diff --git a/gcc/testsuite/rust/compile/const-fn-let-pattern.rs 
b/gcc/testsuite/rust/compile/const-fn-let-pattern.rs
new file mode 100644
index 000000000..471b3b24c
--- /dev/null
+++ b/gcc/testsuite/rust/compile/const-fn-let-pattern.rs
@@ -0,0 +1,36 @@
+// items only used in constants are reported as unused
+// { dg-additional-options "-w" }
+#![feature(no_core, lang_items)]
+#![no_core]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+struct S {
+    a: usize,
+    b: usize,
+}
+
+const fn wildcard() -> usize {
+    let s = 4;
+    let _ = s;
+    s
+}
+
+const fn tuple() -> usize {
+    let t = (3, 4);
+    let (a, b) = t;
+    a + b
+}
+
+const fn record() -> usize {
+    let s = S { a: 5, b: 6 };
+    let S { a, b } = s;
+    a + b
+}
+
+pub fn check() {
+    let _w: [u8; wildcard()] = [0; 4];
+    let _t: [u8; tuple()] = [0; 7];
+    let _r: [u8; record()] = [0; 11];
+}

base-commit: 14b9eaa7a5c1b0c614adb86729d867318a1925c7
-- 
2.56.1

Reply via email to