Auto merge of #123071 - rcvalle:rust-cfi-fix-method-fn-ptr-cast, r=compiler-errors

CFI: Fix methods as function pointer cast

Fix casting between methods and function pointers by assigning a secondary type id to methods with their concrete self so they can be used as function pointers.

This was split off from #116404.

cc `@compiler-errors` `@workingjubilee`
This commit is contained in:
bors 2024-03-29 09:04:05 +00:00
commit 58dcd1fdb9
5 changed files with 84 additions and 27 deletions

View file

@ -0,0 +1,23 @@
// Verifies that casting a method to a function pointer works.
//
// FIXME(#122848): Remove only-linux when fixed.
//@ only-linux
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Copt-level=0 -Cprefer-dynamic=off -Ctarget-feature=-crt-static -Zsanitizer=cfi
//@ run-pass
trait Trait1 {
fn foo(&self);
}
struct Type1;
impl Trait1 for Type1 {
fn foo(&self) {}
}
fn main() {
let type1 = Type1 {};
let f = <Type1 as Trait1>::foo;
f(&type1);
}