public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Fix ICE in generic subsitution of enums containing dataless variants
@ 2022-06-08 11:58 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 11:58 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:5b369a61484c52ea0298cfab11858c3fff8bcc00

commit 5b369a61484c52ea0298cfab11858c3fff8bcc00
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Tue Jan 4 16:15:26 2022 +0000

    Fix ICE in generic subsitution of enums containing dataless variants
    
    Dataless variants do not contain fields that can be substituted, which then
    hits an assertion on access of the fields for the variant. This patch adds
    a guard against the substitution of dataless variants. We could have
    removed the assertion which would have also been a good fix but keeping the
    assertion for now is very helpful in debugging issues.
    
    Fixs #851

Diff:
---
 gcc/rust/typecheck/rust-tyty.cc                 |  3 +++
 gcc/rust/typecheck/rust-tyty.h                  |  2 ++
 gcc/testsuite/rust/execute/torture/issue-851.rs | 35 +++++++++++++++++++++++++
 3 files changed, 40 insertions(+)

diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index 25fdfa9db79..f141a4118c8 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -822,6 +822,9 @@ ADTType::handle_substitions (SubstitutionArgumentMappings subst_mappings)
 
   for (auto &variant : adt->get_variants ())
     {
+      if (variant->is_dataless_variant ())
+	continue;
+
       for (auto &field : variant->get_fields ())
 	{
 	  bool ok = ::Rust::TyTy::handle_substitions (subst_mappings, field);
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index 012e8464ee2..40c06a57783 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -1069,6 +1069,8 @@ public:
   HirId get_id () const { return id; }
 
   VariantType get_variant_type () const { return type; }
+  bool is_data_variant () const { return type != VariantType::NUM; }
+  bool is_dataless_variant () const { return type == VariantType::NUM; }
 
   std::string get_identifier () const { return identifier; }
   int get_discriminant () const { return discriminant; }
diff --git a/gcc/testsuite/rust/execute/torture/issue-851.rs b/gcc/testsuite/rust/execute/torture/issue-851.rs
new file mode 100644
index 00000000000..3881c7a2ada
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/issue-851.rs
@@ -0,0 +1,35 @@
+/* { dg-output "Result: 123\n" } */
+extern "C" {
+    fn printf(s: *const i8, ...);
+}
+
+enum Foo<T> {
+    A,
+    B(T),
+}
+
+fn inspect(a: Foo<i32>) {
+    match a {
+        Foo::A => unsafe {
+            let a = "A\n\0";
+            let b = a as *const str;
+            let c = b as *const i8;
+
+            printf(c);
+        },
+        Foo::B(x) => unsafe {
+            let a = "Result: %i\n\0";
+            let b = a as *const str;
+            let c = b as *const i8;
+
+            printf(c, x);
+        },
+    }
+}
+
+fn main() -> i32 {
+    let a = Foo::B(123);
+    inspect(a);
+
+    0
+}


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

only message in thread, other threads:[~2022-06-08 11:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 11:58 [gcc/devel/rust/master] Fix ICE in generic subsitution of enums containing dataless variants 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).