Skip to content

Commit 8542224

Browse files
authored
Merge pull request RustPython#1562 from ichyo/wrong-zero-div-optimization
Fix wrong const optimization for "1.0 / 0.0" case
2 parents 4b93479 + 74c02d4 commit 8542224

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

compiler/src/peephole/optimizations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn operator(buf: &mut impl OptimizationBuffer) {
5252
(op!(Multiply), lc!(Float, lhs), lc!(Float, rhs)) => {
5353
emitconst!(buf, [lhs_meta, rhs_meta], Float, lhs * rhs)
5454
}
55-
(op!(Divide), lc!(Float, lhs), lc!(Float, rhs)) => {
55+
(op!(Divide), lc!(Float, lhs), lc!(Float, rhs)) if rhs != 0.0 => {
5656
emitconst!(buf, [lhs_meta, rhs_meta], Float, lhs / rhs)
5757
}
5858
(op!(Power), lc!(Float, lhs), lc!(Float, rhs)) => {

tests/snippets/floats.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@
144144
assert_raises(ZeroDivisionError, lambda: 2 // 0.0)
145145
assert_raises(ZeroDivisionError, lambda: 2 % 0.0)
146146
# assert_raises(ZeroDivisionError, divmod, 2, 0.0)
147+
assert_raises(ZeroDivisionError, lambda: 2.0 / 0.0)
148+
assert_raises(ZeroDivisionError, lambda: 2.0 // 0.0)
149+
assert_raises(ZeroDivisionError, lambda: 2.0 % 0.0)
150+
assert_raises(ZeroDivisionError, divmod, 2.0, 0.0)
147151

148152
assert 1.2.__int__() == 1
149153
assert 1.2.__float__() == 1.2

0 commit comments

Comments
 (0)