]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/save-analysis/foo.rs
4d75e58aad938a05cb65590936ea8f9636c78e7c
[rust.git] / src / test / run-make / save-analysis / foo.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![ crate_name = "test" ]
12 #![allow(unstable)]
13 #![feature(box_syntax, rustc_private, core, zero_one)]
14
15 extern crate graphviz;
16 // A simple rust project
17
18 extern crate flate as myflate;
19
20 use std::collections::{HashMap,HashSet};
21 use std::cell::RefCell;
22
23
24 use sub::sub2 as msalias;
25 use sub::sub2;
26 use sub::sub2::nested_struct as sub_struct;
27 use std::num::One;
28
29 use std::mem::size_of;
30
31 static uni: &'static str = "Les Miséééééééérables";
32 static yy: usize = 25;
33
34 static bob: Option<&'static [isize]> = None;
35
36 // buglink test - see issue #1337.
37
38 fn test_alias<I: Iterator>(i: Option<<I as Iterator>::Item>) {
39     let s = sub_struct{ field2: 45, };
40
41     // import tests
42     fn foo(x: &One) {}
43
44     let x = 42;
45
46     myflate::deflate_bytes(&[]);
47
48     let x = (3, 4);
49     let y = x.1;
50 }
51
52 struct TupStruct(isize, isize, Box<str>);
53
54 fn test_tup_struct(x: TupStruct) -> isize {
55     x.1
56 }
57
58 mod sub {
59     pub mod sub2 {
60         pub mod sub3 {
61             pub fn hello() {
62                 println!("hello from module 3");
63             }
64         }
65         pub fn hello() {
66             println!("hello from a module");
67         }
68
69         pub struct nested_struct {
70             pub field2: u32,
71         }
72
73         pub enum nested_enum {
74             Nest2 = 2,
75             Nest3 = 3
76         }
77     }
78 }
79
80 pub mod SameDir;
81 pub mod SubDir;
82
83 #[path = "SameDir3.rs"]
84 pub mod SameDir2;
85
86 struct nofields;
87
88 #[derive(Clone)]
89 struct some_fields {
90     field1: u32,
91 }
92
93 type SF = some_fields;
94
95 trait SuperTrait {
96     fn dummy(&self) { }
97 }
98
99 trait SomeTrait: SuperTrait {
100     fn Method(&self, x: u32) -> u32;
101
102     fn prov(&self, x: u32) -> u32 {
103         println!("{}", &x.to_string());
104         42
105     }
106     fn provided_method(&self) -> u32 {
107         42
108     }
109 }
110
111 trait SubTrait: SomeTrait {
112     fn stat2(x: &Self) -> u32 {
113         32
114     }
115 }
116
117 impl SomeTrait for some_fields {
118     fn Method(&self, x: u32) -> u32 {
119         println!("{}", &x.to_string());
120         self.field1
121     }
122 }
123
124 impl SuperTrait for some_fields {
125 }
126
127 impl SubTrait for some_fields {}
128
129 impl some_fields {
130     fn stat(x: u32) -> u32 {
131         println!("{}", &x.to_string());
132         42
133     }
134     fn stat2(x: &some_fields) -> u32 {
135         42
136     }
137
138     fn align_to<T>(&mut self) {
139     }
140
141     fn test(&mut self) {
142         self.align_to::<bool>();
143     }
144 }
145
146 impl SuperTrait for nofields {
147 }
148 impl SomeTrait for nofields {
149     fn Method(&self, x: u32) -> u32 {
150         self.Method(x);
151         43
152     }
153
154     fn provided_method(&self) -> u32 {
155         21
156     }
157 }
158
159 impl SubTrait for nofields {}
160
161 impl SuperTrait for (Box<nofields>, Box<some_fields>) {}
162
163 fn f_with_params<T: SomeTrait>(x: &T) {
164     x.Method(41);
165 }
166
167 type MyType = Box<some_fields>;
168
169 enum SomeEnum<'a> {
170     Ints(isize, isize),
171     Floats(f64, f64),
172     Strings(&'a str, &'a str, &'a str),
173     MyTypes(MyType, MyType)
174 }
175
176 #[derive(Copy, Clone)]
177 enum SomeOtherEnum {
178     SomeConst1,
179     SomeConst2,
180     SomeConst3
181 }
182
183 enum SomeStructEnum {
184     EnumStruct{a:isize, b:isize},
185     EnumStruct2{f1:MyType, f2:MyType},
186     EnumStruct3{f1:MyType, f2:MyType, f3:SomeEnum<'static>}
187 }
188
189 fn matchSomeEnum(val: SomeEnum) {
190     match val {
191         SomeEnum::Ints(int1, int2) => { println!("{}", &(int1+int2).to_string()); }
192         SomeEnum::Floats(float1, float2) => { println!("{}", &(float2*float1).to_string()); }
193         SomeEnum::Strings(_, _, s3) => { println!("{}", s3); }
194         SomeEnum::MyTypes(mt1, mt2) => {
195             println!("{}", &(mt1.field1 - mt2.field1).to_string());
196         }
197     }
198 }
199
200 fn matchSomeStructEnum(se: SomeStructEnum) {
201     match se {
202         SomeStructEnum::EnumStruct{a:a, ..} => println!("{}", &a.to_string()),
203         SomeStructEnum::EnumStruct2{f1:f1, f2:f_2} => println!("{}", &f_2.field1.to_string()),
204         SomeStructEnum::EnumStruct3{f1, ..} => println!("{}", &f1.field1.to_string()),
205     }
206 }
207
208
209 fn matchSomeStructEnum2(se: SomeStructEnum) {
210     use SomeStructEnum::*;
211     match se {
212         EnumStruct{a: ref aaa, ..} => println!("{}", &aaa.to_string()),
213         EnumStruct2{f1, f2: f2} => println!("{}", &f1.field1.to_string()),
214         EnumStruct3{f1, f3: SomeEnum::Ints(_, _), f2} => println!("{}", &f1.field1.to_string()),
215         _ => {},
216     }
217 }
218
219 fn matchSomeOtherEnum(val: SomeOtherEnum) {
220     use SomeOtherEnum::{SomeConst2, SomeConst3};
221     match val {
222         SomeOtherEnum::SomeConst1 => { println!("I'm const1."); }
223         SomeConst2 | SomeConst3 => { println!("I'm const2 or const3."); }
224     }
225 }
226
227 fn hello<X: SomeTrait>((z, a) : (u32, String), ex: X) {
228     SameDir2::hello(43);
229
230     println!("{}", &yy.to_string());
231     let (x, y): (u32, u32) = (5, 3);
232     println!("{}", &x.to_string());
233     println!("{}", &z.to_string());
234     let x: u32 = x;
235     println!("{}", &x.to_string());
236     let x = "hello";
237     println!("{}", x);
238
239     let x = 32.0f32;
240     let _ = (x + ((x * x) + 1.0).sqrt()).ln();
241
242     // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
243     let s: Box<SomeTrait> = Box::new(some_fields {field1: 43});
244     let s2: Box<some_fields> =  box some_fields {field1: 43};
245     let s3: Box<_> = box nofields;
246
247     s.Method(43);
248     s3.Method(43);
249     s2.Method(43);
250
251     ex.prov(43);
252
253     let y: u32 = 56;
254     // static method on struct
255     let r = some_fields::stat(y);
256     // trait static method, calls override
257     let r = SubTrait::stat2(&*s2);
258     // trait static method, calls default
259     let r = SubTrait::stat2(&*s3);
260
261     let s4 = s3 as Box<SomeTrait>;
262     s4.Method(43);
263
264     s4.provided_method();
265     s2.prov(45);
266
267     let closure = |x: u32, s: &SomeTrait| {
268         s.Method(23);
269         return x + y;
270     };
271
272     let z = closure(10, &*s);
273 }
274
275 pub struct blah {
276     used_link_args: RefCell<[&'static str; 0]>,
277 }
278
279 fn main() { // foo
280     let s: Box<_> = box some_fields {field1: 43};
281     hello((43, "a".to_string()), *s);
282     sub::sub2::hello();
283     sub2::sub3::hello();
284
285     let h = sub2::sub3::hello;
286     h();
287
288     // utf8 chars
289     let ut = "Les Miséééééééérables";
290
291     // For some reason, this pattern of macro_rules foiled our generated code
292     // avoiding strategy.
293     macro_rules! variable_str(($name:expr) => (
294         some_fields {
295             field1: $name,
296         }
297     ));
298     let vs = variable_str!(32);
299
300     let mut candidates: RefCell<HashMap<&'static str, &'static str>> = RefCell::new(HashMap::new());
301     let _ = blah {
302         used_link_args: RefCell::new([]),
303     };
304     let s1 = nofields;
305     let s2 = SF { field1: 55};
306     let s3: some_fields = some_fields{ field1: 55};
307     let s4: msalias::nested_struct = sub::sub2::nested_struct{ field2: 55};
308     let s4: msalias::nested_struct = sub2::nested_struct{ field2: 55};
309     println!("{}", &s2.field1.to_string());
310     let s5: MyType = box some_fields{ field1: 55};
311     let s = SameDir::SameStruct{name: "Bob".to_string()};
312     let s = SubDir::SubStruct{name:"Bob".to_string()};
313     let s6: SomeEnum = SomeEnum::MyTypes(box s2.clone(), s5);
314     let s7: SomeEnum = SomeEnum::Strings("one", "two", "three");
315     matchSomeEnum(s6);
316     matchSomeEnum(s7);
317     let s8: SomeOtherEnum = SomeOtherEnum::SomeConst2;
318     matchSomeOtherEnum(s8);
319     let s9: SomeStructEnum =
320         SomeStructEnum::EnumStruct2{f1: box some_fields{field1:10}, f2: box s2};
321     matchSomeStructEnum(s9);
322 }
323
324 impl Iterator for nofields {
325     type Item = (usize, usize);
326
327     fn next(&mut self) -> Option<(usize, usize)> {
328         panic!()
329     }
330
331     fn size_hint(&self) -> (usize, Option<usize>) {
332         panic!()
333     }
334 }