]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/ufcs-polymorphic-paths.rs
1197e7c1414eb8f3e2a167301a38336e73c4ffa5
[rust.git] / src / test / run-pass / ufcs-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(collections, rand)]
12
13 use std::borrow::{Cow, IntoCow};
14 use std::collections::BitVec;
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: usize) -> bool { x % 2 == 1 }
31 fn dummy_rng() -> DummyRng { DummyRng::new_unseeded() }
32
33 trait Size: Sized {
34     fn size() -> usize { 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(i32) -> i32, (5);
51     id::<i32>, fn(i32) -> i32, (5);
52
53     // Enum variant constructor.
54     Some, fn(i32) -> Option<i32>, (5);
55     Some::<i32>, fn(i32) -> Option<i32>, (5);
56
57     // Tuple struct constructor.
58     Newt, fn(i32) -> Newt<i32>, (5);
59     Newt::<i32>, fn(i32) -> Newt<i32>, (5);
60
61     // Inherent static methods.
62     Vec::new, fn() -> Vec<()>, ();
63     Vec::<()>::new, fn() -> Vec<()>, ();
64     <Vec<()>>::new, fn() -> Vec<()>, ();
65     Vec::with_capacity, fn(usize) -> Vec<()>, (5);
66     Vec::<()>::with_capacity, fn(usize) -> Vec<()>, (5);
67     <Vec<()>>::with_capacity, fn(usize) -> Vec<()>, (5);
68     BitVec::from_fn, fn(usize, fn(usize) -> bool) -> BitVec, (5, odd);
69     BitVec::from_fn::<fn(usize) -> bool>, fn(usize, fn(usize) -> bool) -> BitVec, (5, odd);
70
71     // Inherent non-static method.
72     Vec::map_in_place, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>, (vec![b'f', b'o', b'o'], u8_as_i8);
73     Vec::map_in_place::<i8, fn(u8) -> i8>, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>,
74         (vec![b'f', b'o', b'o'], u8_as_i8);
75     // FIXME these break with "type parameter might not appear here pointing at `<u8>`.
76     // Vec::<u8>::map_in_place: fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>
77     //    , (vec![b'f', b'o', b'o'], u8_as_i8);
78     // Vec::<u8>::map_in_place::<i8, fn(u8) -> i8>: fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>
79     //    , (vec![b'f', b'o', b'o'], u8_as_i8);
80
81     // Trait static methods.
82     bool::size, fn() -> usize, ();
83     <bool>::size, fn() -> usize, ();
84     <bool as Size>::size, fn() -> usize, ();
85
86     Default::default, fn() -> i32, ();
87     i32::default, fn() -> i32, ();
88     <i32>::default, fn() -> i32, ();
89     <i32 as Default>::default, fn() -> i32, ();
90
91     Rand::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
92     i32::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
93     <i32>::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
94     <i32 as Rand>::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
95     Rand::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
96     i32::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
97     <i32>::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
98     <i32 as Rand>::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
99
100     // Trait non-static methods.
101     Clone::clone, fn(&i32) -> i32, (&5);
102     i32::clone, fn(&i32) -> i32, (&5);
103     <i32>::clone, fn(&i32) -> i32, (&5);
104     <i32 as Clone>::clone, fn(&i32) -> i32, (&5);
105
106     FromIterator::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
107     Vec::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
108     <Vec<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
109     <Vec<_> as FromIterator<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>,
110         (Some(5).into_iter());
111     <Vec<i32> as FromIterator<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>,
112         (Some(5).into_iter());
113     FromIterator::from_iter::<OptionIter<i32>>, fn(OptionIter<i32>) -> Vec<i32>,
114         (Some(5).into_iter());
115     <Vec<i32> as FromIterator<_>>::from_iter::<OptionIter<i32>>, fn(OptionIter<i32>) -> Vec<i32>,
116         (Some(5).into_iter());
117
118     Add::add, fn(i32, i32) -> i32, (5, 6);
119     i32::add, fn(i32, i32) -> i32, (5, 6);
120     <i32>::add, fn(i32, i32) -> i32, (5, 6);
121     <i32 as Add<_>>::add, fn(i32, i32) -> i32, (5, 6);
122     <i32 as Add<i32>>::add, fn(i32, i32) -> i32, (5, 6);
123
124     String::into_cow, fn(String) -> Cow<'static, str>,
125         ("foo".to_string());
126     <String>::into_cow, fn(String) -> Cow<'static, str>,
127         ("foo".to_string());
128     <String as IntoCow<_>>::into_cow, fn(String) -> Cow<'static, str>,
129         ("foo".to_string());
130     <String as IntoCow<'static, _>>::into_cow, fn(String) -> Cow<'static, str>,
131         ("foo".to_string());
132 }