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