-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic dispatch with supertraits #3124
Conversation
f95ecd6
to
7e33f40
Compare
NOTE: I found a bug with the following code sample: extern "C" {
fn printf(s: *const i8, ...);
}
struct Foo {
my_int: u32,
}
trait Parent {
fn parent(&self) -> bool;
}
trait Child : Parent {
fn child(&self);
}
impl Parent for Foo {
fn parent(&self) -> bool {
// Call supertrait method
return true;
}
}
impl Child for Foo {
fn child(&self) {
let _ = self;
}
}
pub fn main() {
let a = Foo{ my_int: 0xf00dfeed};
let b: &dyn Child = &a;
let c: &dyn Parent = b;
c.parent();
} I think this particular case might be best handled in a separate issue. Gating casts from dyn Child to dyn Parent seems like it will be quite involved. Also, out of scope of the original issue. For context, the problem here is that you have to take Child's vtable, and prune it to match Parent's vtable. Rustc currently doesn't let you do this, however, they seem somewhat close-ish to stabilizing this conversion. |
6991386
to
313bc53
Compare
313bc53
to
1e437bc
Compare
27b9be7
to
52e6831
Compare
gcc/rust/ChangeLog: * backend/rust-compile.cc: Modify compute_address_for_trait_item to support supertraits * typecheck/rust-tyty.cc: Remove auto gcc/testsuite/ChangeLog: * rust/compile/trait13.rs: Add test for supertraits of supertraits * rust/compile/trait14.rs: Diamond problem with supertraits test * rust/execute/torture/trait14.rs: Add test for dynamic dispatch with supertraits * rust/execute/torture/trait15.rs: Add test for dynamic dispatch with generics * rust/execute/torture/trait16.rs: Add test for dynamic dispatch with lifetime params 1 * rust/execute/torture/trait17.rs: Add test for dynamic dispatch with lifetime params 2 * rust/execute/torture/trait18.rs: Add test for default implementations with dynamic dispatch and supertraits Signed-off-by: Liam Naddell <liam.naddell@mail.utoronto.ca>
5381ca5
to
178ab55
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, great work. I like all those new tests.
There was only one function that needed modification, the rest is testing for this new feature :)
Fixes #914
make check-rust
passes locallyclang-format
gcc/testsuite/rust/
Modifies compute_address_for_trait_item to support supertraits. The algorithm works by looping over receiver_bounds for all relevant traits, and attempting to find one that matches the type, and contains the correct function implementation.