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