]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix bad type canonicalization on ARRAY_TYPES
authorPhilip Herron <herron.philip@googlemail.com>
Tue, 27 May 2025 12:47:04 +0000 (13:47 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:46 +0000 (16:36 +0200)
Fixes Rust-GCC#3660

gcc/rust/ChangeLog:

* backend/rust-compile-type.cc (TyTyResolveCompile::visit): reuse GCC's build_array_type

gcc/testsuite/ChangeLog:

* rust/compile/const_generics_3.rs:
* rust/compile/issue-3660.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/backend/rust-compile-type.cc
gcc/testsuite/rust/compile/const_generics_3.rs
gcc/testsuite/rust/compile/issue-3660.rs [new file with mode: 0644]

index 00b21fbbf952f983cd1557ec596093d70e43bbdd..7e56a0f4e1be6f9f52b252de30a6341902ab0a68 100644 (file)
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-compile-type.h"
-#include "rust-compile-expr.h"
 #include "rust-constexpr.h"
-#include "rust-gcc.h"
+#include "rust-compile-base.h"
 
 #include "tree.h"
+#include "fold-const.h"
 #include "stor-layout.h"
 
 namespace Rust {
@@ -480,9 +480,12 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
 
   tree folded_capacity_expr = fold_expr (capacity_expr);
 
-  translated = Backend::array_type (element_type, folded_capacity_expr);
-  if (translated != error_mark_node)
-    translated = ctx->insert_compiled_type (translated);
+  // build_index_type takes the maximum index, which is one less than
+  // the length.
+  tree index_type_tree = build_index_type (
+    fold_build2 (MINUS_EXPR, sizetype, folded_capacity_expr, size_one_node));
+
+  translated = build_array_type (element_type, index_type_tree, false);
 }
 
 void
index 524d48d5bcf5a32aa9c015c0c75e12953d0ef76b..bd9172949846651057563f3d6d2ab17fb9ef780b 100644 (file)
@@ -1,4 +1,4 @@
-// { dg-additional-options "-w -frust-name-resolution-2.0" }
+// { dg-additional-options "-w -frust-name-resolution-2.0 -frust-compile-until=compilation" }
 
 #[lang = "sized"]
 trait Sized {}
diff --git a/gcc/testsuite/rust/compile/issue-3660.rs b/gcc/testsuite/rust/compile/issue-3660.rs
new file mode 100644 (file)
index 0000000..1f1c583
--- /dev/null
@@ -0,0 +1,3 @@
+pub static A: [u32; 2] = [1, 2];
+
+pub static B: [u8; 2] = [3, 4];