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

.. should be suggested when attempting to ignore a private enum variant with _ #135903

Open
mplanchard opened this issue Jan 22, 2025 · 0 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

@mplanchard
Copy link

mplanchard commented Jan 22, 2025

Code

mod foo {
    struct Bar;
    pub enum Foo {
        #[allow(private_interfaces)]
        A(Bar),
    }
}
fn foo_bar(v: foo::Foo) {
    match v {
        foo::Foo::A(_) => {}
    }
}

Current output

rustc: error: type `jobs::purge_old_event_summaries::foo::Bar` is private
   --> platform/spec-proxy-sidekick/src/jobs/purge_old_event_summaries.rs:454:21
    |
454 |         foo::Foo::A(_) => {}
    |                     ^ private type

Desired output

rustc: you might want to ignore the private value by using `..`

Rationale and extra context

On ignoring a pattern in a match, the reference reads:

There are a few ways to ignore entire values or parts of values in a pattern: using the _ pattern (which you’ve seen), using the _ pattern within another pattern, using a name that starts with an underscore, or using .. to ignore remaining parts of a value. Let’s explore how and why to use each of these patterns.

Their detailed sections read, in part:

We can also use _ inside another pattern to ignore just part of a value, for example, when we want to test for only part of a value but have no use for the other parts in the corresponding code we want to run.

and

With values that have many parts, we can use the .. syntax to use specific parts and ignore the rest, avoiding the need to list underscores for each ignored value. The .. pattern ignores any parts of a value that we haven’t explicitly matched in the rest of the pattern.

All of this makes ignoring a value via _ and .. seem functionally equivalent, and generally this is true, except for when you are ignoring a value whose type is private. In that case, _ causes an error, while .. is fine.

I would have assumed that the compiler was able to entirely ignore your "use" of the private type in either case, but given that it isn't so, it would be nice to have a helpful message pointing in that direction. It seems like _ sufficiently expresses the intent to ignore the private value such that it would justify suggesting .. as an alternative. This would be a reasonable suggestion for 1-tuple data enums and for N-tuple data enums, where assigning a private type to _ can also be replaced with ...

A real world example of where this would have been useful can be seen here: mlua-rs/mlua#502, where I opened an Issue with the mlua project asking for a workaround to allow exhaustive pattern matching in a public enum that had been updated to contain a private type. The library was updated to make the type public but undocumented, but another commenter later pointed out that .. would have also been an option.

It would also be nice to provide similar hints for structs, for example:

mod foo {
    struct Bar;
    pub struct Baz {
        #[allow(private_interfaces)]
        pub a: Bar,
    }
}
fn struct_baz(v: foo::Baz) {
    let foo::Baz { a: _ } = v;
}

In addition to the current error of:

rustc: error: type `jobs::purge_old_event_summaries::foo::Bar` is private
   --> platform/spec-proxy-sidekick/src/jobs/purge_old_event_summaries.rs:467:23
    |
467 |     let foo::Baz { a: _ } = v;
    |                       ^ private type

To add something like: "you might want to ignore the field 'a' entirely by using .."

Other cases

Rust Version

❯ rustc --version --verbose
rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: x86_64-unknown-linux-gnu
release: 1.84.0
LLVM version: 19.1.5

Anything else?

No response

@mplanchard mplanchard 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 Jan 22, 2025
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

1 participant