Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
SOF3 opened this issue Apr 17, 2025 · 0 comments
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.

Comments

@SOF3
Copy link
Contributor

SOF3 commented Apr 17, 2025

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)
@SOF3 SOF3 added the C-bug Category: This is a bug. label Apr 17, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.
Projects
None yet
Development

No branches or pull requests

2 participants