-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
wart: non-Eq [T; 0] can be matched as if T were #[structural_match]
#62336
Comments
nominating for discussion at T-lang team meeting, since this is a question about the design of the language itself. (It may be worthwhile to try to make a PR that errors on such |
#[structural_match]
#[structural_match]
A possibly interesting note: I think the reason this case wasn't caught was because of how this is written: rust/src/librustc_mir/hair/pattern/mod.rs Lines 1037 to 1045 in 8c6fb02
because we have layered the type-checking atop the recursion (encoded in the I mainly note this because I'm curious if there's other checks on the contents' type in |
This is consistent with special handling for zero-length arrays throughout the language. For instance, |
Further to @varkor's point, another case where struct NoCopy;
let x = [NoCopy; 0]; // OK.
let x = [NoCopy; 1]; // OK.
let x = [NoCopy; 2]; // ERROR. |
And yet the type seems like something that's been decided in an ad hoc manner in each case, rather than having a consistent policy, no? |
@pnkfelix A very good point! It seems pretty inconsistent indeed. Might be good to do a survey if time allows. Ostensibly making One further case in which |
…l-match-check, r=nikomatsakis use visitor for #[structural_match] check This changes the code so that we recur down the structure of a type of a const (rather than just inspecting at a shallow one or two levels) when we are looking to see if it has an ADT that did not derive `PartialEq` and `Eq`. Fix #61188 Fix #62307 Cc #62336
Discussed briefly in the @rust-lang/lang meeting. We need to schedule some time to get into structural match. But, in the meantime, a useful step would be to do a crater run that tries to remove the |
cc @RalfJung @oli-obk I think this issue can now be closed? Afaict we now always need the type to implement |
Indeed this should have been closed together with #120362. Empty arrays with element type |
Spawned off of investigation of issue #62307 and PR #55837
We currently allow constants that are empty arrays of any type to be used as patterns in
match
. We allow this regardless of whether they are an array of an ADT that does not derivePartialEq
/Eq
This may be a feature or a bug depending on one's perspective.
Here is an example of the behavior in question (play):
To be clear: This behavior might be fine.
It is just a little weird, because on other uses of consts in patterns, we do tend to require that their types derive
PartialEq
andEq
#[structural_match]
; see Restrict constants in patterns rfcs#1445).PartialEq
is not required for a const in a pattern, namely forfor <'a> fn(&'a T)
; see Function pointer docs may need updating #46989 (comment) )But we can treat an empty array as an exceptional case here, if that's what people want.
& &B
leak use of PartialEq #62307).The text was updated successfully, but these errors were encountered: