]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/tests/run/int_overflow.rs
Rollup merge of #103386 - compiler-errors:no-coerceunsized-to-dynstar, r=eholk
[rust.git] / compiler / rustc_codegen_gcc / tests / run / int_overflow.rs
1 // Compiler:
2 //
3 // Run-time:
4 //   stdout: Success
5 //   status: signal
6
7 #![allow(unused_attributes)]
8 #![feature(auto_traits, lang_items, no_core, start, intrinsics)]
9
10 #![no_std]
11 #![no_core]
12
13 /*
14  * Core
15  */
16
17 // Because we don't have core yet.
18 #[lang = "sized"]
19 pub trait Sized {}
20
21 #[lang = "copy"]
22 trait Copy {
23 }
24
25 impl Copy for isize {}
26 impl Copy for *mut i32 {}
27 impl Copy for usize {}
28 impl Copy for i32 {}
29 impl Copy for u8 {}
30 impl Copy for i8 {}
31
32 #[lang = "receiver"]
33 trait Receiver {
34 }
35
36 #[lang = "freeze"]
37 pub(crate) unsafe auto trait Freeze {}
38
39 #[lang = "panic_location"]
40 struct PanicLocation {
41     file: &'static str,
42     line: u32,
43     column: u32,
44 }
45
46 mod libc {
47     #[link(name = "c")]
48     extern "C" {
49         pub fn puts(s: *const u8) -> i32;
50         pub fn fflush(stream: *mut i32) -> i32;
51
52         pub static stdout: *mut i32;
53     }
54 }
55
56 mod intrinsics {
57     extern "rust-intrinsic" {
58         pub fn abort() -> !;
59     }
60 }
61
62 #[lang = "panic"]
63 #[track_caller]
64 #[no_mangle]
65 pub fn panic(_msg: &str) -> ! {
66     unsafe {
67         // Panicking is expected iff overflow checking is enabled.
68         #[cfg(debug_assertions)]
69         libc::puts("Success\0" as *const str as *const u8);
70         libc::fflush(libc::stdout);
71         intrinsics::abort();
72     }
73 }
74
75 #[lang = "add"]
76 trait Add<RHS = Self> {
77     type Output;
78
79     fn add(self, rhs: RHS) -> Self::Output;
80 }
81
82 impl Add for u8 {
83     type Output = Self;
84
85     fn add(self, rhs: Self) -> Self {
86         self + rhs
87     }
88 }
89
90 impl Add for i8 {
91     type Output = Self;
92
93     fn add(self, rhs: Self) -> Self {
94         self + rhs
95     }
96 }
97
98 impl Add for i32 {
99     type Output = Self;
100
101     fn add(self, rhs: Self) -> Self {
102         self + rhs
103     }
104 }
105
106 impl Add for usize {
107     type Output = Self;
108
109     fn add(self, rhs: Self) -> Self {
110         self + rhs
111     }
112 }
113
114 impl Add for isize {
115     type Output = Self;
116
117     fn add(self, rhs: Self) -> Self {
118         self + rhs
119     }
120 }
121
122 /*
123  * Code
124  */
125
126 #[start]
127 fn main(mut argc: isize, _argv: *const *const u8) -> isize {
128     let int = 9223372036854775807isize;
129     let int = int + argc;  // overflow
130
131     // If overflow checking is disabled, we should reach here.
132     #[cfg(not(debug_assertions))]
133     unsafe {
134         libc::puts("Success\0" as *const str as *const u8);
135         libc::fflush(libc::stdout);
136         intrinsics::abort();
137     }
138
139     int
140 }