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