Access the non-public function from object created by default().
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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