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