Hello world,

this patch fixes PR PR93500.  One part of it is due to
what Steve wrote in the patch (returning from resolutions when both
operands are NULL), but that still left a nonsensical error.
Returning &gfc_bad_expr when simplifying bounds resulted in the
division by zero error actually reaching the user.

As to why there is an extra error when this is done in the main
program, as compared to a subroutine, I don't know, but I do not
particularly care. What is important is that the first error message
is clear and reaches the user.

Regression-tested. OK for trunk?

Regards

        Thomas

2020-04-16  Thomas Koenig  <tkoe...@gcc.gnu.org>

        PR fortran/93500
        * resolve.c (resolve_operator): If both operands are
        NULL, return false.
        * simplify.c (simplify_bound): If a division by zero
        was seen during bound simplification, free the
        corresponcing expression and return &gfc_bad_expr.

2020-04-16  Thomas Koenig  <tkoe...@gcc.gnu.org>

        PR fortran/93500
        * arith_divide_3.f90: New test.
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 9b95200c241..650837e18c3 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -3986,6 +3986,9 @@ resolve_operator (gfc_expr *e)
 
   op1 = e->value.op.op1;
   op2 = e->value.op.op2;
+  if (op1 == NULL && op2 == NULL)
+    return false;
+
   dual_locus_error = false;
 
   /* op1 and op2 cannot both be BOZ.  */
diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index 807565b4e80..fba7f7020be 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -4251,7 +4251,13 @@ simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr 
*kind, int upper)
 
              for (j = 0; j < d; j++)
                gfc_free_expr (bounds[j]);
-             return bounds[d];
+             if (gfc_seen_div0)
+               {
+                 gfc_free_expr (bounds[d]);
+                 return &gfc_bad_expr;
+               }
+             else
+               return bounds[d];
            }
        }
 
diff --git a/gcc/testsuite/gfortran.dg/arith_divide_3.f90 
b/gcc/testsuite/gfortran.dg/arith_divide_3.f90
new file mode 100644
index 00000000000..d9eb4a0d590
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/arith_divide_3.f90
@@ -0,0 +1,13 @@
+! { dg-do compile }
+! PR 93500 - this used to cause an ICE
+
+program p
+   integer :: a(min(2,0)/0) ! { dg-error "Division by zero" }
+   integer :: b = lbound(a) ! { dg-error "must be an array" }
+end
+
+subroutine s
+   integer :: a(min(2,0)/0)  ! { dg-error "Division by zero" }
+   integer :: b = lbound(a)
+end
+

Reply via email to