]> git.lizzy.rs Git - rust.git/commit
Auto merge of #62584 - eddyb:circular-math-is-hard, r=pnkfelix
authorbors <bors@rust-lang.org>
Sat, 13 Jul 2019 20:45:40 +0000 (20:45 +0000)
committerbors <bors@rust-lang.org>
Sat, 13 Jul 2019 20:45:40 +0000 (20:45 +0000)
commitd32a7250dbf797c9a89f56de0842d7ad43bfe85f
tree5a5cffe20a744ad2cb71ea31b9397f5e744c7bf9
parent69656fa4cbafc378fd63f9186d93b0df3cdd9320
parentc063057beb96cd4901ab300eed2267c9b73ed589
Auto merge of #62584 - eddyb:circular-math-is-hard, r=pnkfelix

 rustc_codegen_ssa: fix range check in codegen_get_discr.

Fixes #61696, see https://github.com/rust-lang/rust/issues/61696#issuecomment-505473018 for more details.

In short, I had wanted to use `x - a <= b - a` to check whether `x` is in `a..=b` (as it's 1 comparison instead of 2 *and* `b - a` is guaranteed to fit in the same data type, while `b` itself might not), but I ended up with `x - a + c <= b - a + c` instead, because `x - a + c` was the final value needed.

That latter comparison is equivalent to checking that `x` is in `(a - c)..=b`, i.e. it also includes `(a - c)..a`, not just `a..=b`, so if `c` is not `0`, it will cause false positives.

This presented itself as the non-niche ("dataful") variant sometimes being treated like a niche variant, in the presence of uninhabited variants (which made `c`, aka the index of the first niche variant, arbitrarily large).

r? @nagisa, @rkruppe or @oli-obk