5

lint towards rejecting consts in patterns that do not implement PartialEq by Ral...

 11 months ago
source link: https://github.com/rust-lang/rust/pull/115893
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Conversation

Member

I think we definitely don't want to allow such consts, so even while the general plan around structural matching is up in the air, we can start the process of getting non-PartialEq matches out of the ecosystem.

Collaborator

r? @b-naber

(rustbot has picked a reviewer for you, use r? to override)

rustbot

added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

labels

Sep 16, 2023

Collaborator

Some changes might have occurred in exhaustiveness checking

cc @Nadrieril

This comment has been minimized.

Member

Author

Crucially, tests/ui/rfcs/rfc-1445-restrict-constants-in-patterns/allow-use-behind-cousin-variant.rs is still accepted; this is the example where #62411 (indirect_structural_match) was initially over-eager.

RalfJung

added T-lang Relevant to the language team, which will review and decide on the PR/issue. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). I-lang-nominated Indicates that an issue has been nominated for discussion during a lang team meeting.

labels

Sep 17, 2023

Member

Author

@rust-lang/lang I found a surprising gap in our pattern-related future-incompat lints, which lead to us still accepting (without a warning) some consts as patterns that don't even implement PartialEq. I think we definitely don't want that (and @oli-obk said the intent of his lints was to catch all such cases), so this PR closes that gap. The new lint is immediately set up to show in cargo's reports. I expect this to be rare (one has to work pretty hard to get such a const to be accepted), so it doesn't seem worth the extra delay of having a phase where the lint only shows up for the local crates.

So the question for you is, are you okay with consts in patterns requiring at least a PartialEq bound? (There might be further restrictions, related to all this "structural equality" business. That doesn't have to be answered here. This is just asking whether we need at least PartialEq.)

If you are curious about what the affected code looks like, here are some examples:

On both cases, the reason the constant isn't PartialEq is an unnecessary trait bound introduced by derive(PartialEq). That's why the compiler is still able to generate code that compares the constant with the matched place. These unnecessary trait bounds aren't great, but I don't think that should affect our const-in-match-pattern rules.

Contributor

Checking in from the @rust-lang/lang meeting and unnominating:

Our judgment is that this is a bug fix and only causes more warnings, so no FCP is required, and we are in favor of going forward.

However, we were pretty confused about what the story is around constants and matching -- requiring a PartialEq bound makes sense if we are invoking ==, but it's not clear if that is the case? Or sometimes the case? We would much appreciate to hear the latest thinking at some point (design meeting?).

@rustbot labels -I-lang-nominated

rustbot

removed the I-lang-nominated Indicates that an issue has been nominated for discussion during a lang team meeting. label

Sep 19, 2023

Member

Author

However, we were pretty confused about what the story is around constants and matching -- requiring a PartialEq bound makes sense if we are invoking ==, but it's not clear if that is the case? Or sometimes the case? We would much appreciate to hear the latest thinking at some point (design meeting?).

Yeah I think a design meeting is due, also see here. Maybe we shouldn't add further warnings until that happened, to avoid having to take back the warning again.

Member

Author

I started writing up a design meeting document and there good reasons for requiring PartialEq in either case. So as far as I am concerned, and based on t-lang feedback above, I think it makes sense to go ahead and land this, if the compiler team agrees.

@oli-obk @lcnr what do you think?

This comment has been minimized.

Member

Regarding the lint's name, maybe it is too general? I assume you do not want to forbid code like this:

enum Foo { Bar, Baz }
impl Foo {
    fn is_bar(&self) -> bool {
        matches!(self, Foo::Bar)
    }
}

Where Foo has no derive for PartialEq. I at least have been using this trick a few times when I thought that adding a derive was too burdensome.

Would the name constants_matching_without_partial_eq be better maybe? It's a bit longer but no idea what to remove. (also, generally, lints should be in plural form).

Member

Author

I renamed it to const_patterns_without_partial_eq.

est31 and lcnr reacted with thumbs up emoji

This comment has been minimized.

// Always check for `PartialEq`, even if we emitted other lints. (But not if there were

// any errors.) This ensures it shows up in cargo's future-compat reports as well.

if !self.type_has_partial_eq_impl(cv.ty()) {

Member

Author

There is the potential alternative of checking for Eq here instead of PartialEq. This would then subsume the "disallow floats in patterns" lint as well. However with t-lang in the past having been disinclined to disallow float patterns, it's not clear that we want to require Eq.

Contributor

@bors r+

let's land this and figure out the details of consts in patterns in a design meeting with T-lang

Contributor

📌 Commit a1d6fc4 has been approved by oli-obk

It is now in the queue for this repository.

bors

added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label).

labels

Sep 26, 2023

Contributor

⌛ Testing commit a1d6fc4 with merge 1f2bacf...

Contributor

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing 1f2bacf to master...

bors

added the merged-by-bors This PR was explicitly merged by bors label

Sep 26, 2023

bors

merged commit 1f2bacf into

rust-lang:master

Sep 26, 2023

12 checks passed

RalfJung

added relnotes Marks issues that should be documented in the release notes of the next release.

and removed relnotes Marks issues that should be documented in the release notes of the next release.

labels

Sep 26, 2023

Collaborator

Finished benchmarking commit (1f2bacf): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 631.784s -> 630.892s (-0.14%)
Artifact size: 317.18 MiB -> 317.28 MiB (0.03%)

RalfJung

deleted the match-require-partial-eq branch

September 30, 2023 20:27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

lcnr

lcnr left review comments
Assignees

b-naber

Labels
merged-by-bors This PR was explicitly merged by bors S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects

None yet

Milestone

1.74.0

Development

Successfully merging this pull request may close these issues.

None yet

10 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK