Closed
Description
Adopted and minimized from the HTTP method parsing routine of hyperium/hyper:
println!("{}", match (1u, 3u) { (0, 2...5) => 1, (1, 3) => 2, (_, 2...5) => 3, (_, _) => 4u });
println!("{}", match (1u, 3u) { (1, 3) => 2, (_, 2...5) => 3, (_, _) => 4u });
println!("{}", match (1u, 7u) { (0, 2...5) => 1, (1, 7) => 2, (_, 2...5) => 3, (_, _) => 4u });
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)
). The match
trans apparantly groupped the second pattern even when it may result in the wrong result.
Activity
ghost commentedon Oct 15, 2014
Broken in 0.11 and 0.12. Fine in 0.10 so I blame one of my refactors.
reem commentedon Oct 15, 2014
cc me
edwardw commentedon Oct 16, 2014
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:
gives the wrong answer 4. It has to do with what counts as a default wildcard arm as
trans/_match.rs
sees it. Right now only_ => foo
is but a tuple pattern with all wildcard elements should be, too.ghost commentedon Oct 16, 2014
The bug is that we consider the range
2..5
to be a "constructor" of the integer type being matched which is wrong (constructors must be disjoint much like variants of an enum). The right thing is to treat it asx if x >= 2 && x <= 5
. Should be very easy to fix.Not sure about the wild pattern but I do remember being surprised seeing that we'd only check for
PatWild
.edwardw commentedon Oct 16, 2014
@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?
ghost commentedon Oct 16, 2014
@edwardw Thanks! Yes, I'm on it. :-)
ghost commentedon Nov 11, 2014
An update: I haven't forgotten about this, will look this week.
1 remaining item
remram44 commentedon May 20, 2015
Still happens :(
Rewrite the pattern matching code in trans.
niconii commentedon Sep 21, 2016
This seems to be fixed as of beta?
solson commentedon Sep 21, 2016
Yes, this is already fixed in beta 1.12: https://is.gd/fwGJ30
Auto merge of #37616 - jneem:master, r=alexcrichton