-
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
Wrong reordering of match arms with ranges #18060
Comments
Broken in 0.11 and 0.12. Fine in 0.10 so I blame one of my refactors. |
cc me |
This is troublesome. As being mentioned in #13027 (comment), Marijn's article outlines how the pattern matching is compiled into a decision tree. In the test case illustrated here, the tree ends up like this:
So the match arms are not really reordered but rather grouped together. But never the less, the result here is simply wrong. Some fundamental assumptions of the algorithm could be wrong. There's also a minor issue. The following code: println!("{}", match (1u, 3u) { (0, 2...5) => 1, (1, 3) => 2, (_, 3) => 3, (_, _) => 4u }); gives the wrong answer 4. It has to do with what counts as a default wildcard arm as |
The bug is that we consider the range Not sure about the wild pattern but I do remember being surprised seeing that we'd only check for |
@jakub-, your assessment is more accurate. I have a fix for the minor issue I described. Not really worthy of a separated PR, so should I leave that to you too? |
@edwardw Thanks! Yes, I'm on it. :-) |
An update: I haven't forgotten about this, will look this week. |
If there are both range and literal patterns in the same column of a match expression, rustc may generate incorrect code. This patch fixes it. Closes rust-lang#18060
Still happens :( |
The old code was not well structured, difficult to understand, and buggy. The new implementation is completely naive, so there may be a slight runtime performance loss. That said, adding optimizations on top of a clear and correct implementation seems easier than trying to fix the old mess. Fixes issue rust-lang#19064. Fixes issue rust-lang#26989. Fixes issue rust-lang#26251. Fixes issue rust-lang#18060. Fixes issue rust-lang#24875. Fixes issue rust-lang#23311. Fixes issue rust-lang#20046.
This seems to be fixed as of beta? |
Yes, this is already fixed in beta 1.12: https://is.gd/fwGJ30 |
Adopted and minimized from the HTTP method parsing routine of hyperium/hyper:
All three lines should print 2, but the first line prints 3 on the current nightly (
rustc 0.13.0-nightly (40b244973 2014-10-14 23:22:20 +0000)
). Thematch
trans apparantly groupped the second pattern even when it may result in the wrong result.The text was updated successfully, but these errors were encountered: