aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-10-05 17:24:42 +0100
committerPhilip Herron <philip.herron@embecosm.com>2022-10-06 14:07:17 +0100
commita7d2643d9b09af9f5c5c670626becaa0c0fc1481 (patch)
treecf7011c7659af1416f4a109f81bc264f4686ecef
parente033f1705dde104c44d69cec87cb728dba596c6f (diff)
downloadgcc-a7d2643d9b09af9f5c5c670626becaa0c0fc1481.zip
gcc-a7d2643d9b09af9f5c5c670626becaa0c0fc1481.tar.gz
gcc-a7d2643d9b09af9f5c5c670626becaa0c0fc1481.tar.bz2
Support looking up super traits for trait items
When supporting calls to super traits we need to allow lookups based on the super traits as specified on the TraitReferences. Fixes #1555
-rw-r--r--gcc/rust/typecheck/rust-hir-trait-ref.h19
-rw-r--r--gcc/testsuite/rust/compile/torture/issue-1555.rs48
2 files changed, 67 insertions, 0 deletions
diff --git a/gcc/rust/typecheck/rust-hir-trait-ref.h b/gcc/rust/typecheck/rust-hir-trait-ref.h
index 6eec461..0f4883d 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 0000000..adb4891
--- /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);
+}