Skip to content
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

Suboptimal error message when matching on a tuple struct using a private constructor #124569

Open
JarredAllen opened this issue Apr 30, 2024 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@JarredAllen
Copy link
Contributor

Code

pub mod foo {
    pub struct Foo(u8);
}

pub fn bar(foo: Option<foo::Foo>) {
    match foo {
        // Some(foo::Foo{..}) => println!("Foo"),
        Some(foo::Foo(..)) => println!("Foo"),
        None => println!("No Foo"),
    }
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0603]: tuple struct constructor `Foo` is private
 --> src/lib.rs:8:19
  |
2 |     pub struct Foo(u8);
  |                    -- a constructor is private if any of the fields is private
...
8 |         Some(foo::Foo(..)) => println!("Foo"),
  |                   ^^^ private tuple struct constructor
  |
note: the tuple struct constructor `Foo` is defined here
 --> src/lib.rs:2:5
  |
2 |     pub struct Foo(u8);
  |     ^^^^^^^^^^^^^^^^^^^
help: consider making the field publicly accessible
  |
2 |     pub struct Foo(pub u8);
  |                    +++

For more information about this error, try `rustc --explain E0603`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

Compiling playground v0.0.1 (/playground)
error[E0603]: tuple struct constructor `Foo` is private
 --> src/lib.rs:8:19
  |
2 |     pub struct Foo(u8);
  |                    -- a constructor is private if any of the fields is private
...
8 |         Some(foo::Foo(..)) => println!("Foo"),
  |                   ^^^ private tuple struct constructor
  |
note: the tuple struct constructor `Foo` is defined here
 --> src/lib.rs:2:5
  |
2 |     pub struct Foo(u8);
  |     ^^^^^^^^^^^^^^^^^^^
help: consider making the field publicly accessible
  |
2 |     pub struct Foo(pub u8);
  |                    +++
help: consider matching via `{ .. }` braces instead of the constructor
  |
8 |         Some(foo::Foo { .. }) => println!("Foo")
  |                       ++++++

For more information about this error, try `rustc --explain E0603`.
error: could not compile `playground` (lib) due to 1 previous error

Rationale and extra context

I've used Rust for a while, and I just discovered that this is an option that lets you match on tuple structs with private fields. It would be great if the compiler could recommend this as a solution.

Other cases

If the `foo::Foo` comes from an external dependency, it does not suggest making the field publicly accessible (makes sense, since you likely can't change the dependency), but no suggestions are provided. I think it's more important to include my proposed new help text in that case, so a solution is provided, but still net beneficial in the above case.

Rust Version

Using [the Playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021)'s current nightly release:

1.80.0-nightly (2024-04-29 a8a1d3a771850e1e364e)

(sorry, I don't know how to get the playground to show the full verbose version output)

Anything else?

No response

@JarredAllen JarredAllen added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 30, 2024
@workingjubilee
Copy link
Contributor

hm. It's not clear to me why the version you initially went with isn't permitted.

@JarredAllen
Copy link
Contributor Author

hm. It's not clear to me why the version you initially went with isn't permitted.

I'm not an expert, so I might be wrong, but my guess is that falls out from it being a minor SemVer change to convert between tuple and normal structs if all fields are private.

@workingjubilee
Copy link
Contributor

That seems plausible. The usual semantic of .. in this case is to ignore all other subfields, however, so I guess what I'm really asking is why we can't match on structs with Struct(..) in general.

@workingjubilee
Copy link
Contributor

workingjubilee commented May 1, 2024

I suppose we could want some immediately-adjacent syntactic space for variadics that would block off this being a logical conclusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants