]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/autoderef-privacy.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / run-pass / autoderef-privacy.rs
1 // Copyright 2016 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 // Check we do not select a private method or field when computing autoderefs
12
13 #![allow(unused)]
14
15 #[derive(Default)]
16 pub struct Bar2 { i: i32 }
17 #[derive(Default)]
18 pub struct Baz2(i32);
19
20 impl Bar2 {
21 fn f(&self) -> bool { true }
22 }
23
24 mod foo {
25 #[derive(Default)]
26 pub struct Bar { i: ::Bar2 }
27 #[derive(Default)]
28 pub struct Baz(::Baz2);
29
30 impl Bar {
31 fn f(&self) -> bool { false }
32 }
33
34 impl ::std::ops::Deref for Bar {
35 type Target = ::Bar2;
36 fn deref(&self) -> &::Bar2 { &self.i }
37 }
38
39 impl ::std::ops::Deref for Baz {
40 type Target = ::Baz2;
41 fn deref(&self) -> &::Baz2 { &self.0 }
42 }
43
44 pub fn f(bar: &Bar, baz: &Baz) {
45 // Since the private fields and methods are visible here, there should be no autoderefs.
46 let _: &::Bar2 = &bar.i;
47 let _: &::Baz2 = &baz.0;
48 assert!(!bar.f());
49 }
50 }
51
52 fn main() {
53 let bar = foo::Bar::default();
54 let baz = foo::Baz::default();
55 foo::f(&bar, &baz);
56
57 let _: i32 = bar.i;
58 let _: i32 = baz.0;
59 assert!(bar.f());
60 }