public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-3340] Fortran: handle bad array ctors with typespec [PR93483, PR107216, PR107219]
Date: Mon, 17 Oct 2022 17:26:11 +0000 (GMT)	[thread overview]
Message-ID: <20221017172611.2266F3858281@sourceware.org> (raw)

https://gcc.gnu.org/g:d45af5c2eb1ba1e48449d8f3c5b4e3994a956f92

commit r13-3340-gd45af5c2eb1ba1e48449d8f3c5b4e3994a956f92
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Sat Oct 15 21:56:56 2022 +0200

    Fortran: handle bad array ctors with typespec [PR93483, PR107216, PR107219]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/93483
            PR fortran/107216
            PR fortran/107219
            * arith.cc (reduce_unary): Handled expressions are EXP_CONSTANT and
            EXPR_ARRAY.  Do not attempt to reduce otherwise.
            (reduce_binary_ac): Likewise.
            (reduce_binary_ca): Likewise.
            (reduce_binary_aa): Moved check for EXP_CONSTANT and EXPR_ARRAY
            from here ...
            (reduce_binary): ... to here.
            (eval_intrinsic): Catch failed reductions.
            * gfortran.h (GFC_INTRINSIC_OPS): New enum ARITH_NOT_REDUCED to keep
            track of expressions that were not reduced by the arithmetic evaluation
            code.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/93483
            PR fortran/107216
            PR fortran/107219
            * gfortran.dg/array_constructor_56.f90: New test.
            * gfortran.dg/array_constructor_57.f90: New test.
    
    Co-authored-by: Mikael Morin <mikael@gcc.gnu.org>

Diff:
---
 gcc/fortran/arith.cc                               | 27 ++++++++++---------
 gcc/fortran/gfortran.h                             |  5 ++--
 gcc/testsuite/gfortran.dg/array_constructor_56.f90 | 22 ++++++++++++++++
 gcc/testsuite/gfortran.dg/array_constructor_57.f90 | 30 ++++++++++++++++++++++
 4 files changed, 68 insertions(+), 16 deletions(-)

diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc
index e76a82a1668..fc9224ebc5c 100644
--- a/gcc/fortran/arith.cc
+++ b/gcc/fortran/arith.cc
@@ -1315,14 +1315,14 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
   if (op->expr_type == EXPR_CONSTANT)
     return eval (op, result);
 
+  if (op->expr_type != EXPR_ARRAY)
+    return ARITH_NOT_REDUCED;
+
   rc = ARITH_OK;
   head = gfc_constructor_copy (op->value.constructor);
   for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
     {
-      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);
+      rc = reduce_unary (eval, c->expr, &r);
 
       if (rc != ARITH_OK)
 	break;
@@ -1363,8 +1363,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 if (c->expr->expr_type != EXPR_ARRAY)
+	rc = ARITH_NOT_REDUCED;
       else
 	rc = reduce_binary_ac (eval, c->expr, op2, &r);
 
@@ -1417,8 +1417,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 if (c->expr->expr_type != EXPR_ARRAY)
+	rc = ARITH_NOT_REDUCED;
       else
 	rc = reduce_binary_ca (eval, op1, c->expr, &r);
 
@@ -1478,11 +1478,7 @@ 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);
+      rc = reduce_binary (eval, c->expr, d->expr, &r);
 
       if (rc != ARITH_OK)
 	break;
@@ -1523,6 +1519,9 @@ reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
   if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
     return reduce_binary_ac (eval, op1, op2, result);
 
+  if (op1->expr_type != EXPR_ARRAY || op2->expr_type != EXPR_ARRAY)
+    return ARITH_NOT_REDUCED;
+
   return reduce_binary_aa (eval, op1, op2, result);
 }
 
@@ -1701,7 +1700,7 @@ eval_intrinsic (gfc_intrinsic_op op,
   else
     rc = reduce_binary (eval.f3, op1, op2, &result);
 
-  if (rc == ARITH_INVALID_TYPE)
+  if (rc == ARITH_INVALID_TYPE || rc == ARITH_NOT_REDUCED)
     goto runtime;
 
   /* Something went wrong.  */
diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index 10bb098d136..6bd8800ecf8 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -222,11 +222,12 @@ enum gfc_intrinsic_op
    Assumptions are made about the numbering of the interface_op enums.  */
 #define GFC_INTRINSIC_OPS GFC_INTRINSIC_END
 
-/* Arithmetic results.  */
+/* Arithmetic results.  ARITH_NOT_REDUCED is used to keep track of expressions
+   that were not reduced by the arithmetic evaluation code.  */
 enum arith
 { ARITH_OK = 1, ARITH_OVERFLOW, ARITH_UNDERFLOW, ARITH_NAN,
   ARITH_DIV0, ARITH_INCOMMENSURATE, ARITH_ASYMMETRIC, ARITH_PROHIBIT,
-  ARITH_WRONGCONCAT, ARITH_INVALID_TYPE
+  ARITH_WRONGCONCAT, ARITH_INVALID_TYPE, ARITH_NOT_REDUCED
 };
 
 /* Statements.  */
diff --git a/gcc/testsuite/gfortran.dg/array_constructor_56.f90 b/gcc/testsuite/gfortran.dg/array_constructor_56.f90
new file mode 100644
index 00000000000..4701fb36225
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/array_constructor_56.f90
@@ -0,0 +1,22 @@
+! { dg-do compile }
+!
+! Test the fix for the following:
+! PR fortran/93483
+! PR fortran/107216
+! PR fortran/107219
+!
+! Contributed by G.Steinmetz
+
+program p
+  real, parameter :: r0(*) = +[real :: +(1) ]
+  real, parameter :: r1(*) = +[real :: +[1] ]
+  real, parameter :: r2(*) = -[real :: [(1)]]
+  real, parameter :: r3(*) = +[real :: [-(1)]]
+  real, parameter :: r4(*) = -[real :: [[(1)]]]
+  real, parameter :: r5(*) = -[real :: -[1, 2]]
+  real, parameter :: r6(*) = +[real :: +[1, 2]]
+  real, parameter :: r7(*) =  [real :: 1, 2] * [real :: 1, (2)]
+  real, parameter :: r8(*) =  [real :: 1, (2)] * [real :: 1, 2]
+  real, parameter :: r9(*) = +[real :: 1, 2] * [real :: 1, (2)]
+  real, parameter :: rr(*) = -[real :: 1, (2)] * [real :: 1, 2]
+end
diff --git a/gcc/testsuite/gfortran.dg/array_constructor_57.f90 b/gcc/testsuite/gfortran.dg/array_constructor_57.f90
new file mode 100644
index 00000000000..1298c09cc40
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/array_constructor_57.f90
@@ -0,0 +1,30 @@
+! { dg-do run }
+! PR fortran/93483
+!
+! Verify that resolution (host associated parameter vs. contained function) works.
+!
+! Contributed by Mikael Morin
+
+module m
+  implicit none
+  integer, parameter :: a(*) = [ 7, 11 ]
+contains
+  subroutine bug
+    real :: b(1), c(1)
+    b = [ real :: (a(1)) ]
+    c = [ real ::  a(1)  ]
+    print *, b, c
+    if (any (b /= [ 14. ])) stop 1
+    if (any (c /= [ 14. ])) stop 2
+  contains
+    function a(c)
+      integer :: a, c
+      a = c + 13
+    end function a
+  end subroutine bug
+end module m
+
+program p
+  use m
+  call bug
+end program p

                 reply	other threads:[~2022-10-17 17:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221017172611.2266F3858281@sourceware.org \
    --to=anlauf@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).