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