mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-16 01:25:25 +00:00
Fix wrong const optimization for "1.0 / 0.0" case
In cpython, "1.0 / 0.0" raises ZeroDivisionError ``` >>> 1.0 / 0.0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: float division by zer ``` but RustPython doesn't ``` >>>>> 1.0 / 0.0 inf ``` This is because they emits different byte codes. ``` $ echo "1.0 / 0.0" > zero-div.py $ python -m dis zero-div.py 1 0 LOAD_CONST 0 (1.0) 2 LOAD_CONST 1 (0.0) 4 BINARY_TRUE_DIVIDE 6 POP_TOP 8 LOAD_CONST 2 (None) 10 RETURN_VALUE $ cargo run --example dis zero-div.py zero-div.py: 0 LoadConst (inf) 1 Pop 2 LoadConst (None) 3 ReturnValue ``` This commit stops optimization for zero division case to generate same byte codes.
This commit is contained in:
parent
0fd098569e
commit
142a29be4a
1 changed files with 1 additions and 1 deletions
|
@ -52,7 +52,7 @@ pub fn operator(buf: &mut impl OptimizationBuffer) {
|
|||
(op!(Multiply), lc!(Float, lhs), lc!(Float, rhs)) => {
|
||||
emitconst!(buf, [lhs_meta, rhs_meta], Float, lhs * rhs)
|
||||
}
|
||||
(op!(Divide), lc!(Float, lhs), lc!(Float, rhs)) => {
|
||||
(op!(Divide), lc!(Float, lhs), lc!(Float, rhs)) if rhs != 0.0 => {
|
||||
emitconst!(buf, [lhs_meta, rhs_meta], Float, lhs / rhs)
|
||||
}
|
||||
(op!(Power), lc!(Float, lhs), lc!(Float, rhs)) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue