1 #![crate_name = "test"]
2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
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<graphviz::RenderOption> = None;
28
29 // buglink test - see issue #1337.
30
test_alias<I: Iterator>(i: Option<<I as Iterator>::Item>)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
test_tup_struct(x: TupStruct) -> isize55 fn test_tup_struct(x: TupStruct) -> isize {
56 x.1
57 }
58
println(s: &str)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;
hello()68 pub fn hello() {
69 ::println("hello from module 3");
70 }
71 }
hello()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 {
qux(&self)103 fn qux(&self) {
104 panic!();
105 }
106 }
107
108 trait SomeTrait: SuperTrait {
Method(&self, x: u32) -> u32109 fn Method(&self, x: u32) -> u32;
110
prov(&self, x: u32) -> u32111 fn prov(&self, x: u32) -> u32 {
112 println(&x.to_string());
113 42
114 }
provided_method(&self) -> u32115 fn provided_method(&self) -> u32 {
116 42
117 }
118 }
119
120 trait SubTrait: SomeTrait {
stat2(x: &Self) -> u32121 fn stat2(x: &Self) -> u32 {
122 32
123 }
124 }
125
126 trait SizedTrait: Sized {}
127
error(s: &SizedTrait)128 fn error(s: &SizedTrait) {
129 let foo = 42;
130 println!("Hello world! {}", foo);
131 }
132
133 impl SomeTrait for some_fields {
Method(&self, x: u32) -> u32134 fn Method(&self, x: u32) -> u32 {
135 println(&x.to_string());
136 self.field1
137 }
138 }
139
140 impl SuperTrait for some_fields {}
141
142 impl SubTrait for some_fields {}
143
144 impl some_fields {
stat(x: u32) -> u32145 fn stat(x: u32) -> u32 {
146 println(&x.to_string());
147 42
148 }
stat2(x: &some_fields) -> u32149 fn stat2(x: &some_fields) -> u32 {
150 42
151 }
152
align_to<T>(&mut self)153 fn align_to<T>(&mut self) {}
154
test(&mut self)155 fn test(&mut self) {
156 self.align_to::<bool>();
157 }
158 }
159
160 impl SuperTrait for nofields {}
161 impl SomeTrait for nofields {
Method(&self, x: u32) -> u32162 fn Method(&self, x: u32) -> u32 {
163 self.Method(x);
164 43
165 }
166
provided_method(&self) -> u32167 fn provided_method(&self) -> u32 {
168 21
169 }
170 }
171
172 impl SubTrait for nofields {}
173
174 impl SuperTrait for (Box<nofields>, Box<some_fields>) {}
175
f_with_params<T: SomeTrait>(x: &T)176 fn f_with_params<T: SomeTrait>(x: &T) {
177 x.Method(41);
178 }
179
180 type MyType = Box<some_fields>;
181
182 enum SomeEnum<'a> {
183 Ints(isize, isize),
184 Floats(f64, f64),
185 Strings(&'a str, &'a str, &'a str),
186 MyTypes(MyType, MyType),
187 }
188
189 #[derive(Copy, Clone)]
190 enum SomeOtherEnum {
191 SomeConst1,
192 SomeConst2,
193 SomeConst3,
194 }
195
196 enum SomeStructEnum {
197 EnumStruct { a: isize, b: isize },
198 EnumStruct2 { f1: MyType, f2: MyType },
199 EnumStruct3 { f1: MyType, f2: MyType, f3: SomeEnum<'static> },
200 }
201
matchSomeEnum(val: SomeEnum)202 fn matchSomeEnum(val: SomeEnum) {
203 match val {
204 SomeEnum::Ints(int1, int2) => {
205 println(&(int1 + int2).to_string());
206 }
207 SomeEnum::Floats(float1, float2) => {
208 println(&(float2 * float1).to_string());
209 }
210 SomeEnum::Strings(.., s3) => {
211 println(s3);
212 }
213 SomeEnum::MyTypes(mt1, mt2) => {
214 println(&(mt1.field1 - mt2.field1).to_string());
215 }
216 }
217 }
218
matchSomeStructEnum(se: SomeStructEnum)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
matchSomeStructEnum2(se: SomeStructEnum)227 fn matchSomeStructEnum2(se: SomeStructEnum) {
228 use SomeStructEnum::*;
229 match se {
230 EnumStruct { a: ref aaa, .. } => println(&aaa.to_string()),
231 EnumStruct2 { f1, f2: f2 } => println(&f1.field1.to_string()),
232 EnumStruct3 { f1, f3: SomeEnum::Ints(..), f2 } => println(&f1.field1.to_string()),
233 _ => {}
234 }
235 }
236
matchSomeOtherEnum(val: SomeOtherEnum)237 fn matchSomeOtherEnum(val: SomeOtherEnum) {
238 use SomeOtherEnum::{SomeConst2, SomeConst3};
239 match val {
240 SomeOtherEnum::SomeConst1 => {
241 println("I'm const1.");
242 }
243 SomeConst2 | SomeConst3 => {
244 println("I'm const2 or const3.");
245 }
246 }
247 }
248
hello<X: SomeTrait>((z, a): (u32, String), ex: X)249 fn hello<X: SomeTrait>((z, a): (u32, String), ex: X) {
250 SameDir2::hello(43);
251
252 println(&yy.to_string());
253 let (x, y): (u32, u32) = (5, 3);
254 println(&x.to_string());
255 println(&z.to_string());
256 let x: u32 = x;
257 println(&x.to_string());
258 let x = "hello";
259 println(x);
260
261 let x = 32.0f32;
262 let _ = (x + ((x * x) + 1.0).sqrt()).ln();
263
264 let s: Box<SomeTrait> = box some_fields { field1: 43 };
265 let s2: Box<some_fields> = box some_fields { field1: 43 };
266 let s3 = box nofields;
267
268 s.Method(43);
269 s3.Method(43);
270 s2.Method(43);
271
272 ex.prov(43);
273
274 let y: u32 = 56;
275 // static method on struct
276 let r = some_fields::stat(y);
277 // trait static method, calls default
278 let r = SubTrait::stat2(&*s3);
279
280 let s4 = s3 as Box<SomeTrait>;
281 s4.Method(43);
282
283 s4.provided_method();
284 s2.prov(45);
285
286 let closure = |x: u32, s: &SomeTrait| {
287 s.Method(23);
288 return x + y;
289 };
290
291 let z = closure(10, &*s);
292 }
293
294 pub struct blah {
295 used_link_args: RefCell<[&'static str; 0]>,
296 }
297
298 #[macro_use]
299 mod macro_use_test {
300 macro_rules! test_rec {
301 (q, $src: expr) => {{
302 print!("{}", $src);
303 test_rec!($src);
304 }};
305 ($src: expr) => {
306 print!("{}", $src);
307 };
308 }
309
310 macro_rules! internal_vars {
311 ($src: ident) => {{
312 let mut x = $src;
313 x += 100;
314 }};
315 }
316 }
317
main()318 fn main() {
319 // foo
320 let s = box some_fields { field1: 43 };
321 hello((43, "a".to_string()), *s);
322 sub::sub2::hello();
323 sub2::sub3::hello();
324
325 let h = sub2::sub3::hello;
326 h();
327
328 // utf8 chars
329 let ut = "Les Miséééééééérables";
330
331 // For some reason, this pattern of macro_rules foiled our generated code
332 // avoiding strategy.
333 macro_rules! variable_str(($name:expr) => (
334 some_fields {
335 field1: $name,
336 }
337 ));
338 let vs = variable_str!(32);
339
340 let mut candidates: RefCell<HashMap<&'static str, &'static str>> = RefCell::new(HashMap::new());
341 let _ = blah { used_link_args: RefCell::new([]) };
342 let s1 = nofields;
343 let s2 = SF { field1: 55 };
344 let s3: some_fields = some_fields { field1: 55 };
345 let s4: msalias::nested_struct = sub::sub2::nested_struct { field2: 55 };
346 let s4: msalias::nested_struct = sub2::nested_struct { field2: 55 };
347 println(&s2.field1.to_string());
348 let s5: MyType = box some_fields { field1: 55 };
349 let s = SameDir::SameStruct { name: "Bob".to_string() };
350 let s = SubDir::SubStruct { name: "Bob".to_string() };
351 let s6: SomeEnum = SomeEnum::MyTypes(box s2.clone(), s5);
352 let s7: SomeEnum = SomeEnum::Strings("one", "two", "three");
353 matchSomeEnum(s6);
354 matchSomeEnum(s7);
355 let s8: SomeOtherEnum = SomeOtherEnum::SomeConst2;
356 matchSomeOtherEnum(s8);
357 let s9: SomeStructEnum =
358 SomeStructEnum::EnumStruct2 { f1: box some_fields { field1: 10 }, f2: box s2 };
359 matchSomeStructEnum(s9);
360
361 for x in &vec![1, 2, 3] {
362 let _y = x;
363 }
364
365 let s7: SomeEnum = SomeEnum::Strings("one", "two", "three");
366 if let SomeEnum::Strings(..) = s7 {
367 println!("hello!");
368 }
369
370 for i in 0..5 {
371 foo_foo(i);
372 }
373
374 if let Some(x) = None {
375 foo_foo(x);
376 }
377
378 if false {
379 } else if let Some(y) = None {
380 foo_foo(y);
381 }
382
383 while let Some(z) = None {
384 foo_foo(z);
385 }
386
387 let mut x = 4;
388 test_rec!(q, "Hello");
389 assert_eq!(x, 4);
390 internal_vars!(x);
391 }
392
foo_foo(_: i32)393 fn foo_foo(_: i32) {}
394
395 impl Iterator for nofields {
396 type Item = (usize, usize);
397
next(&mut self) -> Option<(usize, usize)>398 fn next(&mut self) -> Option<(usize, usize)> {
399 panic!()
400 }
401
size_hint(&self) -> (usize, Option<usize>)402 fn size_hint(&self) -> (usize, Option<usize>) {
403 panic!()
404 }
405 }
406
407 trait Pattern<'a> {
408 type Searcher;
409 }
410
411 struct CharEqPattern;
412
413 impl<'a> Pattern<'a> for CharEqPattern {
414 type Searcher = CharEqPattern;
415 }
416
417 struct CharSearcher<'a>(<CharEqPattern as Pattern<'a>>::Searcher);
418
419 pub trait Error {}
420
421 impl Error + 'static {
is<T: Error + 'static>(&self) -> bool422 pub fn is<T: Error + 'static>(&self) -> bool {
423 panic!()
424 }
425 }
426
427 impl Error + 'static + Send {
is<T: Error + 'static>(&self) -> bool428 pub fn is<T: Error + 'static>(&self) -> bool {
429 <Error + 'static>::is::<T>(self)
430 }
431 }
432 extern crate serialize;
433 #[derive(Clone, Copy, Hash, Encodable, Decodable, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
434 struct AllDerives(i32);
435
test_format_args()436 fn test_format_args() {
437 let x = 1;
438 let y = 2;
439 let name = "Joe Blogg";
440 println!("Hello {}", name);
441 print!("Hello {0}", name);
442 print!("{0} + {} = {}", x, y);
443 print!("x is {}, y is {1}, name is {n}", x, y, n = name);
444 }
445
446 extern "C" {
447 static EXTERN_FOO: u8;
extern_foo(a: u8, b: i32) -> String448 fn extern_foo(a: u8, b: i32) -> String;
449 }
450
451 struct Rls699 {
452 f: u32,
453 }
454
new(f: u32) -> Rls699455 fn new(f: u32) -> Rls699 {
456 Rls699 { fs }
457 }
458
invalid_tuple_struct_access()459 fn invalid_tuple_struct_access() {
460 bar.0;
461
462 struct S;
463 S.0;
464 }
465