]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/ufcs-polymorphic-paths.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.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
12 #![feature(collections, rand, into_cow, map_in_place, bitvec)]
13 #![allow(warnings)]
14
15 use std::borrow::{Cow, IntoCow};
16 use std::collections::BitVec;
17 use std::default::Default;
18 use std::iter::FromIterator;
19 use std::ops::Add;
20 use std::option::IntoIter as OptionIter;
21
22 pub struct XorShiftRng;
23 use XorShiftRng as DummyRng;
24 impl Rng for XorShiftRng {}
25 pub trait Rng {}
26 pub trait Rand: Default + Sized {
27 fn rand<R: Rng>(rng: &mut R) -> Self { Default::default() }
28 }
29 impl Rand for i32 { }
30
31 #[derive(PartialEq, Eq)]
32 struct Newt<T>(T);
33
34 fn id<T>(x: T) -> T { x }
35 fn eq<T: Eq>(a: T, b: T) -> bool { a == b }
36 fn u8_as_i8(x: u8) -> i8 { x as i8 }
37 fn odd(x: usize) -> bool { x % 2 == 1 }
38 fn dummy_rng() -> DummyRng { XorShiftRng }
39
40 trait Size: Sized {
41 fn size() -> usize { std::mem::size_of::<Self>() }
42 }
43 impl<T> Size for T {}
44
45 macro_rules! tests {
46 ($($expr:expr, $ty:ty, ($($test:expr),*);)+) => (pub fn main() {$({
47 const C: $ty = $expr;
48 static S: $ty = $expr;
49 assert!(eq(C($($test),*), $expr($($test),*)));
50 assert!(eq(S($($test),*), $expr($($test),*)));
51 assert!(eq(C($($test),*), S($($test),*)));
52 })+})
53 }
54
55 tests! {
56 // Free function.
57 id, fn(i32) -> i32, (5);
58 id::<i32>, fn(i32) -> i32, (5);
59
60 // Enum variant constructor.
61 Some, fn(i32) -> Option<i32>, (5);
62 Some::<i32>, fn(i32) -> Option<i32>, (5);
63
64 // Tuple struct constructor.
65 Newt, fn(i32) -> Newt<i32>, (5);
66 Newt::<i32>, fn(i32) -> Newt<i32>, (5);
67
68 // Inherent static methods.
69 Vec::new, fn() -> Vec<()>, ();
70 Vec::<()>::new, fn() -> Vec<()>, ();
71 <Vec<()>>::new, fn() -> Vec<()>, ();
72 Vec::with_capacity, fn(usize) -> Vec<()>, (5);
73 Vec::<()>::with_capacity, fn(usize) -> Vec<()>, (5);
74 <Vec<()>>::with_capacity, fn(usize) -> Vec<()>, (5);
75 BitVec::from_fn, fn(usize, fn(usize) -> bool) -> BitVec, (5, odd);
76 BitVec::from_fn::<fn(usize) -> bool>, fn(usize, fn(usize) -> bool) -> BitVec, (5, odd);
77
78 // Inherent non-static method.
79 Vec::map_in_place, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>, (vec![b'f', b'o', b'o'], u8_as_i8);
80 Vec::map_in_place::<i8, fn(u8) -> i8>, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>,
81 (vec![b'f', b'o', b'o'], u8_as_i8);
82 Vec::<u8>::map_in_place, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>
83 , (vec![b'f', b'o', b'o'], u8_as_i8);
84 Vec::<u8>::map_in_place::<i8, fn(u8) -> i8>, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>
85 , (vec![b'f', b'o', b'o'], u8_as_i8);
86
87 // Trait static methods.
88 bool::size, fn() -> usize, ();
89 <bool>::size, fn() -> usize, ();
90 <bool as Size>::size, fn() -> usize, ();
91
92 Default::default, fn() -> i32, ();
93 i32::default, fn() -> i32, ();
94 <i32>::default, fn() -> i32, ();
95 <i32 as Default>::default, fn() -> i32, ();
96
97 Rand::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
98 i32::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
99 <i32>::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
100 <i32 as Rand>::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
101 Rand::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
102 i32::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
103 <i32>::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
104 <i32 as Rand>::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
105
106 // Trait non-static methods.
107 Clone::clone, fn(&i32) -> i32, (&5);
108 i32::clone, fn(&i32) -> i32, (&5);
109 <i32>::clone, fn(&i32) -> i32, (&5);
110 <i32 as Clone>::clone, fn(&i32) -> i32, (&5);
111
112 FromIterator::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
113 Vec::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
114 <Vec<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
115 <Vec<_> as FromIterator<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>,
116 (Some(5).into_iter());
117 <Vec<i32> as FromIterator<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>,
118 (Some(5).into_iter());
119 FromIterator::from_iter::<OptionIter<i32>>, fn(OptionIter<i32>) -> Vec<i32>,
120 (Some(5).into_iter());
121 <Vec<i32> as FromIterator<_>>::from_iter::<OptionIter<i32>>, fn(OptionIter<i32>) -> Vec<i32>,
122 (Some(5).into_iter());
123
124 Add::add, fn(i32, i32) -> i32, (5, 6);
125 i32::add, fn(i32, i32) -> i32, (5, 6);
126 <i32>::add, fn(i32, i32) -> i32, (5, 6);
127 <i32 as Add<_>>::add, fn(i32, i32) -> i32, (5, 6);
128 <i32 as Add<i32>>::add, fn(i32, i32) -> i32, (5, 6);
129
130 String::into_cow, fn(String) -> Cow<'static, str>,
131 ("foo".to_string());
132 <String>::into_cow, fn(String) -> Cow<'static, str>,
133 ("foo".to_string());
134 <String as IntoCow<_>>::into_cow, fn(String) -> Cow<'static, str>,
135 ("foo".to_string());
136 <String as IntoCow<'static, _>>::into_cow, fn(String) -> Cow<'static, str>,
137 ("foo".to_string());
138 }