[committed,011/103] gccrs: Support looking up super traits for trait items

Message ID 20230221120230.596966-12-arthur.cohen@embecosm.com
State Unresolved
Headers
Series [committed,001/103] gccrs: Fix missing dead code analysis ICE on local enum definition |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Arthur Cohen Feb. 21, 2023, 12:01 p.m. UTC
  From: Philip Herron <philip.herron@embecosm.com>

When supporting calls to super traits we need to allow lookups based on
the super traits as specified on the TraitReferences.

Fixes #1555

gcc/rust/ChangeLog:

	* typecheck/rust-hir-trait-ref.h (lookup_trait_item): Add lookup
	in super_trait.

gcc/testsuite/ChangeLog:

	* rust/compile/torture/issue-1555.rs: New test.
---
 gcc/rust/typecheck/rust-hir-trait-ref.h       | 19 ++++++++
 .../rust/compile/torture/issue-1555.rs        | 48 +++++++++++++++++++
 2 files changed, 67 insertions(+)
 create mode 100644 gcc/testsuite/rust/compile/torture/issue-1555.rs
  

Patch

diff --git a/gcc/rust/typecheck/rust-hir-trait-ref.h b/gcc/rust/typecheck/rust-hir-trait-ref.h
index f6a3328c5f6..7eeb3300387 100644
--- a/gcc/rust/typecheck/rust-hir-trait-ref.h
+++ b/gcc/rust/typecheck/rust-hir-trait-ref.h
@@ -336,6 +336,15 @@  public:
 	    return true;
 	  }
       }
+
+    // lookup super traits
+    for (const auto &super_trait : super_traits)
+      {
+	bool found = super_trait->lookup_trait_item (ident, ref);
+	if (found)
+	  return true;
+      }
+
     return false;
   }
 
@@ -351,6 +360,16 @@  public:
 	if (ident.compare (item.get_identifier ()) == 0)
 	  return &item;
       }
+
+    // lookup super traits
+    for (const auto &super_trait : super_traits)
+      {
+	const TraitItemReference *res
+	  = super_trait->lookup_trait_item (ident, type);
+	if (!res->is_error ())
+	  return res;
+      }
+
     return &TraitItemReference::error_node ();
   }
 
diff --git a/gcc/testsuite/rust/compile/torture/issue-1555.rs b/gcc/testsuite/rust/compile/torture/issue-1555.rs
new file mode 100644
index 00000000000..adb48911648
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/issue-1555.rs
@@ -0,0 +1,48 @@ 
+extern "C" {
+    fn printf(s: *const i8, ...);
+}
+
+struct Foo(i32);
+trait Bar {
+    fn baz(&self);
+}
+
+trait Baz: Bar {
+    fn qux(&self);
+}
+
+impl Bar for Foo {
+    fn baz(&self) {
+        unsafe {
+            let a = "baz %i\n\0";
+            let b = a as *const str;
+            let c = b as *const i8;
+
+            printf(c, self.0);
+        }
+    }
+}
+
+impl Baz for Foo {
+    fn qux(&self) {
+        unsafe {
+            let a = "qux %i\n\0";
+            let b = a as *const str;
+            let c = b as *const i8;
+
+            printf(c, self.0);
+        }
+    }
+}
+
+fn static_dispatch<T: Baz>(t: &T) {
+    t.baz();
+    t.qux();
+}
+
+pub fn main() {
+    let a;
+    a = &Foo(123);
+
+    static_dispatch(a);
+}