Skip to content

Confusing error when using self to instantiate tuple struct with private field #147343

@lnicola

Description

@lnicola

Code

mod m {
    pub struct S(crate::P);
}

use m::S;

struct P;

impl P {
    fn foo(self) {
        // S(P); // OK, "cannot initialize a tuple struct which contains private fields"

        S(self);
    }
}

Current output

error[E0423]: expected function, tuple struct or tuple variant, found struct `S`
  --> src/main.rs:13:9
   |
13 |         S(self);
   |         ^------
   |         |
   |         help: try calling `S` as a method: `self.S()`

Desired output

error[E0423]: cannot initialize a tuple struct which contains private fields
note: constructor is not visible here due to private fields

Rationale and extra context

No response

Other cases

let s = self;
S(s); // cannot initialize a tuple struct which contains private fields

Rust Version

rustc 1.91.0-beta.3 (bb624dcb4 2025-09-20)
binary: rustc
commit-hash: bb624dcb4c8ab987e10c0808d92d76f3b84dd117
commit-date: 2025-09-20
host: x86_64-unknown-linux-gnu
release: 1.91.0-beta.3
LLVM version: 21.1.1

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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