summaryrefslogtreecommitdiffstats
path: root/src/test/ui/ufcs-polymorphic-paths.rs
blob: a14ebd6a41fc4c3fb68615735970d205a9eec4e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// run-pass

use std::borrow::{Cow, ToOwned};
use std::default::Default;
use std::iter::FromIterator;
use std::ops::Add;
use std::option::IntoIter as OptionIter;

pub struct XorShiftRng;
use XorShiftRng as DummyRng;
impl Rng for XorShiftRng {}
pub trait Rng {}
pub trait Rand: Default + Sized {
    fn rand<R: Rng>(_rng: &mut R) -> Self { Default::default() }
}
impl Rand for i32 { }

pub trait IntoCow<'a, B: ?Sized> where B: ToOwned {
    fn into_cow(self) -> Cow<'a, B>;
}

impl<'a> IntoCow<'a, str> for String {
    fn into_cow(self) -> Cow<'a, str> {
        Cow::Owned(self)
    }
}

#[derive(PartialEq, Eq)]
struct Newt<T>(T);

fn id<T>(x: T) -> T { x }
fn eq<T: Eq>(a: T, b: T) -> bool { a == b }
fn u8_as_i8(x: u8) -> i8 { x as i8 }
fn odd(x: usize) -> bool { x % 2 == 1 }
fn dummy_rng() -> DummyRng { XorShiftRng }

trait Size: Sized {
    fn size() -> usize { std::mem::size_of::<Self>() }
}
impl<T> Size for T {}

#[derive(PartialEq, Eq)]
struct BitVec;

impl BitVec {
    fn from_fn<F>(_: usize, _: F) -> BitVec where F: FnMut(usize) -> bool {
        BitVec
    }
}

#[derive(PartialEq, Eq)]
struct Foo<T>(T);

impl<T> Foo<T> {
    fn map_in_place<U, F>(self, mut f: F) -> Foo<U> where F: FnMut(T) -> U {
        Foo(f(self.0))
    }

}

macro_rules! tests {
    ($($expr:expr, $ty:ty, ($($test:expr),*);)+) => (pub fn main() {$({
        const C: $ty = $expr;
        static S: $ty = $expr;
        assert!(eq(C($($test),*), $expr($($test),*)));
        assert!(eq(S($($test),*), $expr($($test),*)));
        assert!(eq(C($($test),*), S($($test),*)));
    })+})
}

tests! {
    // Free function.
    id, fn(i32) -> i32, (5);
    id::<i32>, fn(i32) -> i32, (5);

    // Enum variant constructor.
    Some, fn(i32) -> Option<i32>, (5);
    Some::<i32>, fn(i32) -> Option<i32>, (5);

    // Tuple struct constructor.
    Newt, fn(i32) -> Newt<i32>, (5);
    Newt::<i32>, fn(i32) -> Newt<i32>, (5);

    // Inherent static methods.
    Vec::new, fn() -> Vec<()>, ();
    Vec::<()>::new, fn() -> Vec<()>, ();
    <Vec<()>>::new, fn() -> Vec<()>, ();
    Vec::with_capacity, fn(usize) -> Vec<()>, (5);
    Vec::<()>::with_capacity, fn(usize) -> Vec<()>, (5);
    <Vec<()>>::with_capacity, fn(usize) -> Vec<()>, (5);
    BitVec::from_fn, fn(usize, fn(usize) -> bool) -> BitVec, (5, odd);
    BitVec::from_fn::<fn(usize) -> bool>, fn(usize, fn(usize) -> bool) -> BitVec, (5, odd);

    // Inherent non-static method.
    Foo::map_in_place, fn(Foo<u8>, fn(u8) -> i8) -> Foo<i8>, (Foo(b'f'), u8_as_i8);
    Foo::map_in_place::<i8, fn(u8) -> i8>, fn(Foo<u8>, fn(u8) -> i8) -> Foo<i8>,
        (Foo(b'f'), u8_as_i8);
    Foo::<u8>::map_in_place, fn(Foo<u8>, fn(u8) -> i8) -> Foo<i8>
        , (Foo(b'f'), u8_as_i8);
    Foo::<u8>::map_in_place::<i8, fn(u8) -> i8>, fn(Foo<u8>, fn(u8) -> i8) -> Foo<i8>
        , (Foo(b'f'), u8_as_i8);

    // Trait static methods.
    bool::size, fn() -> usize, ();
    <bool>::size, fn() -> usize, ();
    <bool as Size>::size, fn() -> usize, ();

    Default::default, fn() -> i32, ();
    i32::default, fn() -> i32, ();
    <i32>::default, fn() -> i32, ();
    <i32 as Default>::default, fn() -> i32, ();

    Rand::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
    i32::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
    <i32>::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
    <i32 as Rand>::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
    Rand::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
    i32::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
    <i32>::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
    <i32 as Rand>::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());

    // Trait non-static methods.
    Clone::clone, fn(&i32) -> i32, (&5);
    i32::clone, fn(&i32) -> i32, (&5);
    <i32>::clone, fn(&i32) -> i32, (&5);
    <i32 as Clone>::clone, fn(&i32) -> i32, (&5);

    FromIterator::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
    Vec::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
    <Vec<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
    <Vec<_> as FromIterator<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>,
        (Some(5).into_iter());
    <Vec<i32> as FromIterator<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>,
        (Some(5).into_iter());
    FromIterator::from_iter::<OptionIter<i32>>, fn(OptionIter<i32>) -> Vec<i32>,
        (Some(5).into_iter());
    <Vec<i32> as FromIterator<_>>::from_iter::<OptionIter<i32>>, fn(OptionIter<i32>) -> Vec<i32>,
        (Some(5).into_iter());

    Add::add, fn(i32, i32) -> i32, (5, 6);
    i32::add, fn(i32, i32) -> i32, (5, 6);
    <i32>::add, fn(i32, i32) -> i32, (5, 6);
    <i32 as Add<_>>::add, fn(i32, i32) -> i32, (5, 6);
    <i32 as Add<i32>>::add, fn(i32, i32) -> i32, (5, 6);

    String::into_cow, fn(String) -> Cow<'static, str>,
        ("foo".to_string());
    <String>::into_cow, fn(String) -> Cow<'static, str>,
        ("foo".to_string());
    <String as IntoCow<_>>::into_cow, fn(String) -> Cow<'static, str>,
        ("foo".to_string());
    <String as IntoCow<'static, _>>::into_cow, fn(String) -> Cow<'static, str>,
        ("foo".to_string());
}