]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/overloaded-autoderef-order.rs
librustc: Remove the fallback to `int` from typechecking.
[rust.git] / src / test / run-pass / overloaded-autoderef-order.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 use std::rc::Rc;
12
13 struct DerefWrapper<X, Y> {
14     x: X,
15     y: Y
16 }
17
18 impl<X, Y> DerefWrapper<X, Y> {
19     fn get_x(self) -> X {
20         self.x
21     }
22 }
23
24 impl<X, Y> Deref<Y> for DerefWrapper<X, Y> {
25     fn deref<'a>(&'a self) -> &'a Y {
26         &self.y
27     }
28 }
29
30 mod priv_test {
31     pub struct DerefWrapperHideX<X, Y> {
32         x: X,
33         pub y: Y
34     }
35
36     impl<X, Y> DerefWrapperHideX<X, Y> {
37         pub fn new(x: X, y: Y) -> DerefWrapperHideX<X, Y> {
38             DerefWrapperHideX {
39                 x: x,
40                 y: y
41             }
42         }
43     }
44
45     impl<X, Y> Deref<Y> for DerefWrapperHideX<X, Y> {
46         fn deref<'a>(&'a self) -> &'a Y {
47             &self.y
48         }
49     }
50 }
51
52 pub fn main() {
53     let nested = DerefWrapper {x: true, y: DerefWrapper {x: 0i, y: 1i}};
54
55     // Use the first field that you can find.
56     assert_eq!(nested.x, true);
57     assert_eq!((*nested).x, 0);
58
59     // Same for methods, even though there are multiple
60     // candidates (at different nesting levels).
61     assert_eq!(nested.get_x(), true);
62     assert_eq!((*nested).get_x(), 0);
63
64     // Also go through multiple levels of indirection.
65     assert_eq!(Rc::new(nested).x, true);
66
67     let nested_priv = priv_test::DerefWrapperHideX::new(true, DerefWrapper {x: 0i, y: 1i});
68     // FIXME(eddyb) #12808 should skip private fields.
69     // assert_eq!(nested_priv.x, 0);
70     assert_eq!((*nested_priv).x, 0);
71 }