-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
lint towards rejecting consts in patterns that do not implement PartialEq #115893
Conversation
r? @b-naber (rustbot has picked a reviewer for you, use r? to override) |
Some changes might have occurred in exhaustiveness checking cc @Nadrieril |
This comment has been minimized.
This comment has been minimized.
892ff5b
to
dc85202
Compare
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. |
@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 So the question for you is, are you okay with consts in patterns requiring at least a If you are curious about what the affected code looks like, here are some examples: On both cases, the reason the constant isn't |
6ca2a35
to
3eac984
Compare
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 @rustbot labels -I-lang-nominated |
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. |
I started writing up a design meeting document and there good reasons for requiring |
3eac984
to
2bbd5c3
Compare
This comment has been minimized.
This comment has been minimized.
2bbd5c3
to
31bd8e0
Compare
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 Would the name |
31bd8e0
to
204272b
Compare
I renamed it to |
This comment has been minimized.
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()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
.
@bors r+ let's land this and figure out the details of consts in patterns in a design meeting with T-lang |
☀️ Test successful - checks-actions |
Finished benchmarking commit (1f2bacf): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 631.784s -> 630.892s (-0.14%) |
remove StructuralEq trait The documentation given for the trait is outdated: *all* function pointers implement `PartialEq` and `Eq` these days. So the `StructuralEq` trait doesn't really seem to have any reason to exist any more. One side-effect of this PR is that we allow matching on some consts that do not implement `Eq`. However, we already allowed matching on floats and consts containing floats, so this is not new, it is just allowed in more cases now. IMO it makes no sense at all to allow float matching but also sometimes require an `Eq` instance. If we want to require `Eq` we should adjust rust-lang#115893 to check for `Eq`, and rule out float matching for good. Fixes rust-lang#115881
remove StructuralEq trait The documentation given for the trait is outdated: *all* function pointers implement `PartialEq` and `Eq` these days. So the `StructuralEq` trait doesn't really seem to have any reason to exist any more. One side-effect of this PR is that we allow matching on some consts that do not implement `Eq`. However, we already allowed matching on floats and consts containing floats, so this is not new, it is just allowed in more cases now. IMO it makes no sense at all to allow float matching but also sometimes require an `Eq` instance. If we want to require `Eq` we should adjust rust-lang#115893 to check for `Eq`, and rule out float matching for good. Fixes rust-lang#115881
remove StructuralEq trait The documentation given for the trait is outdated: *all* function pointers implement `PartialEq` and `Eq` these days. So the `StructuralEq` trait doesn't really seem to have any reason to exist any more. One side-effect of this PR is that we allow matching on some consts that do not implement `Eq`. However, we already allowed matching on floats and consts containing floats, so this is not new, it is just allowed in more cases now. IMO it makes no sense at all to allow float matching but also sometimes require an `Eq` instance. If we want to require `Eq` we should adjust rust-lang#115893 to check for `Eq`, and rule out float matching for good. Fixes rust-lang#115881
remove StructuralEq trait The documentation given for the trait is outdated: *all* function pointers implement `PartialEq` and `Eq` these days. So the `StructuralEq` trait doesn't really seem to have any reason to exist any more. One side-effect of this PR is that we allow matching on some consts that do not implement `Eq`. However, we already allowed matching on floats and consts containing floats, so this is not new, it is just allowed in more cases now. IMO it makes no sense at all to allow float matching but also sometimes require an `Eq` instance. If we want to require `Eq` we should adjust rust-lang/rust#115893 to check for `Eq`, and rule out float matching for good. Fixes rust-lang/rust#115881
remove StructuralEq trait The documentation given for the trait is outdated: *all* function pointers implement `PartialEq` and `Eq` these days. So the `StructuralEq` trait doesn't really seem to have any reason to exist any more. One side-effect of this PR is that we allow matching on some consts that do not implement `Eq`. However, we already allowed matching on floats and consts containing floats, so this is not new, it is just allowed in more cases now. IMO it makes no sense at all to allow float matching but also sometimes require an `Eq` instance. If we want to require `Eq` we should adjust rust-lang/rust#115893 to check for `Eq`, and rule out float matching for good. Fixes rust-lang/rust#115881
remove StructuralEq trait The documentation given for the trait is outdated: *all* function pointers implement `PartialEq` and `Eq` these days. So the `StructuralEq` trait doesn't really seem to have any reason to exist any more. One side-effect of this PR is that we allow matching on some consts that do not implement `Eq`. However, we already allowed matching on floats and consts containing floats, so this is not new, it is just allowed in more cases now. IMO it makes no sense at all to allow float matching but also sometimes require an `Eq` instance. If we want to require `Eq` we should adjust rust-lang/rust#115893 to check for `Eq`, and rule out float matching for good. Fixes rust-lang/rust#115881
make non-PartialEq-typed consts as patterns a hard error This lint was introduced in rust-lang#115893, for Rust 1.74, so we just had the third stable release where this is shown as a future-compat lint (which is shown for dependencies). Not a single comment or backreference showed up in the tracking issue, rust-lang#116122. So this seems fairly safe to turn into a hard error. Of course we should do a crater run first. This is part of rust-lang#120362.
remove StructuralEq trait The documentation given for the trait is outdated: *all* function pointers implement `PartialEq` and `Eq` these days. So the `StructuralEq` trait doesn't really seem to have any reason to exist any more. One side-effect of this PR is that we allow matching on some consts that do not implement `Eq`. However, we already allowed matching on floats and consts containing floats, so this is not new, it is just allowed in more cases now. IMO it makes no sense at all to allow float matching but also sometimes require an `Eq` instance. If we want to require `Eq` we should adjust rust-lang/rust#115893 to check for `Eq`, and rule out float matching for good. Fixes rust-lang/rust#115881
…li-obk make non-PartialEq-typed consts as patterns a hard error This lint was introduced in rust-lang#115893, for Rust 1.74, so we just had the third stable release where this is shown as a future-compat lint (which is shown for dependencies). Not a single comment or backreference showed up in the tracking issue, rust-lang#116122. So this seems fairly safe to turn into a hard error. Of course we should do a crater run first. This is part of rust-lang#120362. Closes rust-lang#116122.
Rollup merge of rust-lang#120805 - RalfJung:const-pat-partial-eq, r=oli-obk make non-PartialEq-typed consts as patterns a hard error This lint was introduced in rust-lang#115893, for Rust 1.74, so we just had the third stable release where this is shown as a future-compat lint (which is shown for dependencies). Not a single comment or backreference showed up in the tracking issue, rust-lang#116122. So this seems fairly safe to turn into a hard error. Of course we should do a crater run first. This is part of rust-lang#120362. Closes rust-lang#116122.
make non-PartialEq-typed consts as patterns a hard error This lint was introduced in rust-lang/rust#115893, for Rust 1.74, so we just had the third stable release where this is shown as a future-compat lint (which is shown for dependencies). Not a single comment or backreference showed up in the tracking issue, rust-lang/rust#116122. So this seems fairly safe to turn into a hard error. Of course we should do a crater run first. This is part of rust-lang/rust#120362. Closes rust-lang/rust#116122.
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.