-
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
Pattern-matching array values in struct fields causes compiler panic #57472
Comments
I actually think I know what is going on here. If you add I'm not entirely sure what the best way to fix this is, but I'll post a PR once I get something working. |
Thumbs-up for an interesting workaround, however it does not work for union patterns as they can only be matched on one field. I'm unclear on why that limitation even exists. |
For union patterns you're only accessing one field, so you're not using |
I also get a UCE if I make the obvious tweaks to use a union instead: pub union Punned {
foo: [u8; 1],
bar: [u8; 1],
}
pub fn test(punned: Punned) {
match punned {
Punned { foo: [_] } => println!("foo"),
Punned { bar: [_] } => println!("bar"),
}
} It doesn't matter whether or not the match is in an |
Ah! you're right! I think I was using my updated compiler when I tested that. I added the example with the union to the test in the PR. |
librustc_mir: Fix ICE with slice patterns If a match arm does not include all fields in a structure and a later pattern includes a field that is an array, we will attempt to use the array type from the prior arm. When calculating the field type, treat a array of an unknown size as a `TyErr`. Fixes: rust-lang#57472
I am writing some code which parses on-disk structures which vary their type based on magic numbers scattered through the structure. I have chosen to model this as a union of structs, and the magic number fields as byte arrays because they have neither power-of-two alignment or size, and then pattern-match on magic numbers to determine the type. This turns out to be sufficiently pathological to cause compiler panics.
Here is a minimal case that triggers the panic:
Playing around with the different things being matched on — my real code matches on constants — suggest that this is a bug in the exhaustiveness checking.
Output of
rustc --version --verbose
:Output of
RUST_BACKTRACE=1 rustc crash.rs
:The text was updated successfully, but these errors were encountered: