diff mbox series

PR fortran/103776 - ICE in gfc_compare_string, at fortran/arith.c:1118

Message ID trinity-da0de4d7-91c4-4e4d-a85e-2e211cc144c4-1640034465508@3c-app-gmx-bs55
State New
Headers show
Series PR fortran/103776 - ICE in gfc_compare_string, at fortran/arith.c:1118 | expand

Commit Message

Harald Anlauf Dec. 20, 2021, 9:07 p.m. UTC
Dear all,

we need to check that expressions in CASE selectors are scalar,
and reject them early if they aren't.

Testcase by Gerhard.  The fix is obvious and sort of a follow-up
to the fix for PR103591.

Regtested on x86_64-pc-linux-gnu.

Will commit as obvious within 48h unless there are objections or
other comments.

Thanks,
Harald
diff mbox series

Patch

From 70385de214fe4de289c56e7f4e06a4b252414379 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 20 Dec 2021 22:01:05 +0100
Subject: [PATCH] Fortran: CASE selector expressions must be scalar

gcc/fortran/ChangeLog:

	PR fortran/103776
	* match.c (match_case_selector): Reject expressions in CASE
	selector which are not scalar.

gcc/testsuite/ChangeLog:

	PR fortran/103776
	* gfortran.dg/select_10.f90: New test.
---
 gcc/fortran/match.c                     | 13 +++++++++++++
 gcc/testsuite/gfortran.dg/select_10.f90 | 25 +++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/select_10.f90

diff --git a/gcc/fortran/match.c b/gcc/fortran/match.c
index 52bc5af7542..617fb35c9cd 100644
--- a/gcc/fortran/match.c
+++ b/gcc/fortran/match.c
@@ -6088,6 +6088,19 @@  match_case_selector (gfc_case **cp)
 	}
     }

+  if (c->low && c->low->rank != 0)
+    {
+      gfc_error ("Expression in CASE selector at %L must be scalar",
+		 &c->low->where);
+      goto cleanup;
+    }
+  if (c->high && c->high->rank != 0)
+    {
+      gfc_error ("Expression in CASE selector at %L must be scalar",
+		 &c->high->where);
+      goto cleanup;
+    }
+
   *cp = c;
   return MATCH_YES;

diff --git a/gcc/testsuite/gfortran.dg/select_10.f90 b/gcc/testsuite/gfortran.dg/select_10.f90
new file mode 100644
index 00000000000..2d9b0170ce9
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/select_10.f90
@@ -0,0 +1,25 @@ 
+! { dg-do compile }
+! PR fortran/103776 - ICE in gfc_compare_string
+! Contributed by G.Steinmetz
+
+program p
+  integer :: n
+  select case (n)
+  case ([1])        ! { dg-error "must be scalar" }
+  end select
+  select case (n)
+  case (:[2])       ! { dg-error "must be scalar" }
+  end select
+  select case (n)
+  case (['1'])      ! { dg-error "must be scalar" }
+  end select
+  select case (n)
+  case (['1']:2)    ! { dg-error "must be scalar" }
+  end select
+  select case (n)
+  case(['1']:['2']) ! { dg-error "must be scalar" }
+  end select
+  select case (n)
+  case(1:['2'])     ! { dg-error "must be scalar" }
+  end select
+end
--
2.26.2