From f0ae8631450a9dd6b452a38c10b8963384dd0146 Mon Sep 17 00:00:00 2001 From: Mark Shinwell Date: Thu, 8 Dec 2016 14:35:07 +0000 Subject: [PATCH] MPR#7201 --- Changes | 4 ++++ asmcomp/cmmgen.ml | 4 ---- testsuite/tests/basic/zero_divided_by_n.ml | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 testsuite/tests/basic/zero_divided_by_n.ml diff --git a/Changes b/Changes index 2a63679e2cbc..8ae8967ba587 100644 --- a/Changes +++ b/Changes @@ -11,6 +11,10 @@ Next version (4.05.0): (Stephen Dolan, review by Alain Frisch, Pierre Chambart, Mark Shinwell, Gabriel Scherer and Damien Doligez) +- MPR#7201, GPR#954: Correct wrong optimisation of "0 / " + and "0 mod " in the case when was a non-constant + evaluating to zero (Mark Shinwell) + ### Runtime system: ### Type system: diff --git a/asmcomp/cmmgen.ml b/asmcomp/cmmgen.ml index fbd3f92dc5e2..8ecfd2a25ac6 100644 --- a/asmcomp/cmmgen.ml +++ b/asmcomp/cmmgen.ml @@ -339,8 +339,6 @@ let rec div_int c1 c2 is_safe dbg = Csequence(c1, raise_symbol dbg "caml_exn_Division_by_zero") | (c1, Cconst_int 1) -> c1 - | (Cconst_int 0 as c1, c2) -> - Csequence(c2, c1) | (Cconst_int n1, Cconst_int n2) -> Cconst_int (n1 / n2) | (c1, Cconst_int n) when n <> min_int -> @@ -387,8 +385,6 @@ let mod_int c1 c2 is_safe dbg = Csequence(c1, raise_symbol dbg "caml_exn_Division_by_zero") | (c1, Cconst_int (1 | (-1))) -> Csequence(c1, Cconst_int 0) - | (Cconst_int 0, c2) -> - Csequence(c2, Cconst_int 0) | (Cconst_int n1, Cconst_int n2) -> Cconst_int (n1 mod n2) | (c1, (Cconst_int n as c2)) when n <> min_int -> diff --git a/testsuite/tests/basic/zero_divided_by_n.ml b/testsuite/tests/basic/zero_divided_by_n.ml new file mode 100644 index 000000000000..1523d962cfa0 --- /dev/null +++ b/testsuite/tests/basic/zero_divided_by_n.ml @@ -0,0 +1,17 @@ +(* Mantis 7201 *) + +let f () = 0 [@@inline never] + +let () = + try + ignore ((0 / f ()) : int); + assert false + with Division_by_zero -> () + +(* Not in Mantis 7201, but related: *) + +let () = + try + ignore ((0 mod f ()) : int); + assert false + with Division_by_zero -> ()