public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] gccrs: Fix ICE using constructors for intilizers in statics
@ 2023-04-06 21:36 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-06 21:36 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:b99448f4b02957c71ceb1adc3fb8cfa2a7602ab9

commit b99448f4b02957c71ceb1adc3fb8cfa2a7602ab9
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Sun Apr 2 21:59:50 2023 +0100

    gccrs: Fix ICE using constructors for intilizers in statics
    
    We are getting constant expressions for the initilizers for static items
    this hits an assertion in the GCC middle-end which is looking for a
    constructor so we need to unwrap the constant expression using DECL_INITIAL
    as the initilizer to the global static.
    
    Fixes #2080
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-item.cc (CompileItem::visit): unwrap the constant expression
    
    gcc/testsuite/ChangeLog:
    
            * rust/execute/torture/issue-2080.rs: New test.
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>

Diff:
---
 gcc/rust/backend/rust-compile-item.cc            |  6 +++---
 gcc/testsuite/rust/execute/torture/issue-2080.rs | 26 ++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-item.cc b/gcc/rust/backend/rust-compile-item.cc
index b2e9b3fbf6d..e1e2bb6d5a0 100644
--- a/gcc/rust/backend/rust-compile-item.cc
+++ b/gcc/rust/backend/rust-compile-item.cc
@@ -18,9 +18,7 @@
 
 #include "rust-compile-item.h"
 #include "rust-compile-implitem.h"
-#include "rust-compile-expr.h"
 #include "rust-compile-extern.h"
-#include "rust-constexpr.h"
 
 namespace Rust {
 namespace Compile {
@@ -66,7 +64,9 @@ CompileItem::visit (HIR::StaticItem &var)
     = ctx->get_backend ()->global_variable (name, asm_name, type, is_external,
 					    is_hidden, in_unique_section,
 					    var.get_locus ());
-  ctx->get_backend ()->global_variable_set_init (static_global, value);
+
+  tree init = value == error_mark_node ? error_mark_node : DECL_INITIAL (value);
+  ctx->get_backend ()->global_variable_set_init (static_global, init);
 
   ctx->insert_var_decl (var.get_mappings ().get_hirid (), static_global);
   ctx->push_var (static_global);
diff --git a/gcc/testsuite/rust/execute/torture/issue-2080.rs b/gcc/testsuite/rust/execute/torture/issue-2080.rs
new file mode 100644
index 00000000000..dbdbf41ff92
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/issue-2080.rs
@@ -0,0 +1,26 @@
+// { dg-output "hello world: gccrs\n" }
+// { dg-additional-options "-w" }
+static TEST_1: &str = "gccrs";
+static TEST_2: i32 = 123;
+
+struct Foo(i32, bool);
+static TEST_3: Foo = Foo(123, false);
+
+extern "C" {
+    fn printf(s: *const i8, ...);
+}
+
+fn main() -> i32 {
+    unsafe {
+        let a1 = "hello world: %s\n";
+        let b1 = a1 as *const str;
+        let c1 = b1 as *const i8;
+
+        let a2 = TEST_1;
+        let b2 = a2 as *const str;
+        let c2 = b2 as *const i8;
+
+        printf(c1, c2);
+    }
+    0
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-06 21:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 21:36 [gcc/devel/rust/master] gccrs: Fix ICE using constructors for intilizers in statics Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).