]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/tests/run/mut_ref.rs
Remove unnecessary .as_ref().
[rust.git] / compiler / rustc_codegen_gcc / tests / run / mut_ref.rs
1
2 // Compiler:
3 //
4 // Run-time:
5 //   stdout: 2
6 //     7
7 //     6
8 //     11
9
10 #![allow(unused_attributes)]
11 #![feature(auto_traits, lang_items, no_core, start, intrinsics, track_caller)]
12
13 #![no_std]
14 #![no_core]
15
16 /*
17  * Core
18  */
19
20 // Because we don't have core yet.
21 #[lang = "sized"]
22 pub trait Sized {}
23
24 #[lang = "copy"]
25 trait Copy {
26 }
27
28 impl Copy for isize {}
29 impl Copy for *mut i32 {}
30 impl Copy for usize {}
31 impl Copy for u8 {}
32 impl Copy for i8 {}
33 impl Copy for i32 {}
34
35 #[lang = "receiver"]
36 trait Receiver {
37 }
38
39 #[lang = "freeze"]
40 pub(crate) unsafe auto trait Freeze {}
41
42 #[lang = "panic_location"]
43 struct PanicLocation {
44     file: &'static str,
45     line: u32,
46     column: u32,
47 }
48
49 mod libc {
50     #[link(name = "c")]
51     extern "C" {
52         pub fn puts(s: *const u8) -> i32;
53         pub fn fflush(stream: *mut i32) -> i32;
54         pub fn printf(format: *const i8, ...) -> i32;
55
56         pub static STDOUT: *mut i32;
57     }
58 }
59
60 mod intrinsics {
61     extern "rust-intrinsic" {
62         pub fn abort() -> !;
63     }
64 }
65
66 #[lang = "panic"]
67 #[track_caller]
68 #[no_mangle]
69 pub fn panic(_msg: &str) -> ! {
70     unsafe {
71         libc::puts("Panicking\0" as *const str as *const u8);
72         libc::fflush(libc::STDOUT);
73         intrinsics::abort();
74     }
75 }
76
77 #[lang = "add"]
78 trait Add<RHS = Self> {
79     type Output;
80
81     fn add(self, rhs: RHS) -> Self::Output;
82 }
83
84 impl Add for u8 {
85     type Output = Self;
86
87     fn add(self, rhs: Self) -> Self {
88         self + rhs
89     }
90 }
91
92 impl Add for i8 {
93     type Output = Self;
94
95     fn add(self, rhs: Self) -> Self {
96         self + rhs
97     }
98 }
99
100 impl Add for i32 {
101     type Output = Self;
102
103     fn add(self, rhs: Self) -> Self {
104         self + rhs
105     }
106 }
107
108 impl Add for usize {
109     type Output = Self;
110
111     fn add(self, rhs: Self) -> Self {
112         self + rhs
113     }
114 }
115
116 impl Add for isize {
117     type Output = Self;
118
119     fn add(self, rhs: Self) -> Self {
120         self + rhs
121     }
122 }
123
124 /*
125  * Code
126  */
127
128 struct Test {
129     field: isize,
130 }
131
132 fn test(num: isize) -> Test {
133     Test {
134         field: num + 1,
135     }
136 }
137
138 fn update_num(num: &mut isize) {
139     *num = *num + 5;
140 }
141
142 #[start]
143 fn main(mut argc: isize, _argv: *const *const u8) -> isize {
144     let mut test = test(argc);
145     unsafe {
146         libc::printf(b"%ld\n\0" as *const u8 as *const i8, test.field);
147     }
148     update_num(&mut test.field);
149     unsafe {
150         libc::printf(b"%ld\n\0" as *const u8 as *const i8, test.field);
151     }
152
153     update_num(&mut argc);
154     unsafe {
155         libc::printf(b"%ld\n\0" as *const u8 as *const i8, argc);
156     }
157
158     let refe = &mut argc;
159     *refe = *refe + 5;
160     unsafe {
161         libc::printf(b"%ld\n\0" as *const u8 as *const i8, argc);
162     }
163
164     0
165 }