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