]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/ufcs-polymorphic-paths.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / test / run-pass / ufcs-polymorphic-paths.rs
CommitLineData
1a4d82fc
JJ
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
54a0048b 11use std::borrow::{Cow, ToOwned};
1a4d82fc
JJ
12use std::default::Default;
13use std::iter::FromIterator;
85aaf69f 14use std::ops::Add;
1a4d82fc 15use std::option::IntoIter as OptionIter;
1a4d82fc 16
9346a6ac
AL
17pub struct XorShiftRng;
18use XorShiftRng as DummyRng;
19impl Rng for XorShiftRng {}
20pub trait Rng {}
21pub trait Rand: Default + Sized {
e9174d1e 22 fn rand<R: Rng>(_rng: &mut R) -> Self { Default::default() }
9346a6ac
AL
23}
24impl Rand for i32 { }
25
54a0048b
SL
26pub trait IntoCow<'a, B: ?Sized> where B: ToOwned {
27 fn into_cow(self) -> Cow<'a, B>;
28}
29
30impl<'a> IntoCow<'a, str> for String {
31 fn into_cow(self) -> Cow<'a, str> {
32 Cow::Owned(self)
33 }
34}
35
1a4d82fc
JJ
36#[derive(PartialEq, Eq)]
37struct Newt<T>(T);
38
39fn id<T>(x: T) -> T { x }
40fn eq<T: Eq>(a: T, b: T) -> bool { a == b }
41fn u8_as_i8(x: u8) -> i8 { x as i8 }
c34b1796 42fn odd(x: usize) -> bool { x % 2 == 1 }
9346a6ac 43fn dummy_rng() -> DummyRng { XorShiftRng }
1a4d82fc 44
85aaf69f 45trait Size: Sized {
c34b1796 46 fn size() -> usize { std::mem::size_of::<Self>() }
85aaf69f
SL
47}
48impl<T> Size for T {}
49
e9174d1e
SL
50#[derive(PartialEq, Eq)]
51struct BitVec;
52
53impl BitVec {
54 fn from_fn<F>(_: usize, _: F) -> BitVec where F: FnMut(usize) -> bool {
55 BitVec
56 }
57}
58
59#[derive(PartialEq, Eq)]
60struct Foo<T>(T);
61
62impl<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
1a4d82fc
JJ
69macro_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
79tests! {
80 // Free function.
c34b1796
AL
81 id, fn(i32) -> i32, (5);
82 id::<i32>, fn(i32) -> i32, (5);
1a4d82fc
JJ
83
84 // Enum variant constructor.
c34b1796
AL
85 Some, fn(i32) -> Option<i32>, (5);
86 Some::<i32>, fn(i32) -> Option<i32>, (5);
1a4d82fc
JJ
87
88 // Tuple struct constructor.
c34b1796
AL
89 Newt, fn(i32) -> Newt<i32>, (5);
90 Newt::<i32>, fn(i32) -> Newt<i32>, (5);
1a4d82fc
JJ
91
92 // Inherent static methods.
93 Vec::new, fn() -> Vec<()>, ();
94 Vec::<()>::new, fn() -> Vec<()>, ();
c34b1796
AL
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);
1a4d82fc
JJ
101
102 // Inherent non-static method.
e9174d1e
SL
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);
1a4d82fc
JJ
110
111 // Trait static methods.
c34b1796
AL
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());
1a4d82fc
JJ
129
130 // Trait non-static methods.
c34b1796
AL
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>,
85aaf69f 140 (Some(5).into_iter());
c34b1796 141 <Vec<i32> as FromIterator<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>,
85aaf69f 142 (Some(5).into_iter());
c34b1796 143 FromIterator::from_iter::<OptionIter<i32>>, fn(OptionIter<i32>) -> Vec<i32>,
85aaf69f 144 (Some(5).into_iter());
c34b1796 145 <Vec<i32> as FromIterator<_>>::from_iter::<OptionIter<i32>>, fn(OptionIter<i32>) -> Vec<i32>,
85aaf69f 146 (Some(5).into_iter());
c34b1796 147
85aaf69f 148 Add::add, fn(i32, i32) -> i32, (5, 6);
c34b1796
AL
149 i32::add, fn(i32, i32) -> i32, (5, 6);
150 <i32>::add, fn(i32, i32) -> i32, (5, 6);
85aaf69f
SL
151 <i32 as Add<_>>::add, fn(i32, i32) -> i32, (5, 6);
152 <i32 as Add<i32>>::add, fn(i32, i32) -> i32, (5, 6);
c34b1796
AL
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());
85aaf69f
SL
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());
1a4d82fc 162}