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.
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.
11 #![ crate_name = "test" ]
12 #![feature(box_syntax)]
13 #![feature(rustc_private)]
14 #![feature(associated_type_defaults)]
15 #![feature(external_doc)]
17 extern crate graphviz;
18 // A simple rust project
21 extern crate krate2 as krate3;
23 use graphviz::RenderOption;
24 use std::collections::{HashMap,HashSet};
25 use std::cell::RefCell;
29 use sub::sub2 as msalias;
31 use sub::sub2::nested_struct as sub_struct;
33 use std::mem::size_of;
35 use std::char::from_u32;
37 static uni: &'static str = "Les Miséééééééérables";
38 static yy: usize = 25;
40 static bob: Option<graphviz::RenderOption> = None;
42 // buglink test - see issue #1337.
44 fn test_alias<I: Iterator>(i: Option<<I as Iterator>::Item>) {
45 let s = sub_struct{ field2: 45u32, };
49 let _: Option<_> = from_u32(45);
56 let x = (3isize, 4usize);
61 const LUT_BITS: usize = 3;
62 pub struct HuffmanTable {
63 ac_lut: Option<[(i16, u8); 1 << LUT_BITS]>,
66 struct TupStruct(isize, isize, Box<str>);
68 fn test_tup_struct(x: TupStruct) -> isize {
73 std::io::stdout().write_all(s.as_bytes());
82 ::println("hello from module 3");
86 ::println("hello from a module");
89 pub struct nested_struct {
93 pub enum nested_enum {
103 #[path = "SameDir3.rs"]
113 type SF = some_fields;
116 fn qux(&self) { panic!(); }
119 trait SomeTrait: SuperTrait {
120 fn Method(&self, x: u32) -> u32;
122 fn prov(&self, x: u32) -> u32 {
123 println(&x.to_string());
126 fn provided_method(&self) -> u32 {
131 trait SubTrait: SomeTrait {
132 fn stat2(x: &Self) -> u32 {
137 impl SomeTrait for some_fields {
138 fn Method(&self, x: u32) -> u32 {
139 println(&x.to_string());
144 impl SuperTrait for some_fields {
147 impl SubTrait for some_fields {}
150 fn stat(x: u32) -> u32 {
151 println(&x.to_string());
154 fn stat2(x: &some_fields) -> u32 {
158 fn align_to<T>(&mut self) {
163 self.align_to::<bool>();
167 impl SuperTrait for nofields {
169 impl SomeTrait for nofields {
170 fn Method(&self, x: u32) -> u32 {
175 fn provided_method(&self) -> u32 {
180 impl SubTrait for nofields {}
182 impl SuperTrait for (Box<nofields>, Box<some_fields>) {}
184 fn f_with_params<T: SomeTrait>(x: &T) {
188 type MyType = Box<some_fields>;
193 Strings(&'a str, &'a str, &'a str),
194 MyTypes(MyType, MyType)
197 #[derive(Copy, Clone)]
204 enum SomeStructEnum {
205 EnumStruct{a:isize, b:isize},
206 EnumStruct2{f1:MyType, f2:MyType},
207 EnumStruct3{f1:MyType, f2:MyType, f3:SomeEnum<'static>}
210 fn matchSomeEnum(val: SomeEnum) {
212 SomeEnum::Ints(int1, int2) => { println(&(int1+int2).to_string()); }
213 SomeEnum::Floats(float1, float2) => { println(&(float2*float1).to_string()); }
214 SomeEnum::Strings(.., s3) => { println(s3); }
215 SomeEnum::MyTypes(mt1, mt2) => { println(&(mt1.field1 - mt2.field1).to_string()); }
219 fn matchSomeStructEnum(se: SomeStructEnum) {
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()),
228 fn matchSomeStructEnum2(se: SomeStructEnum) {
229 use SomeStructEnum::*;
231 EnumStruct{a: ref aaa, ..} => println(&aaa.to_string()),
232 EnumStruct2{f1, f2: f2} => println(&f1.field1.to_string()),
233 EnumStruct3{f1, f3: SomeEnum::Ints(..), f2} => println(&f1.field1.to_string()),
238 fn matchSomeOtherEnum(val: SomeOtherEnum) {
239 use SomeOtherEnum::{SomeConst2, SomeConst3};
241 SomeOtherEnum::SomeConst1 => { println("I'm const1."); }
242 SomeConst2 | SomeConst3 => { println("I'm const2 or const3."); }
246 fn hello<X: SomeTrait>((z, a) : (u32, String), ex: X) {
249 println(&yy.to_string());
250 let (x, y): (u32, u32) = (5, 3);
251 println(&x.to_string());
252 println(&z.to_string());
254 println(&x.to_string());
259 let _ = (x + ((x * x) + 1.0).sqrt()).ln();
261 let s: Box<SomeTrait> = box some_fields {field1: 43};
262 let s2: Box<some_fields> = box some_fields {field1: 43};
263 let s3 = box nofields;
272 // static method on struct
273 let r = some_fields::stat(y);
274 // trait static method, calls default
275 let r = SubTrait::stat2(&*s3);
277 let s4 = s3 as Box<SomeTrait>;
280 s4.provided_method();
283 let closure = |x: u32, s: &SomeTrait| {
288 let z = closure(10, &*s);
292 used_link_args: RefCell<[&'static str; 0]>,
297 macro_rules! test_rec {
298 (q, $src: expr) => {{
307 macro_rules! internal_vars {
316 let s = box some_fields {field1: 43};
317 hello((43, "a".to_string()), *s);
321 let h = sub2::sub3::hello;
325 let ut = "Les Miséééééééérables";
327 // For some reason, this pattern of macro_rules foiled our generated code
328 // avoiding strategy.
329 macro_rules! variable_str(($name:expr) => (
334 let vs = variable_str!(32);
336 let mut candidates: RefCell<HashMap<&'static str, &'static str>> = RefCell::new(HashMap::new());
338 used_link_args: RefCell::new([]),
341 let s2 = SF { field1: 55};
342 let s3: some_fields = some_fields{ field1: 55};
343 let s4: msalias::nested_struct = sub::sub2::nested_struct{ field2: 55};
344 let s4: msalias::nested_struct = sub2::nested_struct{ field2: 55};
345 println(&s2.field1.to_string());
346 let s5: MyType = box some_fields{ field1: 55};
347 let s = SameDir::SameStruct{name: "Bob".to_string()};
348 let s = SubDir::SubStruct{name:"Bob".to_string()};
349 let s6: SomeEnum = SomeEnum::MyTypes(box s2.clone(), s5);
350 let s7: SomeEnum = SomeEnum::Strings("one", "two", "three");
353 let s8: SomeOtherEnum = SomeOtherEnum::SomeConst2;
354 matchSomeOtherEnum(s8);
355 let s9: SomeStructEnum = SomeStructEnum::EnumStruct2{ f1: box some_fields{ field1:10 },
357 matchSomeStructEnum(s9);
359 for x in &vec![1, 2, 3] {
363 let s7: SomeEnum = SomeEnum::Strings("one", "two", "three");
364 if let SomeEnum::Strings(..) = s7 {
372 if let Some(x) = None {
377 } else if let Some(y) = None {
381 while let Some(z) = None {
386 test_rec!(q, "Hello");
391 fn foo_foo(_: i32) {}
393 impl Iterator for nofields {
394 type Item = (usize, usize);
396 fn next(&mut self) -> Option<(usize, usize)> {
400 fn size_hint(&self) -> (usize, Option<usize>) {
409 struct CharEqPattern;
411 impl<'a> Pattern<'a> for CharEqPattern {
412 type Searcher = CharEqPattern;
415 struct CharSearcher<'a>(<CharEqPattern as Pattern<'a>>::Searcher);
420 impl Error + 'static {
421 pub fn is<T: Error + 'static>(&self) -> bool {
426 impl Error + 'static + Send {
427 pub fn is<T: Error + 'static>(&self) -> bool {
428 <Error + 'static>::is::<T>(self)
431 extern crate serialize;
432 #[derive(Clone, Copy, Hash, Encodable, Decodable, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
433 struct AllDerives(i32);
435 fn test_format_args() {
438 let name = "Joe Blogg";
439 println!("Hello {}", name);
440 print!("Hello {0}", name);
441 print!("{0} + {} = {}", x, y);
442 print!("x is {}, y is {1}, name is {n}", x, y, n = name);
452 struct SilenceGenerator;
454 impl Iterator for SilenceGenerator {
455 type Item = FrameBuffer;
457 fn next(&mut self) -> Option<Self::Item> {
463 type Bar = FrameBuffer;
466 #[doc(include="extra-docs.md")]
467 struct StructWithDocs;