-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Compiler rejects cast in match arm #11791
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
Comments
This is working as designed: we used to allow arbitrary constant expressions in patterns, but this was removed in db0693a . There are (at least) two options:
|
Thanks for the suggestion. I tried something like that actually but it doesn't seem to work for all types. Here's an example:
It's kind of strange because std::libc uses the same pattern. Do you know what the rationale for commit db0693a was? It doesn't seem like an improvement from my point of view as a user. |
Ah, looks like we don't support casts to raw pointers in const-expressions (yet). In this case you may be reduced to just using I don't remember the exact context, and, as you can see, the commit message is rather terse (as is the pull request: #6417). However, I believe it was simplifying the language and making the pattern grammar more sensible (previously, it wasn't clear if |
Thanks, that makes sense. The asymmetry between match predicates and normal expressions is somewhat displeasing but I can see the point of removing ambiguity from the language. (EDIT: Though in the case of casts there is never any ambiguity, I think? Or is there?) |
Sorry for necroposting but this was the first issue I found when googling for "Rust cast in match arm". Something like const TEN: u32 = 10;
fn main() {
let x: u64 = 20;
match x {
TEN as u64 => { /* ... */ },
_ => { /* ... */ },
}
} still doesn't compile on const TEN: u32 = 10;
fn main() {
let x: u64 = 20;
match x {
a if a == TEN as u64 => { /* ... */ },
_ => { /* ... */ },
}
} If you move the cast to inside the (of course here it'd be trivial to |
Hello, just passing by to say i was bitten by this when trying to write code like the following: enum XX {
A = 0,
B = 1,
C = 2,
} let x: u64 = ...;
match x {
XX::A as u64 => XX::A,
XX::B as u64 => XX::B,
XX::C as u64 => XX::C,
_=> panic!()
} I can hardcode the values but it would have been great to use the cast values in the pattern. |
I also just ran into this, when trying to match an |
Implement new lint `iter_over_hash_type` Implements and fixes rust-lang/rust-clippy#11788 This PR adds a new *restriction* lint `iter_over_hash_type` which prevents `Hash`-types (that is, `HashSet` and `HashMap`) from being used as the iterator in `for` loops. The justification for this is because in `Hash`-based types, the ordering of items is not guaranteed and may vary between executions of the same program on the same hardware. In addition, it reduces readability due to the unclear iteration order. The implementation of this lint also ensures the following: - Calls to `HashMap::keys`, `HashMap::values`, and `HashSet::iter` are also denied when used in `for` loops, - When this expression is used in procedural macros, it is not linted/denied. changelog: add new `iter_over_hash_type` lint to prevent unordered iterations through hashed data structures
At commit a5ab960. I would expect the following to work:
But the compiler rejects it with the following error:
The text was updated successfully, but these errors were encountered: