Fix handling of discriminantless univariant enums in Rust; fix bug with encoded enums
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.rust / simple.rs
CommitLineData
67218854
TT
1// Copyright (C) 2016 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
21pub struct HiBob {
22 pub field1: i32,
23 field2: u64,
24}
25
26struct ByeBob(i32, u64);
27
28enum Something {
29 One,
30 Two,
31 Three
32}
33
34enum MoreComplicated {
35 One,
36 Two(i32),
37 Three(HiBob),
38 Four{this: bool, is: u8, a: char, struct_: u64, variant: u32},
39}
40
fccb08f8
MG
41// tests the nonzero optimization, but fields are reversed
42enum NonZeroOptimized {
43 Empty,
44 Value(String),
45}
46
67218854
TT
47fn diff2(x: i32, y: i32) -> i32 {
48 x - y
49}
50
921d8f54
MG
51// Empty function, should not have "void"
52// or "()" in its return type
53fn empty() {
54
55}
56
67218854
TT
57pub struct Unit;
58
59// This triggers the non-zero optimization that yields a different
60// enum representation in the debug info.
61enum SpaceSaver {
62 Thebox(u8, Box<i32>),
63 Nothing,
64}
65
51a789c3
MG
66enum Univariant {
67 Foo {a: u8}
68}
69enum UnivariantAnon {
70 Foo(u8)
71}
72
73enum ParametrizedEnum<T> {
74 Val { val: T },
75 Empty,
76}
77
78struct ParametrizedStruct<T> {
79 next: ParametrizedEnum<Box<ParametrizedStruct<T>>>,
80 value: T
81}
82
67218854
TT
83fn main () {
84 let a = ();
85 let b : [i32; 0] = [];
86
87 let mut c = 27;
88 let d = c = 99;
89
90 let e = MoreComplicated::Two(73);
91 let e2 = MoreComplicated::Four {this: true, is: 8, a: 'm',
92 struct_: 100, variant: 10};
93
94 let f = "hi bob";
95 let g = b"hi bob";
96 let h = b'9';
97
98 let i = ["whatever"; 8];
99
100 let j = Unit;
12df5c00 101 let j2 = Unit{};
67218854
TT
102
103 let k = SpaceSaver::Nothing;
104 let l = SpaceSaver::Thebox(9, Box::new(1729));
105
106 let v = Something::Three;
107 let w = [1,2,3,4];
42d94011 108 let w_ptr = &w[0];
67218854
TT
109 let x = (23, 25.5);
110 let y = HiBob {field1: 7, field2: 8};
111 let z = ByeBob(7, 8);
112
51a789c3
MG
113 let univariant = Univariant::Foo {a : 1};
114 let univariant_anon = UnivariantAnon::Foo(1);
115
67218854
TT
116 let slice = &w[2..3];
117 let fromslice = slice[0];
118 let slice2 = &slice[0..1];
119
120 let all1 = &w[..];
121 let all2 = &slice[..];
122
123 let from1 = &w[1..];
124 let from2 = &slice[1..];
125
126 let to1 = &w[..3];
127 let to2 = &slice[..1];
128
fccb08f8
MG
129 // tests for enum optimizations
130
131 let str_some = Some("hi".to_string());
132 let str_none = None::<String>;
133 let box_some = Some(Box::new(1u8));
134 let box_none = None::<Box<u8>>;
135 let int_some = Some(1u8);
136 let int_none = None::<u8>;
137 let custom_some = NonZeroOptimized::Value("hi".into());
138 let custom_none = NonZeroOptimized::Empty;
139
51a789c3
MG
140 let parametrized = ParametrizedStruct {
141 next: ParametrizedEnum::Val {
142 val: Box::new(ParametrizedStruct {
143 next: ParametrizedEnum::Empty,
144 value: 1,
145 })
146 },
147 value: 0,
148 };
149
67218854
TT
150 println!("{}, {}", x.0, x.1); // set breakpoint here
151 println!("{}", diff2(92, 45));
921d8f54 152 empty();
67218854 153}
This page took 0.118576 seconds and 4 git commands to generate.