Please find attached a patch, it is based on Steve Kargl's patch in PR93580 adding  a check for %len and test case.

The commit message in the patch includes change logs as I believe that is the intended way forward. One thing I'm unsure of it how handle multiple authors, it was easy in the ChangeLog files but it is not obvious for git commit messages.

OK for master and gcc 9 branch?

gcc/fortran/ChangeLog:

    Steven G. Kargl <ka...@gcc.gnu.org>
    Mark Eggleston <markeggles...@gcc.gnu.org>

    PR fortran/93580
    * primary.c (gfc_match_varspec): If the symbol following %
    is re or im and the primary expression type is not BT_COMPLEX
    issue an error. If the symbol is len and the primary
    expression type is not BT_CHARACTER is an error.

gcc/testsuite/ChangeLog:

    Mark Eggleston <markeggles...@gcc.gnu.org>

    PR fortran/93580
    * gfortran.dg/dg/pr93580.f90: New test.

--
https://www.codethink.co.uk/privacy.html

>From 2d4fddda1ccd981c9956f798b3ed4871d8fcb4f1 Mon Sep 17 00:00:00 2001
From: Mark Eggleston <markeggles...@gcc.gnu.org>
Date: Wed, 5 Feb 2020 10:38:37 +0000
Subject: [PATCH] fortran: ICE in gfc_validate_kind(): Got bad kind [PR93580]

Caused by using invalid part_refs in kind specifications,
e.g. %re or %im on non-complex expressions and %len on
non character expressions.

Check whether %re, %im and %len are valid when checking
kind specification.

The original patch from Steven G. Kargl  <ka...@gcc.gnu.org> only
checked for %re and %im.

gcc/fortran/ChangeLog:

	PR fortran/93580
	* primary.c (gfc_match_varspec): If the symbol following %
	is re or im and the primary expression type is not BT_COMPLEX
	issue an error. If the symbol is len and the primary
	expression type is not BT_CHARACTER is an error.

gcc/testsuite/ChangeLog:

	PR fortran/93580
	* gfortran.dg/dg/pr93580.f90: New test.
---
 gcc/fortran/primary.c                 | 24 ++++++++++++++++++++++--
 gcc/testsuite/gfortran.dg/pr93580.f90 | 13 +++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr93580.f90

diff --git a/gcc/fortran/primary.c b/gcc/fortran/primary.c
index bd50827bb15..d73898473df 100644
--- a/gcc/fortran/primary.c
+++ b/gcc/fortran/primary.c
@@ -2241,8 +2241,28 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
 	  if (inquiry)
 	    sym = NULL;
 
-	  if (sep == '%' && primary->ts.type != BT_UNKNOWN)
-	    intrinsic = true;
+	  if (sep == '%')
+	    {
+	      if (tmp)
+		{
+		  if ((tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
+		      && primary->ts.type != BT_COMPLEX)
+		    {
+			gfc_error ("The RE or IM part_ref at %C must be "
+				   "applied to a COMPLEX expression");
+			return MATCH_ERROR;
+		    }
+		  else if (tmp->u.i == INQUIRY_LEN
+			   && primary->ts.type != BT_CHARACTER)
+		    {
+			gfc_error ("The LEN part_ref at %C must be applied "
+				   "to a CHARACTER expression");
+			return MATCH_ERROR;
+		    }
+		}
+	      if (primary->ts.type != BT_UNKNOWN)
+		intrinsic = true;
+	    }
 	}
       else
 	inquiry = false;
diff --git a/gcc/testsuite/gfortran.dg/pr93580.f90 b/gcc/testsuite/gfortran.dg/pr93580.f90
new file mode 100644
index 00000000000..4feaa112914
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr93580.f90
@@ -0,0 +1,13 @@
+! { dg-do compile }
+! PR fortran/93580
+
+program p
+   integer, parameter :: n = 4
+   complex(n%re) :: x    ! { dg-error "The RE or IM part_ref at" }
+   complex(n%im) :: y    ! { dg-error "The RE or IM part_ref at" }
+   complex(n%len) :: z   ! { dg-error "The LEN part_ref at" }
+   character(n%im) :: a  ! { dg-error "The RE or IM part_ref at" }
+   character(n%re) :: b  ! { dg-error "The RE or IM part_ref at" }
+   character(n%len) :: c ! { dg-error "The LEN part_ref at" }
+end
+
-- 
2.11.0

Reply via email to