Debian Codegraph @

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

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