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