public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-3176] Fortran: error recovery for invalid types in array constructors [PR107000]
@ 2022-10-08 20:27 Mikael Morin
  0 siblings, 0 replies; only message in thread
From: Mikael Morin @ 2022-10-08 20:27 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:705ed42a1ad950860f46c51216ff69dbe0f4857a

commit r13-3176-g705ed42a1ad950860f46c51216ff69dbe0f4857a
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Oct 4 23:04:06 2022 +0200

    Fortran: error recovery for invalid types in array constructors [PR107000]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/107000
            * arith.cc (gfc_arith_error): Define error message for
            ARITH_INVALID_TYPE.
            (reduce_unary): Catch arithmetic expressions with invalid type.
            (reduce_binary_ac): Likewise.
            (reduce_binary_ca): Likewise.
            (reduce_binary_aa): Likewise.
            (eval_intrinsic): Likewise.
            (gfc_real2complex): Source expression must be of type REAL.
            * gfortran.h (enum arith): Add ARITH_INVALID_TYPE.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/107000
            * gfortran.dg/pr107000.f90: New test.
    
    Co-authored-by: Mikael Morin <mikael@gcc.gnu.org>

Diff:
---
 gcc/fortran/arith.cc                   | 30 ++++++++++++++++----
 gcc/fortran/gfortran.h                 |  2 +-
 gcc/testsuite/gfortran.dg/pr107000.f90 | 50 ++++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+), 6 deletions(-)

diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc
index d57059a375f..086b1f856b1 100644
--- a/gcc/fortran/arith.cc
+++ b/gcc/fortran/arith.cc
@@ -118,6 +118,9 @@ gfc_arith_error (arith code)
     case ARITH_WRONGCONCAT:
       p = G_("Illegal type in character concatenation at %L");
       break;
+    case ARITH_INVALID_TYPE:
+      p = G_("Invalid type in arithmetic operation at %L");
+      break;
 
     default:
       gfc_internal_error ("gfc_arith_error(): Bad error code");
@@ -1268,7 +1271,10 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
   head = gfc_constructor_copy (op->value.constructor);
   for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
     {
-      rc = reduce_unary (eval, c->expr, &r);
+      if (c->expr->expr_type == EXPR_OP && c->expr->ts.type == BT_UNKNOWN)
+	rc = ARITH_INVALID_TYPE;
+      else
+	rc = reduce_unary (eval, c->expr, &r);
 
       if (rc != ARITH_OK)
 	break;
@@ -1309,6 +1315,8 @@ reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
 
       if (c->expr->expr_type == EXPR_CONSTANT)
         rc = eval (c->expr, op2, &r);
+      else if (c->expr->expr_type == EXPR_OP && c->expr->ts.type == BT_UNKNOWN)
+	rc = ARITH_INVALID_TYPE;
       else
 	rc = reduce_binary_ac (eval, c->expr, op2, &r);
 
@@ -1361,6 +1369,8 @@ reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
 
       if (c->expr->expr_type == EXPR_CONSTANT)
 	rc = eval (op1, c->expr, &r);
+      else if (c->expr->expr_type == EXPR_OP && c->expr->ts.type == BT_UNKNOWN)
+	rc = ARITH_INVALID_TYPE;
       else
 	rc = reduce_binary_ca (eval, op1, c->expr, &r);
 
@@ -1420,14 +1430,19 @@ reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
        c && d;
        c = gfc_constructor_next (c), d = gfc_constructor_next (d))
     {
+      if ((c->expr->expr_type == EXPR_OP && c->expr->ts.type == BT_UNKNOWN)
+	  || (d->expr->expr_type == EXPR_OP && d->expr->ts.type == BT_UNKNOWN))
+	rc = ARITH_INVALID_TYPE;
+      else
 	rc = reduce_binary (eval, c->expr, d->expr, &r);
-	if (rc != ARITH_OK)
-	  break;
 
-	gfc_replace_expr (c->expr, r);
+      if (rc != ARITH_OK)
+	break;
+
+      gfc_replace_expr (c->expr, r);
     }
 
-  if (c || d)
+  if (rc == ARITH_OK && (c || d))
     rc = ARITH_INCOMMENSURATE;
 
   if (rc != ARITH_OK)
