-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CFI: Encode Virtual calls as calls through the defining trait
For example, if `trait Foo: Bar`, and we try to call a method from `Bar` on `dyn Foo`, encode the callsite as passing a `dyn Bar`, not a `dyn Foo`.
- Loading branch information
Showing
2 changed files
with
85 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#![feature(trait_upcasting)] | ||
// Check that super-traits are callable. | ||
|
||
//@ needs-sanitizer-cfi | ||
// FIXME(#122848) Remove only-linux once OSX CFI binaries work | ||
//@ only-linux | ||
//@ compile-flags: --crate-type=bin -Cprefer-dynamic=off -Clto -Zsanitizer=cfi | ||
//@ compile-flags: -C target-feature=-crt-static -C codegen-units=1 -C opt-level=0 | ||
//@ run-pass | ||
|
||
trait Parent1 { | ||
type P1; | ||
fn p1(&self) -> Self::P1; | ||
} | ||
|
||
trait Parent2 { | ||
type P2; | ||
fn p2(&self) -> Self::P2; | ||
} | ||
|
||
trait Child : Parent1 + Parent2 { | ||
type C; | ||
fn c(&self) -> Self::C; | ||
} | ||
|
||
struct Foo; | ||
|
||
impl Parent1 for Foo { | ||
type P1 = u16; | ||
fn p1(&self) -> Self::P1 { | ||
println!("p1"); | ||
1 | ||
} | ||
} | ||
|
||
impl Parent2 for Foo { | ||
type P2 = u32; | ||
fn p2(&self) -> Self::P2 { | ||
println!("p2"); | ||
2 | ||
} | ||
} | ||
|
||
impl Child for Foo { | ||
type C = u8; | ||
fn c(&self) -> Self::C { | ||
println!("c"); | ||
0 | ||
} | ||
} | ||
|
||
fn main() { | ||
// Child can access its own methods and super methods. | ||
let x = &Foo as &dyn Child<C=u8,P1=u16,P2=u32>; | ||
x.c(); | ||
x.p1(); | ||
x.p2(); | ||
// Parents can be created and access their methods. | ||
let y = &Foo as &dyn Parent1<P1=u16>; | ||
y.p1(); | ||
let z = &Foo as &dyn Parent2<P2=u32>; | ||
z.p2(); | ||
// Trait upcasting works | ||
let x1 = x as &dyn Parent1<P1=u16>; | ||
x1.p1(); | ||
let x2 = x as &dyn Parent2<P2=u32>; | ||
x2.p2(); | ||
} |