File: autoderef-privacy.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (51 lines) | stat: -rw-r--r-- 1,082 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//@ run-pass
// Check we do not select a private method or field when computing autoderefs

#![allow(unused)]

#[derive(Default)]
pub struct Bar2 { i: i32 }
#[derive(Default)]
pub struct Baz2(i32);

impl Bar2 {
    fn f(&self) -> bool { true }
}

mod foo {
    #[derive(Default)]
    pub struct Bar { i: ::Bar2 }
    #[derive(Default)]
    pub struct Baz(::Baz2);

    impl Bar {
        fn f(&self) -> bool { false }
    }

    impl ::std::ops::Deref for Bar {
        type Target = ::Bar2;
        fn deref(&self) -> &::Bar2 { &self.i }
    }

    impl ::std::ops::Deref for Baz {
        type Target = ::Baz2;
        fn deref(&self) -> &::Baz2 { &self.0 }
    }

    pub fn f(bar: &Bar, baz: &Baz) {
        // Since the private fields and methods are visible here, there should be no autoderefs.
        let _: &::Bar2 = &bar.i;
        let _: &::Baz2 = &baz.0;
        assert!(!bar.f());
    }
}

fn main() {
    let bar = foo::Bar::default();
    let baz = foo::Baz::default();
    foo::f(&bar, &baz);

    let _: i32 = bar.i;
    let _: i32 = baz.0;
    assert!(bar.f());
}