Debian Codegraph @

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

raw
1 // { dg-additional-options "-w" }
2 // { dg-output "mut_deref\r*\nfoobar: 123\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 #[lang = "deref_mut"]
18 pub trait DerefMut: Deref {
19 fn deref_mut(&mut self) -> &mut Self::Target;
20 }
21
22 impl<T> Deref for &T {
23 type Target = T;
24
25 fn deref(&self) -> &T {
26 *self
27 }
28 }
29
30 impl<T> Deref for &mut T {
31 type Target = T;
32 fn deref(&self) -> &T {
33 *self
34 }
35 }
36
37 pub struct Bar(i32);
38 impl Bar {
39 pub fn foobar(&mut self) -> i32 {
40 self.0
41 }
42 }
43
44 pub struct Foo<T>(T);
45 impl<T> Deref for Foo<T> {
46 type Target = T;
47
48 fn deref(&self) -> &Self::Target {
49 &self.0
50 }
51 }
52
53 impl<T> DerefMut for Foo<T> {
54 fn deref_mut(&mut self) -> &mut Self::Target {
55 unsafe {
56 let a = "mut_deref\n\0";
57 let b = a as *const str;
58 let c = b as *const i8;
59
60 printf(c);
61 }
62
63 &mut self.0
64 }
65 }
66
67 pub fn main() -> i32 {
68 let mut bar = Bar(123);
69 let mut foo: Foo<&mut Bar> = Foo(&mut bar);
70 let foobar = foo.foobar();
71
72 unsafe {
73 let a = "foobar: %i\n\0";
74 let b = a as *const str;
75 let c = b as *const i8;
76
77 printf(c, foobar);
78 }
79
80 foobar - 123
81 }