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