]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/associated-types-conditional-dispatch.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.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 // pretty-expanded FIXME #23616
18
19 use std::marker::PhantomData;
20 use std::ops::Deref;
21
22 pub trait MyEq<U: ?Sized=Self> {
23 fn eq(&self, u: &U) -> bool;
24 }
25
26 impl<A, B> MyEq<[B]> for [A]
27 where A : MyEq<B>
28 {
29 fn eq(&self, other: &[B]) -> bool {
30 self.len() == other.len() &&
31 self.iter().zip(other).all(|(a, b)| MyEq::eq(a, b))
32 }
33 }
34
35 // (*) This impl conflicts with everything unless the `Target=[A]`
36 // constraint is considered.
37 impl<'a, A, B, Lhs> MyEq<[B; 0]> for Lhs
38 where A: MyEq<B>, Lhs: Deref<Target=[A]>
39 {
40 fn eq(&self, other: &[B; 0]) -> bool {
41 MyEq::eq(&**self, other)
42 }
43 }
44
45 struct DerefWithHelper<H, T> {
46 pub helper: H,
47 pub marker: PhantomData<T>,
48 }
49
50 trait Helper<T> {
51 fn helper_borrow(&self) -> &T;
52 }
53
54 impl<T> Helper<T> for Option<T> {
55 fn helper_borrow(&self) -> &T {
56 self.as_ref().unwrap()
57 }
58 }
59
60 impl<T, H: Helper<T>> Deref for DerefWithHelper<H, T> {
61 type Target = T;
62
63 fn deref(&self) -> &T {
64 self.helper.helper_borrow()
65 }
66 }
67
68 pub fn check<T: MyEq>(x: T, y: T) -> bool {
69 let d: DerefWithHelper<Option<T>, T> = DerefWithHelper { helper: Some(x),
70 marker: PhantomData };
71 d.eq(&y)
72 }
73
74 pub fn main() {
75 }