rust-lang / rust-lang/rust

Access the non-public function from object created by default().

Open
#130,096 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-discussion T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The following code can successfully compile.

#![allow(unused)]

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

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

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

    impl Bar {
        // here this function ,  
        fn f(&self) -> bool { false }
    }

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

fn main() {
    let bar = foo::Bar::default(); 
    print!("{}",bar.f()); // true
}

Here it print 'true', which I think it is the default implementation of function f.
The function f is not a public function?

And if I comment these lines of codes.

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

Then the program is not compilable.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided example with and without the Deref implementation, then inspect how method resolution handles the private f methods and the dereferenced Bar2 value. Confirm whether the observed access is expected Rust behavior or a compiler bug, and document the result with a focused regression test if a compiler change is needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.