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