public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-9358] Fortran: generate error message for negative elements in SHAPE array
@ 2021-12-04 22:06 Harald Anlauf
  0 siblings, 0 replies; only message in thread
From: Harald Anlauf @ 2021-12-04 22:06 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:6fa3c73e88ccbb6fffb4169bcab830fc3f4f3c9a

commit r11-9358-g6fa3c73e88ccbb6fffb4169bcab830fc3f4f3c9a
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Thu Oct 14 20:19:50 2021 +0200

    Fortran: generate error message for negative elements in SHAPE array
    
    gcc/fortran/ChangeLog:
    
            PR fortran/102717
            * simplify.c (gfc_simplify_reshape): Replace assert by error
            message for negative elements in SHAPE array.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/102717
            * gfortran.dg/reshape_shape_2.f90: New test.
    
    (cherry picked from commit b47490c572c5938f887b54240af6096a7c90f640)

Diff:
---
 gcc/fortran/simplify.c                        | 8 +++++++-
 gcc/testsuite/gfortran.dg/reshape_shape_2.f90 | 7 +++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index 77b982005fc..fb7b7814603 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -6852,7 +6852,13 @@ gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
       gfc_extract_int (e, &shape[rank]);
 
       gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS);
-      gcc_assert (shape[rank] >= 0);
+      if (shape[rank] < 0)
+	{
+	  gfc_error ("The SHAPE array for the RESHAPE intrinsic at %L has a "
+		     "negative value %d for dimension %d",
+		     &shape_exp->where, shape[rank], rank+1);
+	  return &gfc_bad_expr;
+	}
 
       rank++;
     }
diff --git a/gcc/testsuite/gfortran.dg/reshape_shape_2.f90 b/gcc/testsuite/gfortran.dg/reshape_shape_2.f90
new file mode 100644
index 00000000000..8f1757687bc
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/reshape_shape_2.f90
@@ -0,0 +1,7 @@
+! { dg-do compile }
+! PR fortran/102717
+
+program p
+  integer, parameter :: a(1) = 2
+  integer, parameter :: b(2) = reshape([3,4], -[a]) ! { dg-error "negative" }
+end


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

only message in thread, other threads:[~2021-12-04 22:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-04 22:06 [gcc r11-9358] Fortran: generate error message for negative elements in SHAPE array Harald Anlauf

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).