]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/associated-types-conditional-dispatch.rs
Merge pull request #20903 from XMPPwocky/deadlink1
[rust.git] / src / test / run-pass / associated-types-conditional-dispatch.rs
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
11 // Test that we evaluate projection predicates to winnow out
12 // candidates during trait selection and method resolution (#20296).
13 // If we don't properly winnow out candidates based on the output type
14 // `Target=[A]`, then the impl marked with `(*)` is seen to conflict
15 // with all the others.
16
17 use std::ops::Deref;
18
19 pub trait MyEq<U: ?Sized=Self> {
20     fn eq(&self, u: &U) -> bool;
21 }
22
23 impl<A, B> MyEq<[B]> for [A]
24     where A : MyEq<B>
25 {
26     fn eq(&self, other: &[B]) -> bool {
27         self.len() == other.len() &&
28             self.iter().zip(other.iter())
29                        .all(|(a, b)| MyEq::eq(a, b))
30     }
31 }
32
33 // (*) This impl conflicts with everything unless the `Target=[A]`
34 // constraint is considered.
35 impl<'a, A, B, Lhs> MyEq<[B; 0]> for Lhs
36     where A: MyEq<B>, Lhs: Deref<Target=[A]>
37 {
38     fn eq(&self, other: &[B; 0]) -> bool {
39         MyEq::eq(&**self, other.as_slice())
40     }
41 }
42
43 struct DerefWithHelper<H, T> {
44     pub helper: H
45 }
46
47 trait Helper<T> {
48     fn helper_borrow(&self) -> &T;
49 }
50
51 impl<T> Helper<T> for Option<T> {
52     fn helper_borrow(&self) -> &T {
53         self.as_ref().unwrap()
54     }
55 }
56
57 impl<T, H: Helper<T>> Deref for DerefWithHelper<H, T> {
58     type Target = T;
59
60     fn deref(&self) -> &T {
61         self.helper.helper_borrow()
62     }
63 }
64
65 pub fn check<T: MyEq>(x: T, y: T) -> bool {
66     let d: DerefWithHelper<Option<T>, T> = DerefWithHelper { helper: Some(x) };
67     d.eq(&y)
68 }
69
70 pub fn main() {
71 }