rust/25535 Apply embedded offset to enum variant calculation
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.rust / simple.rs
1 // Copyright (C) 2016-2020 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 #[derive(Debug)]
108 struct EnumWithNonzeroOffset {
109 a: Option<u8>,
110 b: Option<u8>,
111 }
112
113 fn main () {
114 let a = ();
115 let b : [i32; 0] = [];
116
117 let mut c = 27;
118 let d = c = 99;
119
120 let e = MoreComplicated::Two(73);
121 let e2 = MoreComplicated::Four {this: true, is: 8, a: 'm',
122 struct_: 100, variant: 10};
123
124 let f = "hi bob";
125 let g = b"hi bob";
126 let h = b'9';
127
128 let fslice = &f[3..];
129
130 let i = ["whatever"; 8];
131
132 let j = Unit;
133 let j2 = Unit{};
134
135 let k = SpaceSaver::Nothing;
136 let l = SpaceSaver::Thebox(9, Box::new(1729));
137
138 let v = Something::Three;
139 let w = [1,2,3,4];
140 let w_ptr = &w[0];
141 let x = (23, 25.5);
142 let y = HiBob {field1: 7, field2: 8};
143 let z = ByeBob(7, 8);
144
145 let field1 = 77;
146 let field2 = 88;
147 let y0 = HiBob { field1, field2 };
148
149 let univariant = Univariant::Foo {a : 1};
150 let univariant_anon = UnivariantAnon::Foo(1);
151
152 let slice = &w[2..3];
153 let fromslice = slice[0];
154 let slice2 = &slice[0..1];
155
156 let all1 = &w[..];
157 let all2 = &slice[..];
158
159 let from1 = &w[1..];
160 let from2 = &slice[1..];
161
162 let to1 = &w[..3];
163 let to2 = &slice[..1];
164
165 let st = StringAtOffset { field1: "hello", field2: 1, field3: "world" };
166
167 // tests for enum optimizations
168
169 let str_some = Some("hi".to_string());
170 let str_none = None::<String>;
171 let box_some = Some(Box::new(1u8));
172 let box_none = None::<Box<u8>>;
173 let int_some = Some(1u8);
174 let int_none = None::<u8>;
175 let custom_some = NonZeroOptimized::Value("hi".into());
176 let custom_none = NonZeroOptimized::Empty;
177
178 let parametrized = ParametrizedStruct {
179 next: ParametrizedEnum::Val {
180 val: Box::new(ParametrizedStruct {
181 next: ParametrizedEnum::Empty,
182 value: 1,
183 })
184 },
185 value: 0,
186 };
187
188 let u = Union { f2: 255 };
189 let simplelayout = SimpleLayout { f1: 8, f2: 9 };
190
191 let empty_enum_value: EmptyEnum;
192
193 let u2 = Union2 { name: [1] };
194
195 let nonzero_offset = EnumWithNonzeroOffset { a: Some(1), b: None };
196
197 println!("{}, {}", x.0, x.1); // set breakpoint here
198 println!("{}", diff2(92, 45));
199 empty();
200 }
This page took 0.03444 seconds and 4 git commands to generate.