| 1 |
/* { dg-output "foo_deref\r*\nimm_deref\r*\n123\r*\n" } */ |
| 2 |
extern "C" { |
| 3 |
fn printf(s: *const i8, ...); |
| 4 |
} |
| 5 |
|
| 6 |
#[lang = "sized"] |
| 7 |
pub trait Sized {} |
| 8 |
|
| 9 |
#[lang = "deref"] |
| 10 |
pub trait Deref { |
| 11 |
type Target; |
| 12 |
|
| 13 |
fn deref(&self) -> &Self::Target; |
| 14 |
} |
| 15 |
|
| 16 |
impl<T> Deref for &T { |
| 17 |
type Target = T; |
| 18 |
|
| 19 |
fn deref(&self) -> &T { |
| 20 |
unsafe { |
| 21 |
let a = "imm_deref\n\0"; |
| 22 |
let b = a as *const str; |
| 23 |
let c = b as *const i8; |
| 24 |
|
| 25 |
printf(c); |
| 26 |
} |
| 27 |
|
| 28 |
*self |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
impl<T> Deref for &mut T { |
| 33 |
type Target = T; |
| 34 |
|
| 35 |
fn deref(&self) -> &T { |
| 36 |
unsafe { |
| 37 |
let a = "mut_deref\n\0"; |
| 38 |
let b = a as *const str; |
| 39 |
let c = b as *const i8; |
| 40 |
|
| 41 |
printf(c); |
| 42 |
} |
| 43 |
|
| 44 |
*self |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
struct Foo<T>(T); |
| 49 |
impl<T> Deref for Foo<T> { |
| 50 |
type Target = T; |
| 51 |
|
| 52 |
fn deref(&self) -> &Self::Target { |
| 53 |
unsafe { |
| 54 |
let a = "foo_deref\n\0"; |
| 55 |
let b = a as *const str; |
| 56 |
let c = b as *const i8; |
| 57 |
|
| 58 |
printf(c); |
| 59 |
} |
| 60 |
|
| 61 |
&self.0 |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
fn main() -> i32 { |
| 66 |
let foo: Foo<i32> = Foo(123); |
| 67 |
let bar: &i32 = &foo; |
| 68 |
|
| 69 |
unsafe { |
| 70 |
let a = "%i\n\0"; |
| 71 |
let b = a as *const str; |
| 72 |
let c = b as *const i8; |
| 73 |
|
| 74 |
printf(c, *bar); |
| 75 |
} |
| 76 |
|
| 77 |
0 |
| 78 |
} |