Skip to content

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

Open
@SOF3

Description

@SOF3

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions