Debian Codegraph @

gcc/testsuite/rust/execute/torture/operator_overload_9.rs

raw
1 /* { dg-output "mut_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 fn main() -> i32 {
49 let foo = &mut 123;
50 let res: i32 = *foo;
51
52 unsafe {
53 let a = "%i\n\0";
54 let b = a as *const str;
55 let c = b as *const i8;
56
57 printf(c, res);
58 }
59
60 0
61 }