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