]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/existential_type.rs
Auto merge of #61361 - estebank:infer-type, r=varkor
[rust.git] / src / test / run-pass / existential_type.rs
1 #![allow(dead_code)]
2 #![allow(unused_assignments)]
3 #![allow(unused_variables)]
4 #![feature(existential_type)]
5
6 fn main() {
7     assert_eq!(foo().to_string(), "foo");
8     assert_eq!(bar1().to_string(), "bar1");
9     assert_eq!(bar2().to_string(), "bar2");
10     let mut x = bar1();
11     x = bar2();
12     assert_eq!(boo::boo().to_string(), "boo");
13     assert_eq!(my_iter(42u8).collect::<Vec<u8>>(), vec![42u8]);
14 }
15
16 // single definition
17 existential type Foo: std::fmt::Display;
18
19 fn foo() -> Foo {
20     "foo"
21 }
22
23 // two definitions
24 existential type Bar: std::fmt::Display;
25
26 fn bar1() -> Bar {
27     "bar1"
28 }
29
30 fn bar2() -> Bar {
31     "bar2"
32 }
33
34 // definition in submodule
35 existential type Boo: std::fmt::Display;
36
37 mod boo {
38     pub fn boo() -> super::Boo {
39         "boo"
40     }
41 }
42
43 existential type MyIter<T>: Iterator<Item = T>;
44
45 fn my_iter<T>(t: T) -> MyIter<T> {
46     std::iter::once(t)
47 }
48
49 fn my_iter2<T>(t: T) -> MyIter<T> {
50     std::iter::once(t)
51 }
52
53 // param names should not have an effect!
54 fn my_iter3<U>(u: U) -> MyIter<U> {
55     std::iter::once(u)
56 }
57
58 // param position should not have an effect!
59 fn my_iter4<U, V>(_: U, v: V) -> MyIter<V> {
60     std::iter::once(v)
61 }
62
63 // param names should not have an effect!
64 existential type MyOtherIter<T>: Iterator<Item = T>;
65
66 fn my_other_iter<U>(u: U) -> MyOtherIter<U> {
67     std::iter::once(u)
68 }
69
70 trait Trait {}
71 existential type GenericBound<'a, T: Trait>: Sized + 'a;
72
73 fn generic_bound<'a, T: Trait + 'a>(t: T) -> GenericBound<'a, T> {
74     t
75 }
76
77 mod pass_through {
78     pub existential type Passthrough<T>: Sized + 'static;
79
80     fn define_passthrough<T: 'static>(t: T) -> Passthrough<T> {
81         t
82     }
83 }
84
85 fn use_passthrough(x: pass_through::Passthrough<u32>) -> pass_through::Passthrough<u32> {
86     x
87 }