]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/const-polymorphic-paths.rs
rollup merge of #21151: brson/beta
[rust.git] / src / test / run-pass / const-polymorphic-paths.rs
1 // Copyright 2014 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 #![feature(macro_rules)]
12
13 use std::borrow::{Cow, IntoCow};
14 use std::collections::Bitv;
15 use std::default::Default;
16 use std::iter::FromIterator;
17 use std::ops::Add;
18 use std::option::IntoIter as OptionIter;
19 use std::rand::Rand;
20 use std::rand::XorShiftRng as DummyRng;
21 // FIXME the glob std::prelude::*; import of Vec is missing non-static inherent methods.
22 use std::vec::Vec;
23
24 #[derive(PartialEq, Eq)]
25 struct Newt<T>(T);
26
27 fn id<T>(x: T) -> T { x }
28 fn eq<T: Eq>(a: T, b: T) -> bool { a == b }
29 fn u8_as_i8(x: u8) -> i8 { x as i8 }
30 fn odd(x: uint) -> bool { x % 2 == 1 }
31 fn dummy_rng() -> DummyRng { DummyRng::new_unseeded() }
32
33 trait Size: Sized {
34     fn size() -> uint { std::mem::size_of::<Self>() }
35 }
36 impl<T> Size for T {}
37
38 macro_rules! tests {
39     ($($expr:expr, $ty:ty, ($($test:expr),*);)+) => (pub fn main() {$({
40         const C: $ty = $expr;
41         static S: $ty = $expr;
42         assert!(eq(C($($test),*), $expr($($test),*)));
43         assert!(eq(S($($test),*), $expr($($test),*)));
44         assert!(eq(C($($test),*), S($($test),*)));
45     })+})
46 }
47
48 tests! {
49     // Free function.
50     id, fn(int) -> int, (5);
51     id::<int>, fn(int) -> int, (5);
52
53     // Enum variant constructor.
54     Some, fn(int) -> Option<int>, (5);
55     Some::<int>, fn(int) -> Option<int>, (5);
56
57     // Tuple struct constructor.
58     Newt, fn(int) -> Newt<int>, (5);
59     Newt::<int>, fn(int) -> Newt<int>, (5);
60
61     // Inherent static methods.
62     Vec::new, fn() -> Vec<()>, ();
63     Vec::<()>::new, fn() -> Vec<()>, ();
64     Vec::with_capacity, fn(uint) -> Vec<()>, (5);
65     Vec::<()>::with_capacity, fn(uint) -> Vec<()>, (5);
66     Bitv::from_fn, fn(uint, fn(uint) -> bool) -> Bitv, (5, odd);
67     Bitv::from_fn::<fn(uint) -> bool>, fn(uint, fn(uint) -> bool) -> Bitv, (5, odd);
68
69     // Inherent non-static method.
70     Vec::map_in_place, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>, (vec![b'f', b'o', b'o'], u8_as_i8);
71     Vec::map_in_place::<i8, fn(u8) -> i8>, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>,
72         (vec![b'f', b'o', b'o'], u8_as_i8);
73     // FIXME these break with "type parameter might not appear here pointing at `<u8>`.
74     // Vec::<u8>::map_in_place: fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>
75     //    , (vec![b'f', b'o', b'o'], u8_as_i8);
76     // Vec::<u8>::map_in_place::<i8, fn(u8) -> i8>: fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>
77     //    , (vec![b'f', b'o', b'o'], u8_as_i8);
78
79     // Trait static methods.
80     <bool as Size>::size, fn() -> uint, ();
81     Default::default, fn() -> int, ();
82     <int as Default>::default, fn() -> int, ();
83     Rand::rand, fn(&mut DummyRng) -> int, (&mut dummy_rng());
84     <int as Rand>::rand, fn(&mut DummyRng) -> int, (&mut dummy_rng());
85     Rand::rand::<DummyRng>, fn(&mut DummyRng) -> int, (&mut dummy_rng());
86     <int as Rand>::rand::<DummyRng>, fn(&mut DummyRng) -> int, (&mut dummy_rng());
87
88     // Trait non-static methods.
89     Clone::clone, fn(&int) -> int, (&5);
90     <int as Clone>::clone, fn(&int) -> int, (&5);
91     FromIterator::from_iter, fn(OptionIter<int>) -> Vec<int>, (Some(5).into_iter());
92     <Vec<_> as FromIterator<_>>::from_iter, fn(OptionIter<int>) -> Vec<int>,
93         (Some(5).into_iter());
94     <Vec<int> as FromIterator<_>>::from_iter, fn(OptionIter<int>) -> Vec<int>,
95         (Some(5).into_iter());
96     FromIterator::from_iter::<OptionIter<int>>, fn(OptionIter<int>) -> Vec<int>,
97         (Some(5).into_iter());
98     <Vec<int> as FromIterator<_>>::from_iter::<OptionIter<int>>, fn(OptionIter<int>) -> Vec<int>,
99         (Some(5).into_iter());
100     Add::add, fn(i32, i32) -> i32, (5, 6);
101     <i32 as Add<_>>::add, fn(i32, i32) -> i32, (5, 6);
102     <i32 as Add<i32>>::add, fn(i32, i32) -> i32, (5, 6);
103     <String as IntoCow<_, _>>::into_cow, fn(String) -> Cow<'static, String, str>,
104         ("foo".to_string());
105     <String as IntoCow<'static, _, _>>::into_cow, fn(String) -> Cow<'static, String, str>,
106         ("foo".to_string());
107 }