Skip to content

Tuple struct's private field can be accessed outside the defining module #111220

Closed
@akonradi

Description

@akonradi

I tried this code:

mod my {
    pub struct Foo(&'static str);
}

impl AsRef<str> for my::Foo {
    fn as_ref(&self) -> &str {
        let Self(s) = self;
        s
    }
}

I expected to see this happen: compilation should fail because the trait impl is defined outside of the module for the type but refers to its private field.

Instead, this happened: this compiled successfully

Meta

This reproduces on the playground for stable Rust, version 1.69.0: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=20f12a0f682d8dd6530d75f1b5e7a8dd

Metadata

Metadata

Assignees

Labels

A-visibilityArea: Visibility / privacyC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions