]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.rust/simple.rs
65b57f42df6a7cc38c597875b63cd854dd4193a9
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.rust / simple.rs
1 // Copyright (C) 2016-2019 Free Software Foundation, Inc.
2
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 #![allow(dead_code)]
17 #![allow(unused_variables)]
18 #![allow(unused_assignments)]
19
20
21 pub struct HiBob {
22 pub field1: i32,
23 field2: u64,
24 }
25
26 struct ByeBob(i32, u64);
27
28 enum Something {
29 One,
30 Two,
31 Three
32 }
33
34 enum MoreComplicated {
35 One,
36 Two(i32),
37 Three(HiBob),
38 Four{this: bool, is: u8, a: char, struct_: u64, variant: u32},
39 }
40
41 // tests the nonzero optimization, but fields are reversed
42 enum NonZeroOptimized {
43 Empty,
44 Value(String),
45 }
46
47 fn diff2(x: i32, y: i32) -> i32 {
48 x - y
49 }
50
51 // Empty function, should not have "void"
52 // or "()" in its return type
53 fn empty() {
54
55 }
56
57 pub struct Unit;
58
59 // This triggers the non-zero optimization that yields a different
60 // enum representation in the debug info.
61 enum SpaceSaver {
62 Thebox(u8, Box<i32>),
63 Nothing,
64 }
65
66 enum Univariant {
67 Foo {a: u8}
68 }
69 enum UnivariantAnon {
70 Foo(u8)
71 }
72
73 enum ParametrizedEnum<T> {
74 Val { val: T },
75 Empty,
76 }
77
78 struct ParametrizedStruct<T> {
79 next: ParametrizedEnum<Box<ParametrizedStruct<T>>>,
80 value: T
81 }
82
83 union Union {
84 f1: i8,
85 f2: u8,
86 }
87
88 pub union Union2 {
89 pub name: [u8; 1],
90 }
91
92 struct StringAtOffset {
93 pub field1: &'static str,
94 pub field2: i32,
95 pub field3: &'static str,
96 }
97
98 // A simple structure whose layout won't be changed by the compiler,
99 // so that ptype/o testing will work on any platform.
100 struct SimpleLayout {
101 f1: u16,
102 f2: u16
103 }
104
105 enum EmptyEnum {}
106
107 fn main () {
108 let a = ();
109 let b : [i32; 0] = [];
110
111 let mut c = 27;
112 let d = c = 99;
113
114 let e = MoreComplicated::Two(73);
115 let e2 = MoreComplicated::Four {this: true, is: 8, a: 'm',
116 struct_: 100, variant: 10};
117
118 let f = "hi bob";
119 let g = b"hi bob";
120 let h = b'9';
121
122 let fslice = &f[3..];
123
124 let i = ["whatever"; 8];
125
126 let j = Unit;
127 let j2 = Unit{};
128
129 let k = SpaceSaver::Nothing;
130 let l = SpaceSaver::Thebox(9, Box::new(1729));
131
132 let v = Something::Three;
133 let w = [1,2,3,4];
134 let w_ptr = &w[0];
135 let x = (23, 25.5);
136 let y = HiBob {field1: 7, field2: 8};
137 let z = ByeBob(7, 8);
138
139 let field1 = 77;
140 let field2 = 88;
141 let y0 = HiBob { field1, field2 };
142
143 let univariant = Univariant::Foo {a : 1};
144 let univariant_anon = UnivariantAnon::Foo(1);
145
146 let slice = &w[2..3];
147 let fromslice = slice[0];
148 let slice2 = &slice[0..1];
149
150 let all1 = &w[..];
151 let all2 = &slice[..];
152
153 let from1 = &w[1..];
154 let from2 = &slice[1..];
155
156 let to1 = &w[..3];
157 let to2 = &slice[..1];
158
159 let st = StringAtOffset { field1: "hello", field2: 1, field3: "world" };
160
161 // tests for enum optimizations
162
163 let str_some = Some("hi".to_string());
164 let str_none = None::<String>;
165 let box_some = Some(Box::new(1u8));
166 let box_none = None::<Box<u8>>;
167 let int_some = Some(1u8);
168 let int_none = None::<u8>;
169 let custom_some = NonZeroOptimized::Value("hi".into());
170 let custom_none = NonZeroOptimized::Empty;
171
172 let parametrized = ParametrizedStruct {
173 next: ParametrizedEnum::Val {
174 val: Box::new(ParametrizedStruct {
175 next: ParametrizedEnum::Empty,
176 value: 1,
177 })
178 },
179 value: 0,
180 };
181
182 let u = Union { f2: 255 };
183 let simplelayout = SimpleLayout { f1: 8, f2: 9 };
184
185 let empty_enum_value: EmptyEnum;
186
187 let u2 = Union2 { name: [1] };
188
189 println!("{}, {}", x.0, x.1); // set breakpoint here
190 println!("{}", diff2(92, 45));
191 empty();
192 }