]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/save-analysis/foo.rs
e9ed897bf302be8e294e8a1224be72358918a6a1
[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 #![feature(box_syntax)]
13 #![feature(rustc_private)]
14
15 extern crate graphviz;
16 // A simple rust project
17
18 extern crate krate2;
19 extern crate krate2 as krate3;
20 extern crate flate as myflate;
21
22 use graphviz::RenderOption;
23 use std::collections::{HashMap,HashSet};
24 use std::cell::RefCell;
25 use std::io::Write;
26
27
28 use sub::sub2 as msalias;
29 use sub::sub2;
30 use sub::sub2::nested_struct as sub_struct;
31
32 use std::mem::size_of;
33
34 use std::char::from_u32;
35
36 static uni: &'static str = "Les Miséééééééérables";
37 static yy: usize = 25;
38
39 static bob: Option<graphviz::RenderOption> = None;
40
41 // buglink test - see issue #1337.
42
43 fn test_alias<I: Iterator>(i: Option<<I as Iterator>::Item>) {
44     let s = sub_struct{ field2: 45u32, };
45
46     // import tests
47     fn foo(x: &Write) {}
48     let _: Option<_> = from_u32(45);
49
50     let x = 42usize;
51
52     krate2::hello();
53     krate3::hello();
54     myflate::deflate_bytes(&[]);
55
56     let x = (3isize, 4usize);
57     let y = x.1;
58 }
59
60 struct TupStruct(isize, isize, Box<str>);
61
62 fn test_tup_struct(x: TupStruct) -> isize {
63     x.1
64 }
65
66 fn println(s: &str) {
67     std::io::stdout().write_all(s.as_bytes());
68 }
69
70 mod sub {
71     pub mod sub2 {
72         use std::io::Write;
73         pub mod sub3 {
74             use std::io::Write;
75             pub fn hello() {
76                 ::println("hello from module 3");
77             }
78         }
79         pub fn hello() {
80             ::println("hello from a module");
81         }
82
83         pub struct nested_struct {
84             pub field2: u32,
85         }
86
87         pub enum nested_enum {
88             Nest2 = 2,
89             Nest3 = 3
90         }
91     }
92 }
93
94 pub mod SameDir;
95 pub mod SubDir;
96
97 #[path = "SameDir3.rs"]
98 pub mod SameDir2;
99
100 struct nofields;
101
102 #[derive(Clone)]
103 struct some_fields {
104     field1: u32,
105 }
106
107 type SF = some_fields;
108
109 trait SuperTrait {
110     fn qux(&self) { panic!(); }
111 }
112
113 trait SomeTrait: SuperTrait {
114     fn Method(&self, x: u32) -> u32;
115
116     fn prov(&self, x: u32) -> u32 {
117         println(&x.to_string());
118         42
119     }
120     fn provided_method(&self) -> u32 {
121         42
122     }
123 }
124
125 trait SubTrait: SomeTrait {
126     fn stat2(x: &Self) -> u32 {
127         32
128     }
129 }
130
131 impl SomeTrait for some_fields {
132     fn Method(&self, x: u32) -> u32 {
133         println(&x.to_string());
134         self.field1
135     }
136 }
137
138 impl SuperTrait for some_fields {
139 }
140
141 impl SubTrait for some_fields {}
142
143 impl some_fields {
144     fn stat(x: u32) -> u32 {
145         println(&x.to_string());
146         42
147     }
148     fn stat2(x: &some_fields) -> u32 {
149         42
150     }
151
152     fn align_to<T>(&mut self) {
153
154     }
155
156     fn test(&mut self) {
157         self.align_to::<bool>();
158     }
159 }
160
161 impl SuperTrait for nofields {
162 }
163 impl SomeTrait for nofields {
164     fn Method(&self, x: u32) -> u32 {
165         self.Method(x);
166         43
167     }
168
169     fn provided_method(&self) -> u32 {
170         21
171     }
172 }
173
174 impl SubTrait for nofields {}
175
176 impl SuperTrait for (Box<nofields>, Box<some_fields>) {}
177
178 fn f_with_params<T: SomeTrait>(x: &T) {
179     x.Method(41);
180 }
181
182 type MyType = Box<some_fields>;
183
184 enum SomeEnum<'a> {
185     Ints(isize, isize),
186     Floats(f64, f64),
187     Strings(&'a str, &'a str, &'a str),
188     MyTypes(MyType, MyType)
189 }
190
191 #[derive(Copy, Clone)]
192 enum SomeOtherEnum {
193     SomeConst1,
194     SomeConst2,
195     SomeConst3
196 }
197
198 enum SomeStructEnum {
199     EnumStruct{a:isize, b:isize},
200     EnumStruct2{f1:MyType, f2:MyType},
201     EnumStruct3{f1:MyType, f2:MyType, f3:SomeEnum<'static>}
202 }
203
204 fn matchSomeEnum(val: SomeEnum) {
205     match val {
206         SomeEnum::Ints(int1, int2) => { println(&(int1+int2).to_string()); }
207         SomeEnum::Floats(float1, float2) => { println(&(float2*float1).to_string()); }
208         SomeEnum::Strings(.., s3) => { println(s3); }
209         SomeEnum::MyTypes(mt1, mt2) => { println(&(mt1.field1 - mt2.field1).to_string()); }
210     }
211 }
212
213 fn matchSomeStructEnum(se: SomeStructEnum) {
214     match se {
215         SomeStructEnum::EnumStruct{a:a, ..} => println(&a.to_string()),
216         SomeStructEnum::EnumStruct2{f1:f1, f2:f_2} => println(&f_2.field1.to_string()),
217         SomeStructEnum::EnumStruct3{f1, ..} => println(&f1.field1.to_string()),
218     }
219 }
220
221
222 fn matchSomeStructEnum2(se: SomeStructEnum) {
223     use SomeStructEnum::*;
224     match se {
225         EnumStruct{a: ref aaa, ..} => println(&aaa.to_string()),
226         EnumStruct2{f1, f2: f2} => println(&f1.field1.to_string()),
227         EnumStruct3{f1, f3: SomeEnum::Ints(..), f2} => println(&f1.field1.to_string()),
228         _ => {},
229     }
230 }
231
232 fn matchSomeOtherEnum(val: SomeOtherEnum) {
233     use SomeOtherEnum::{SomeConst2, SomeConst3};
234     match val {
235         SomeOtherEnum::SomeConst1 => { println("I'm const1."); }
236         SomeConst2 | SomeConst3 => { println("I'm const2 or const3."); }
237     }
238 }
239
240 fn hello<X: SomeTrait>((z, a) : (u32, String), ex: X) {
241     SameDir2::hello(43);
242
243     println(&yy.to_string());
244     let (x, y): (u32, u32) = (5, 3);
245     println(&x.to_string());
246     println(&z.to_string());
247     let x: u32 = x;
248     println(&x.to_string());
249     let x = "hello";
250     println(x);
251
252     let x = 32.0f32;
253     let _ = (x + ((x * x) + 1.0).sqrt()).ln();
254
255     let s: Box<SomeTrait> = box some_fields {field1: 43};
256     let s2: Box<some_fields> =  box some_fields {field1: 43};
257     let s3 = box nofields;
258
259     s.Method(43);
260     s3.Method(43);
261     s2.Method(43);
262
263     ex.prov(43);
264
265     let y: u32 = 56;
266     // static method on struct
267     let r = some_fields::stat(y);
268     // trait static method, calls default
269     let r = SubTrait::stat2(&*s3);
270
271     let s4 = s3 as Box<SomeTrait>;
272     s4.Method(43);
273
274     s4.provided_method();
275     s2.prov(45);
276
277     let closure = |x: u32, s: &SomeTrait| {
278         s.Method(23);
279         return x + y;
280     };
281
282     let z = closure(10, &*s);
283 }
284
285 pub struct blah {
286     used_link_args: RefCell<[&'static str; 0]>,
287 }
288
289 #[macro_use]
290 mod macro_use_test {
291     macro_rules! test_rec {
292         (q, $src: expr) => {{
293             print!("{}", $src);
294             test_rec!($src);
295         }};
296         ($src: expr) => {
297             print!("{}", $src);
298         };
299     }
300
301     macro_rules! internal_vars {
302         ($src: ident) => {{
303             let mut x = $src;
304             x += 100;
305         }};
306     }
307 }
308
309 fn main() { // foo
310     let s = box some_fields {field1: 43};
311     hello((43, "a".to_string()), *s);
312     sub::sub2::hello();
313     sub2::sub3::hello();
314
315     let h = sub2::sub3::hello;
316     h();
317
318     // utf8 chars
319     let ut = "Les Miséééééééérables";
320
321     // For some reason, this pattern of macro_rules foiled our generated code
322     // avoiding strategy.
323     macro_rules! variable_str(($name:expr) => (
324         some_fields {
325             field1: $name,
326         }
327     ));
328     let vs = variable_str!(32);
329
330     let mut candidates: RefCell<HashMap<&'static str, &'static str>> = RefCell::new(HashMap::new());
331     let _ = blah {
332         used_link_args: RefCell::new([]),
333     };
334     let s1 = nofields;
335     let s2 = SF { field1: 55};
336     let s3: some_fields = some_fields{ field1: 55};
337     let s4: msalias::nested_struct = sub::sub2::nested_struct{ field2: 55};
338     let s4: msalias::nested_struct = sub2::nested_struct{ field2: 55};
339     println(&s2.field1.to_string());
340     let s5: MyType = box some_fields{ field1: 55};
341     let s = SameDir::SameStruct{name: "Bob".to_string()};
342     let s = SubDir::SubStruct{name:"Bob".to_string()};
343     let s6: SomeEnum = SomeEnum::MyTypes(box s2.clone(), s5);
344     let s7: SomeEnum = SomeEnum::Strings("one", "two", "three");
345     matchSomeEnum(s6);
346     matchSomeEnum(s7);
347     let s8: SomeOtherEnum = SomeOtherEnum::SomeConst2;
348     matchSomeOtherEnum(s8);
349     let s9: SomeStructEnum = SomeStructEnum::EnumStruct2{ f1: box some_fields{ field1:10 },
350                                                           f2: box s2 };
351     matchSomeStructEnum(s9);
352
353     for x in &vec![1, 2, 3] {
354         let _y = x;
355     }
356
357     let s7: SomeEnum = SomeEnum::Strings("one", "two", "three");
358     if let SomeEnum::Strings(..) = s7 {
359         println!("hello!");
360     }
361
362     for i in 0..5 {
363         foo_foo(i);
364     }
365
366     if let Some(x) = None {
367         foo_foo(x);
368     }
369
370     if false {
371     } else if let Some(y) = None {
372         foo_foo(y);
373     }
374
375     while let Some(z) = None {
376         foo_foo(z);
377     }
378
379     let mut x = 4;
380     test_rec!(q, "Hello");
381     assert_eq!(x, 4);
382     internal_vars!(x);
383 }
384
385 fn foo_foo(_: i32) {}
386
387 impl Iterator for nofields {
388     type Item = (usize, usize);
389
390     fn next(&mut self) -> Option<(usize, usize)> {
391         panic!()
392     }
393
394     fn size_hint(&self) -> (usize, Option<usize>) {
395         panic!()
396     }
397 }
398
399 trait Pattern<'a> {
400     type Searcher;
401 }
402
403 struct CharEqPattern;
404
405 impl<'a> Pattern<'a> for CharEqPattern {
406     type Searcher = CharEqPattern;
407 }
408
409 struct CharSearcher<'a>(<CharEqPattern as Pattern<'a>>::Searcher);
410
411 pub trait Error {
412 }
413
414 impl Error + 'static {
415     pub fn is<T: Error + 'static>(&self) -> bool {
416         panic!()
417     }
418 }
419
420 impl Error + 'static + Send {
421     pub fn is<T: Error + 'static>(&self) -> bool {
422         <Error + 'static>::is::<T>(self)
423     }
424 }
425 extern crate serialize;
426 #[derive(Clone, Copy, Hash, Encodable, Decodable, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
427 struct AllDerives(i32);
428
429 fn test_format_args() {
430     let x = 1;
431     let y = 2;
432     let name = "Joe Blogg";
433     println!("Hello {}", name);
434     print!("Hello {0}", name);
435     print!("{0} + {} = {}", x, y);
436     print!("x is {}, y is {1}, name is {n}", x, y, n = name);
437 }