rust-lang / rust-lang/rust

Destructuring tuple structs with public leading fields and private trailing fields by tuple syntax is disallowed

Open
#139,972 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Minimal reproducible example
mod foo {
    pub struct Bar(pub i32, ());
}

fn get(foo::Bar(i, ..): foo::Bar) {
    dbg!(i);
}
Current behavior
error[E0603]: tuple struct constructor `Bar` is private
 --> src/main.rs:6:13
  |
3 |     pub struct Bar(pub i32, ());
  |                    ----------- a constructor is private if any of the fields is private
...
6 | fn get(foo::Bar(i, ..): foo::Bar) { dbg!(i); }
  |             ^^^ private tuple struct constructor
  |
note: the tuple struct constructor `Bar` is defined here
 --> src/main.rs:3:5
  |
3 |     pub struct Bar(pub i32, ());
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider making the fields publicly accessible
  |
3 |     pub struct Bar(pub i32, pub ());
  |                             +++

For more information about this error, try `rustc --explain E0603`.
Expected behvaior

This should be allowed to compile, since it is unambiguous that the first element of the tuple struct is a public field, regardless of the number of subsequent fields. The tuple struct destructuring syntax

foo::Bar(i, ..)

implies the exact same conditions as the following pattern:

foo::Bar { 0: i, .. }

Both of them imply:

  • foo::Bar MUST be a tuple struct
  • foo::Bar MUST have a visible field .0
  • foo::Bar MAY have non-exhaustive fields after .0, which we do not need to access

However, the foo:: Bar {0: i, ..} case compiles, while foo::Bar(i, ..) does not.

Proposed fix

If the first 3 fields of a tuple struct are visible, users should be allowed to match a value of the tuple struct by Path(_, ..), Path(_, _, ..) or Path(_, _, _, ..), where _ are placeholders for arbitrary patterns.

Meta

rustc --version --verbose:

rustc 1.87.0-beta.1 (45165c82a 2025-04-01)

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 minimal Rust reproducer with the noted rustc version and compare the failing tuple pattern with the working struct-pattern form. Trace the compiler's visibility handling for tuple-struct patterns, then confirm that visible leading fields with private trailing fields compile and add coverage for the demonstrated cases.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.