@@ -1638,6 +1653,8 @@ eval_intrinsic (gfc_intrinsic_op op,
   else
     rc = reduce_binary (eval.f3, op1, op2, &result);
 
+  if (rc == ARITH_INVALID_TYPE)
+    goto runtime;
 
   /* Something went wrong.  */
   if (op == INTRINSIC_POWER && rc == ARITH_PROHIBIT)
@@ -2238,6 +2255,9 @@ gfc_real2complex (gfc_expr *src, int kind)
   arith rc;
   bool did_warn = false;
 
+  if (src->ts.type != BT_REAL)
+    return NULL;
+
   result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
 
   mpc_set_fr (result->value.complex, src->value.real, GFC_MPC_RND_MODE);
diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index 608dda4bf55..10bb098d136 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -226,7 +226,7 @@ enum gfc_intrinsic_op
 enum arith
 { ARITH_OK = 1, ARITH_OVERFLOW, ARITH_UNDERFLOW, ARITH_NAN,
   ARITH_DIV0, ARITH_INCOMMENSURATE, ARITH_ASYMMETRIC, ARITH_PROHIBIT,
-  ARITH_WRONGCONCAT
+  ARITH_WRONGCONCAT, ARITH_INVALID_TYPE
 };
 
 /* Statements.  */
diff --git a/gcc/testsuite/gfortran.dg/pr107000.f90 b/gcc/testsuite/gfortran.dg/pr107000.f90
new file mode 100644
index 00000000000..30289078c57
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr107000.f90
@@ -0,0 +1,50 @@
+! { dg-do compile }
+! PR fortran/107000 - ICE in gfc_real2complex, reduce_unary, reduce_binary_*
+! Contributed by G.Steinmetz
+
+program p
+  real    :: y(1)
+  complex :: x(1)
+  x = (1.0, 2.0) * [real :: -'1']    ! { dg-error "Operand of unary numeric operator" }
+  x = (1.0, 2.0) * [complex :: +'1'] ! { dg-error "Operand of unary numeric operator" }
+  x = [complex :: -'1'] * (1.0, 2.0) ! { dg-error "Operand of unary numeric operator" }
+  y = [complex :: -'1'] * 2          ! { dg-error "Operand of unary numeric operator" }
+  y = 2 * [complex :: -'1']          ! { dg-error "Operand of unary numeric operator" }
+  y = 2 * [complex :: -(.true.)]     ! { dg-error "Operand of unary numeric operator" }
+  y = [complex :: -(.true.)] * 2     ! { dg-error "Operand of unary numeric operator" }
+  print *, - [real ::  -'1' ]        ! { dg-error "Operand of unary numeric operator" }
+  print *, - [real :: [-'1']]        ! { dg-error "Operand of unary numeric operator" }
+  print *, - [real ::  +(.true.) ]   ! { dg-error "Operand of unary numeric operator" }
+  print *, - [real :: [+(.true.)]]   ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real ::  -'1' ]      ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real :: (-'1')]      ! { dg-error "Operand of unary numeric operator" }
+  print *, [real ::  -'1' ] * 2      ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: (-'1')] * 2      ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [integer :: -('1')]   ! { dg-error "Operand of unary numeric operator" }
+  print *, [integer :: -('1')] * 2   ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real :: 0, (-'1')]   ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 0, (-'1')] * 2   ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real :: 0, -'1']     ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 0, -'1'] * 2     ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real :: 0, 1+'1']    ! { dg-error "Operands of binary numeric operator" }
+  print *, [real :: 0, 1+'1'] * 2    ! { dg-error "Operands of binary numeric operator" }
+  print *, [real :: 1, +(.true.)]    ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, -(.true.)]    ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real :: 1, +(.true.)]      ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, +(.true.)] * 2      ! { dg-error "Operand of unary numeric operator" }
+  print *, [1, 2] * [real :: 1, +(.true.)] ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, +(.true.)] * [1, 2] ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, 2] * [real :: 1, +(.true.)] ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, +(.true.)] * [real :: 1, 2] ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 0, -'1'] * [real :: 1, +(+(.true.))] ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, [(+(.true.))]] * [real :: 0, [(-'1')]] ! { dg-error "Operand of unary numeric operator" }
+
+  ! Legal:
+  print *, 2 * [real :: 1, [2], 3]
+  print *, [real :: 1, [2], 3] * 2
+  print *, [real :: 1, [2], 3] * [real :: 1, [2], 3]
+  print *, [real :: 1, [2], 3] * [integer :: 1, [2], 3]
+  print *, [real :: 1, [2], 3] * [1, [2], 3]
+  print *, [real :: 1,  huge(2.0)] * [real :: 1,  real(1.0)]
+  print *, [real :: 1, -(huge(2.0))] * [real :: 1, +(real(1))]
+end

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-08 20:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-08 20:27 [gcc r13-3176] Fortran: error recovery for invalid types in array constructors [PR107000] Mikael Morin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).