]> git.lizzy.rs Git - rust.git/blob - src/test/ui/existential_types/existential_type.rs
6824d362049777bd0ce2f5d3dfae28a1b9111229
[rust.git] / src / test / ui / existential_types / existential_type.rs
1 // Copyright 2018 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
12 #![feature(existential_type)]
13
14 fn main() {}
15
16 // two definitions with different types
17 existential type Foo: std::fmt::Debug;
18
19 fn foo() -> Foo {
20     ""
21 }
22
23 fn bar() -> Foo { //~ ERROR defining existential type use differs from previous
24     42i32
25 }
26
27 // declared but never defined
28 existential type Bar: std::fmt::Debug; //~ ERROR could not find defining uses
29
30 mod boo {
31     // declared in module but not defined inside of it
32     pub existential type Boo: ::std::fmt::Debug; //~ ERROR could not find defining uses
33 }
34
35 fn bomp() -> boo::Boo {
36     "" //~ ERROR mismatched types
37 }
38
39 mod boo2 {
40     mod boo {
41         pub existential type Boo: ::std::fmt::Debug;
42         fn bomp() -> Boo {
43             ""
44         }
45     }
46
47     // don't actually know the type here
48
49     fn bomp2() {
50         let _: &str = bomp(); //~ ERROR mismatched types
51     }
52
53     fn bomp() -> boo::Boo {
54         "" //~ ERROR mismatched types
55     }
56 }
57
58 // generics
59
60 trait Trait {}
61 existential type Underconstrained<T: Trait>: 'static; //~ ERROR the trait bound `T: Trait`
62
63 // no `Trait` bound
64 fn underconstrain<T>(_: T) -> Underconstrained<T> {
65     unimplemented!()
66 }
67
68 existential type MyIter<T>: Iterator<Item = T>;
69
70 fn my_iter<T>(t: T) -> MyIter<T> {
71     std::iter::once(t)
72 }
73
74 fn my_iter2<T>(t: T) -> MyIter<T> { //~ ERROR defining existential type use differs from previous
75     Some(t).into_iter()
76 }
77
78 existential type WrongGeneric<T>: 'static;
79 //~^ ERROR the parameter type `T` may not live long enough
80
81 fn wrong_generic<T>(t: T) -> WrongGeneric<T> {
82     t
83 }
84
85 // don't reveal the concrete type
86 existential type NoReveal: std::fmt::Debug;
87
88 fn define_no_reveal() -> NoReveal {
89     ""
90 }
91
92 fn no_reveal(x: NoReveal) {
93     let _: &'static str = x; //~ mismatched types
94     let _ = x as &'static str; //~ non-primitive cast
95 }