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