Blame SOURCES/gcc12-fortran-fdec-override-kind.patch

1198f7
From 786869fd62813e80da9b6545a295d53c36275c19 Mon Sep 17 00:00:00 2001
1198f7
From: Mark Eggleston <markeggleston@gcc.gnu.org>
1198f7
Date: Fri, 22 Jan 2021 13:12:14 +0000
1198f7
Subject: [PATCH 06/10] Allow string length and kind to be specified on a per
1198f7
 variable basis.
1198f7
1198f7
This allows kind/length to be mixed with array specification in
1198f7
declarations.
1198f7
1198f7
e.g.
1198f7
1198f7
      INTEGER*4 x*2, y*8
1198f7
      CHARACTER names*20(10)
1198f7
      REAL v(100)*8, vv*4(50)
1198f7
1198f7
The per-variable size overrides the kind or length specified for the type.
1198f7
1198f7
Use -fdec-override-kind to enable. Also enabled by -fdec.
1198f7
1198f7
Note: this feature is a merger of two previously separate features.
1198f7
1198f7
Now accepts named constants as kind parameters:
1198f7
1198f7
      INTEGER A
1198f7
      PARAMETER (A=2)
1198f7
      INTEGER B*(A)
1198f7
1198f7
Contributed by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
1198f7
Now rejects invalid kind parameters and prints error messages:
1198f7
1198f7
      INTEGER X*3
1198f7
1198f7
caused an internal compiler error.
1198f7
1198f7
Contributed by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
---
1198f7
 gcc/fortran/decl.cc                           | 156 ++++++++++++++----
1198f7
 gcc/fortran/lang.opt                          |   4 +
1198f7
 gcc/fortran/options.cc                        |   1 +
1198f7
 .../dec_mixed_char_array_declaration_1.f      |  13 ++
1198f7
 .../dec_mixed_char_array_declaration_2.f      |  13 ++
1198f7
 .../dec_mixed_char_array_declaration_3.f      |  13 ++
1198f7
 .../gfortran.dg/dec_spec_in_variable_1.f      |  31 ++++
1198f7
 .../gfortran.dg/dec_spec_in_variable_2.f      |  31 ++++
1198f7
 .../gfortran.dg/dec_spec_in_variable_3.f      |  31 ++++
1198f7
 .../gfortran.dg/dec_spec_in_variable_4.f      |  14 ++
1198f7
 .../gfortran.dg/dec_spec_in_variable_5.f      |  19 +++
1198f7
 .../gfortran.dg/dec_spec_in_variable_6.f      |  19 +++
1198f7
 .../gfortran.dg/dec_spec_in_variable_7.f      |  15 ++
1198f7
 .../gfortran.dg/dec_spec_in_variable_8.f      |  14 ++
1198f7
 14 files changed, 340 insertions(+), 34 deletions(-)
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_1.f
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_2.f
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_3.f
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_1.f
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_2.f
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_3.f
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_4.f
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_5.f
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_6.f
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_7.f
1198f7
 create mode 100644 gcc/testsuite/gfortran.dg/dec_spec_in_variable_8.f
1198f7
1198f7
diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
1198f7
index 5c8c1b7981b..f7dc9d8263d 100644
1198f7
--- a/gcc/fortran/decl.cc
1198f7
+++ b/gcc/fortran/decl.cc
1198f7
@@ -1213,6 +1213,54 @@ syntax:
1198f7
   return MATCH_ERROR;
1198f7
 }
1198f7
 
1198f7
+/* This matches the nonstandard kind given after a variable name, like:
1198f7
+   INTEGER x*2, y*4
1198f7
+   The per-variable kind will override any kind given in the type
1198f7
+   declaration.
1198f7
+*/
1198f7
+
1198f7
+static match
1198f7
+match_per_symbol_kind (int *length)
1198f7
+{
1198f7
+  match m;
1198f7
+  gfc_expr *expr = NULL;
1198f7
+
1198f7
+  m = gfc_match_char ('*');
1198f7
+  if (m != MATCH_YES)
1198f7
+    return m;
1198f7
+
1198f7
+  m = gfc_match_small_literal_int (length, NULL);
1198f7
+  if (m == MATCH_YES || m == MATCH_ERROR)
1198f7
+    return m;
1198f7
+
1198f7
+  if (gfc_match_char ('(') == MATCH_NO)
1198f7
+    return MATCH_ERROR;
1198f7
+
1198f7
+  m = gfc_match_expr (&expr;;
1198f7
+  if (m == MATCH_YES)
1198f7
+    {
1198f7
+      m = MATCH_ERROR; // Assume error
1198f7
+      if (gfc_expr_check_typed (expr, gfc_current_ns, false))
1198f7
+	{
1198f7
+	  if ((expr->expr_type == EXPR_CONSTANT)
1198f7
+	      && (expr->ts.type == BT_INTEGER))
1198f7
+	    {
1198f7
+	      *length = mpz_get_si(expr->value.integer);
1198f7
+	      m = MATCH_YES;
1198f7
+	    }
1198f7
+	}
1198f7
+
1198f7
+	if (m == MATCH_YES)
1198f7
+	  {
1198f7
+	    if (gfc_match_char (')') == MATCH_NO)
1198f7
+	       m = MATCH_ERROR;
1198f7
+  }
1198f7
+     }
1198f7
+
1198f7
+  if (expr != NULL)
1198f7
+     gfc_free_expr (expr);
1198f7
+  return m;
1198f7
+}
1198f7
 
1198f7
 /* Special subroutine for finding a symbol.  Check if the name is found
1198f7
    in the current name space.  If not, and we're compiling a function or
1198f7
@@ -2443,6 +2491,35 @@ check_function_name (char *name)
1198f7
 }
1198f7
 
1198f7
 
1198f7
+static match
1198f7
+match_character_length_clause (gfc_charlen **cl, bool *cl_deferred, int elem)
1198f7
+{
1198f7
+  gfc_expr* char_len;
1198f7
+  char_len = NULL;
1198f7
+
1198f7
+  match m = match_char_length (&char_len, cl_deferred, false);
1198f7
+  if (m == MATCH_YES)
1198f7
+    {
1198f7
+      *cl = gfc_new_charlen (gfc_current_ns, NULL);
1198f7
+      (*cl)->length = char_len;
1198f7
+    }
1198f7
+  else if (m == MATCH_NO)
1198f7
+    {
1198f7
+      if (elem > 1
1198f7
+	  && (current_ts.u.cl->length == NULL
1198f7
+	      || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1198f7
+	{
1198f7
+	  *cl = gfc_new_charlen (gfc_current_ns, NULL);
1198f7
+	  (*cl)->length = gfc_copy_expr (current_ts.u.cl->length);
1198f7
+	}
1198f7
+      else
1198f7
+      *cl = current_ts.u.cl;
1198f7
+
1198f7
+      *cl_deferred = current_ts.deferred;
1198f7
+    }
1198f7
+  return m;
1198f7
+}
1198f7
+
1198f7
 /* Match a variable name with an optional initializer.  When this
1198f7
    subroutine is called, a variable is expected to be parsed next.
1198f7
    Depending on what is happening at the moment, updates either the
1198f7
@@ -2453,7 +2530,7 @@ variable_decl (int elem)
1198f7
 {
1198f7
   char name[GFC_MAX_SYMBOL_LEN + 1];
1198f7
   static unsigned int fill_id = 0;
1198f7
-  gfc_expr *initializer, *char_len;
1198f7
+  gfc_expr *initializer;
1198f7
   gfc_array_spec *as;
1198f7
   gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
1198f7
   gfc_charlen *cl;
1198f7
@@ -2462,11 +2539,15 @@ variable_decl (int elem)
1198f7
   match m;
1198f7
   bool t;
1198f7
   gfc_symbol *sym;
1198f7
+  match cl_match;
1198f7
+  match kind_match;
1198f7
+  int overridden_kind;
1198f7
   char c;
1198f7
 
1198f7
   initializer = NULL;
1198f7
   as = NULL;
1198f7
   cp_as = NULL;
1198f7
+  kind_match = MATCH_NO;
1198f7
 
1198f7
   /* When we get here, we've just matched a list of attributes and
1198f7
      maybe a type and a double colon.  The next thing we expect to see
1198f7
@@ -2519,6 +2600,28 @@ variable_decl (int elem)
1198f7
 
1198f7
   var_locus = gfc_current_locus;
1198f7
 
1198f7
+
1198f7
+  cl = NULL;
1198f7
+  cl_deferred = false;
1198f7
+  cl_match = MATCH_NO;
1198f7
+
1198f7
+  /* Check for a character length clause before an array clause */
1198f7
+  if (flag_dec_override_kind)
1198f7
+    {
1198f7
+      if (current_ts.type == BT_CHARACTER)
1198f7
+	{
1198f7
+	  cl_match = match_character_length_clause (&cl, &cl_deferred, elem);
1198f7
+	  if (cl_match == MATCH_ERROR)
1198f7
+	    goto cleanup;
1198f7
+	}
1198f7
+      else
1198f7
+	{
1198f7
+	  kind_match = match_per_symbol_kind (&overridden_kind);
1198f7
+	  if (kind_match == MATCH_ERROR)
1198f7
+	    goto cleanup;
1198f7
+	}
1198f7
+    }
1198f7
+
1198f7
   /* Now we could see the optional array spec. or character length.  */
1198f7
   m = gfc_match_array_spec (&as, true, true);
1198f7
   if (m == MATCH_ERROR)
1198f7
@@ -2667,40 +2770,12 @@ variable_decl (int elem)
1198f7
 	}
1198f7
     }
1198f7
 
1198f7
-  char_len = NULL;
1198f7
-  cl = NULL;
1198f7
-  cl_deferred = false;
1198f7
-
1198f7
-  if (current_ts.type == BT_CHARACTER)
1198f7
+  /* Second chance for a character length clause */
1198f7
+  if (cl_match == MATCH_NO && current_ts.type == BT_CHARACTER)
1198f7
     {
1198f7
-      switch (match_char_length (&char_len, &cl_deferred, false))
1198f7
-	{
1198f7
-	case MATCH_YES:
1198f7
-	  cl = gfc_new_charlen (gfc_current_ns, NULL);
1198f7
-
1198f7
-	  cl->length = char_len;
1198f7
-	  break;
1198f7
-
1198f7
-	/* Non-constant lengths need to be copied after the first
1198f7
-	   element.  Also copy assumed lengths.  */
1198f7
-	case MATCH_NO:
1198f7
-	  if (elem > 1
1198f7
-	      && (current_ts.u.cl->length == NULL
1198f7
-		  || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1198f7
-	    {
1198f7
-	      cl = gfc_new_charlen (gfc_current_ns, NULL);
1198f7
-	      cl->length = gfc_copy_expr (current_ts.u.cl->length);
1198f7
-	    }
1198f7
-	  else
1198f7
-	    cl = current_ts.u.cl;
1198f7
-
1198f7
-	  cl_deferred = current_ts.deferred;
1198f7
-
1198f7
-	  break;
1198f7
-
1198f7
-	case MATCH_ERROR:
1198f7
-	  goto cleanup;
1198f7
-	}
1198f7
+      m = match_character_length_clause (&cl, &cl_deferred, elem);
1198f7
+      if (m == MATCH_ERROR)
1198f7
+	goto cleanup;
1198f7
     }
1198f7
 
1198f7
   /* The dummy arguments and result of the abreviated form of MODULE
1198f7
@@ -2802,6 +2877,19 @@ variable_decl (int elem)
1198f7
       goto cleanup;
1198f7
     }
1198f7
 
1198f7
+  if (kind_match == MATCH_YES)
1198f7
+    {
1198f7
+      gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1198f7
+      /* sym *must* be found at this point */
1198f7
+      sym->ts.kind = overridden_kind;
1198f7
+      if (gfc_validate_kind (sym->ts.type, sym->ts.kind, true) < 0)
1198f7
+	{
1198f7
+	  gfc_error ("Kind %d not supported for type %s at %C",
1198f7
+		     sym->ts.kind, gfc_basic_typename (sym->ts.type));
1198f7
+	  return MATCH_ERROR;
1198f7
+	}
1198f7
+    }
1198f7
+
1198f7
   if (!check_function_name (name))
1198f7
     {
1198f7
       m = MATCH_ERROR;
1198f7
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
1198f7
index 25cc948699b..4a269ebb22d 100644
1198f7
--- a/gcc/fortran/lang.opt
1198f7
+++ b/gcc/fortran/lang.opt
1198f7
@@ -502,6 +502,10 @@ fdec-math
1198f7
 Fortran Var(flag_dec_math)
1198f7
 Enable legacy math intrinsics for compatibility.
1198f7
 
1198f7
+fdec-override-kind
1198f7
+Fortran Var(flag_dec_override_kind)
1198f7
+Enable support for per variable kind specification.
1198f7
+
1198f7
 fdec-structure
1198f7
 Fortran Var(flag_dec_structure)
1198f7
 Enable support for DEC STRUCTURE/RECORD.
1198f7
diff --git a/gcc/fortran/options.cc b/gcc/fortran/options.cc
1198f7
index d6bd36c3a8a..edbab483b36 100644
1198f7
--- a/gcc/fortran/options.cc
1198f7
+++ b/gcc/fortran/options.cc
1198f7
@@ -78,6 +78,7 @@ set_dec_flags (int value)
1198f7
   SET_BITFLAG (flag_dec_blank_format_item, value, value);
1198f7
   SET_BITFLAG (flag_dec_char_conversions, value, value);
1198f7
   SET_BITFLAG (flag_dec_duplicates, value, value);
1198f7
+  SET_BITFLAG (flag_dec_override_kind, value, value);
1198f7
 }
1198f7
 
1198f7
 /* Finalize DEC flags.  */
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_1.f b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_1.f
1198f7
new file mode 100644
1198f7
index 00000000000..706ea4112a4
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_1.f
1198f7
@@ -0,0 +1,13 @@
1198f7
+! { dg-do run }
1198f7
+! { dg-options "-fdec" }
1198f7
+!
1198f7
+! Test character declaration with mixed string length and array specification
1198f7
+!
1198f7
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
1198f7
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+       PROGRAM character_declaration
1198f7
+          CHARACTER ASPEC_SLENGTH*2 (5) /'01','02','03','04','05'/
1198f7
+          CHARACTER SLENGTH_ASPEC(5)*2 /'01','02','03','04','05'/
1198f7
+          if (ASPEC_SLENGTH(3).NE.SLENGTH_ASPEC(3)) STOP 1
1198f7
+        END
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_2.f b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_2.f
1198f7
new file mode 100644
1198f7
index 00000000000..26d2acf01de
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_2.f
1198f7
@@ -0,0 +1,13 @@
1198f7
+! { dg-do run }
1198f7
+! { dg-options "-fdec-override-kind" }
1198f7
+!
1198f7
+! Test character declaration with mixed string length and array specification
1198f7
+!
1198f7
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
1198f7
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+        PROGRAM character_declaration
1198f7
+          CHARACTER ASPEC_SLENGTH*2 (5) /'01','02','03','04','05'/
1198f7
+          CHARACTER SLENGTH_ASPEC(5)*2 /'01','02','03','04','05'/
1198f7
+          if (ASPEC_SLENGTH(3).NE.SLENGTH_ASPEC(3)) STOP 1
1198f7
+        END
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_3.f b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_3.f
1198f7
new file mode 100644
1198f7
index 00000000000..76e4f0bdb93
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_mixed_char_array_declaration_3.f
1198f7
@@ -0,0 +1,13 @@
1198f7
+! { dg-do compile }
1198f7
+! { dg-options "-fdec-override-kind -fno-dec-override-kind" }
1198f7
+!
1198f7
+! Test character declaration with mixed string length and array specification
1198f7
+!
1198f7
+! Contributed by Jim MacArthur <jim.macarthur@codethink.co.uk>
1198f7
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+        PROGRAM character_declaration
1198f7
+          CHARACTER ASPEC_SLENGTH*2 (5) /'01','02','03','04','05'/ ! { dg-error "Syntax error" }
1198f7
+          CHARACTER SLENGTH_ASPEC(5)*2 /'01','02','03','04','05'/
1198f7
+          if (ASPEC_SLENGTH(3).NE.SLENGTH_ASPEC(3)) STOP 1 ! { dg-error " Operands of comparison operator" }
1198f7
+        END
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_1.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_1.f
1198f7
new file mode 100644
1198f7
index 00000000000..edd0f5874b7
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_1.f
1198f7
@@ -0,0 +1,31 @@
1198f7
+! { dg-do run }
1198f7
+! { dg-options "-fdec" }
1198f7
+!
1198f7
+! Test kind specification in variable not in type
1198f7
+!
1198f7
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+        program spec_in_var
1198f7
+          integer*8  ai*1, bi*4, ci
1198f7
+          real*4 ar*4, br*8, cr
1198f7
+
1198f7
+          ai = 1
1198f7
+          ar = 1.0
1198f7
+          bi = 2
1198f7
+          br = 2.0
1198f7
+          ci = 3
1198f7
+          cr = 3.0
1198f7
+
1198f7
+          if (ai .ne. 1) stop 1
1198f7
+          if (abs(ar - 1.0) > 1.0D-6) stop 2
1198f7
+          if (bi .ne. 2) stop 3
1198f7
+          if (abs(br - 2.0) > 1.0D-6) stop 4
1198f7
+          if (ci .ne. 3) stop 5
1198f7
+          if (abs(cr - 3.0) > 1.0D-6) stop 6
1198f7
+          if (kind(ai) .ne. 1) stop 7
1198f7
+          if (kind(ar) .ne. 4) stop 8
1198f7
+          if (kind(bi) .ne. 4) stop 9
1198f7
+          if (kind(br) .ne. 8) stop 10
1198f7
+          if (kind(ci) .ne. 8) stop 11
1198f7
+          if (kind(cr) .ne. 4) stop 12
1198f7
+        end
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_2.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_2.f
1198f7
new file mode 100644
1198f7
index 00000000000..bfaba584dbb
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_2.f
1198f7
@@ -0,0 +1,31 @@
1198f7
+! { dg-do run }
1198f7
+! { dg-options "-fdec-override-kind" }
1198f7
+!
1198f7
+! Test kind specification in variable not in type
1198f7
+!
1198f7
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+        program spec_in_var
1198f7
+          integer*8  ai*1, bi*4, ci
1198f7
+          real*4 ar*4, br*8, cr
1198f7
+
1198f7
+          ai = 1
1198f7
+          ar = 1.0
1198f7
+          bi = 2
1198f7
+          br = 2.0
1198f7
+          ci = 3
1198f7
+          cr = 3.0
1198f7
+
1198f7
+          if (ai .ne. 1) stop 1
1198f7
+          if (abs(ar - 1.0) > 1.0D-6) stop 2
1198f7
+          if (bi .ne. 2) stop 3
1198f7
+          if (abs(br - 2.0) > 1.0D-6) stop 4
1198f7
+          if (ci .ne. 3) stop 5
1198f7
+          if (abs(cr - 3.0) > 1.0D-6) stop 6
1198f7
+          if (kind(ai) .ne. 1) stop 7
1198f7
+          if (kind(ar) .ne. 4) stop 8
1198f7
+          if (kind(bi) .ne. 4) stop 9
1198f7
+          if (kind(br) .ne. 8) stop 10
1198f7
+          if (kind(ci) .ne. 8) stop 11
1198f7
+          if (kind(cr) .ne. 4) stop 12
1198f7
+        end
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_3.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_3.f
1198f7
new file mode 100644
1198f7
index 00000000000..5ff434e7466
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_3.f
1198f7
@@ -0,0 +1,31 @@
1198f7
+! { dg-do compile }
1198f7
+! { dg-options "-fdec -fno-dec-override-kind" }
1198f7
+!
1198f7
+! Test kind specification in variable not in type
1198f7
+!
1198f7
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+        program spec_in_var
1198f7
+          integer*8  ai*1, bi*4, ci ! { dg-error "Syntax error" }
1198f7
+          real*4 ar*4, br*8, cr ! { dg-error "Syntax error" }
1198f7
+
1198f7
+          ai = 1
1198f7
+          ar = 1.0
1198f7
+          bi = 2
1198f7
+          br = 2.0
1198f7
+          ci = 3
1198f7
+          cr = 3.0
1198f7
+
1198f7
+          if (ai .ne. 1) stop 1
1198f7
+          if (abs(ar - 1.0) > 1.0D-6) stop 2
1198f7
+          if (bi .ne. 2) stop 3
1198f7
+          if (abs(br - 2.0) > 1.0D-6) stop 4
1198f7
+          if (ci .ne. 3) stop 5
1198f7
+          if (abs(cr - 3.0) > 1.0D-6) stop 6
1198f7
+          if (kind(ai) .ne. 1) stop 7
1198f7
+          if (kind(ar) .ne. 4) stop 8
1198f7
+          if (kind(bi) .ne. 4) stop 9
1198f7
+          if (kind(br) .ne. 8) stop 10
1198f7
+          if (kind(ci) .ne. 8) stop 11
1198f7
+          if (kind(cr) .ne. 4) stop 12
1198f7
+        end
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_4.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_4.f
1198f7
new file mode 100644
1198f7
index 00000000000..c01980e8b9d
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_4.f
1198f7
@@ -0,0 +1,14 @@
1198f7
+! { dg-do compile }
1198f7
+!
1198f7
+! Test kind specification in variable not in type. The per variable
1198f7
+! kind specification is not enabled so these should fail
1198f7
+!
1198f7
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+        program spec_in_var
1198f7
+          integer a
1198f7
+          parameter(a=2)
1198f7
+          integer b*(a) ! { dg-error "Syntax error" }
1198f7
+          real c*(8)    ! { dg-error "Syntax error" }
1198f7
+          logical d*1_1 ! { dg-error "Syntax error" }
1198f7
+        end
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_5.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_5.f
1198f7
new file mode 100644
1198f7
index 00000000000..e2f39da3f4f
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_5.f
1198f7
@@ -0,0 +1,19 @@
1198f7
+! { dg-do run }
1198f7
+! { dg-options "-fdec-override-kind" }
1198f7
+!
1198f7
+! Test kind specification in variable not in type
1198f7
+!
1198f7
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+        program spec_in_var
1198f7
+          integer a
1198f7
+          parameter(a=2)
1198f7
+          integer b*(a)
1198f7
+          real c*(8)
1198f7
+          logical d*(1_1)
1198f7
+          character e*(a)
1198f7
+          if (kind(b).ne.2) stop 1
1198f7
+          if (kind(c).ne.8) stop 2
1198f7
+          if (kind(d).ne.1) stop 3
1198f7
+          if (len(e).ne.2) stop 4
1198f7
+        end
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_6.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_6.f
1198f7
new file mode 100644
1198f7
index 00000000000..569747874e3
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_6.f
1198f7
@@ -0,0 +1,19 @@
1198f7
+! { dg-do run }
1198f7
+! { dg-options "-fdec" }
1198f7
+!
1198f7
+! Test kind specification in variable not in type
1198f7
+!
1198f7
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+        program spec_in_var
1198f7
+          integer a
1198f7
+          parameter(a=2)
1198f7
+          integer b*(a)
1198f7
+          real c*(8)
1198f7
+          logical d*(1_1)
1198f7
+          character e*(a)
1198f7
+          if (kind(b).ne.2) stop 1
1198f7
+          if (kind(c).ne.8) stop 2
1198f7
+          if (kind(d).ne.1) stop 3
1198f7
+          if (len(e).ne.2) stop 4
1198f7
+        end
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_7.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_7.f
1198f7
new file mode 100644
1198f7
index 00000000000..b975bfd15c5
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_7.f
1198f7
@@ -0,0 +1,15 @@
1198f7
+! { dg-do compile }
1198f7
+! { dg-options "-fdec -fno-dec-override-kind" }
1198f7
+!
1198f7
+! Test kind specification in variable not in type as the per variable
1198f7
+! kind specification is not enables these should fail
1198f7
+!
1198f7
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+        program spec_in_var
1198f7
+          integer a
1198f7
+          parameter(a=2)
1198f7
+          integer b*(a) ! { dg-error "Syntax error" }
1198f7
+          real c*(8)    ! { dg-error "Syntax error" }
1198f7
+          logical d*1_1 ! { dg-error "Syntax error" }
1198f7
+        end
1198f7
diff --git a/gcc/testsuite/gfortran.dg/dec_spec_in_variable_8.f b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_8.f
1198f7
new file mode 100644
1198f7
index 00000000000..85732e0bd85
1198f7
--- /dev/null
1198f7
+++ b/gcc/testsuite/gfortran.dg/dec_spec_in_variable_8.f
1198f7
@@ -0,0 +1,14 @@
1198f7
+! { dg-do compile }
1198f7
+! { dg-options "-fdec" }
1198f7
+!
1198f7
+! Check that invalid kind values are rejected.
1198f7
+!
1198f7
+! Contributed by Mark Eggleston <mark.eggleston@codethink.com>
1198f7
+!
1198f7
+        program spec_in_var
1198f7
+          integer a
1198f7
+          parameter(a=3)
1198f7
+          integer b*(a) ! { dg-error "Kind 3 not supported" }
1198f7
+          real c*(78)   ! { dg-error "Kind 78 not supported" }
1198f7
+          logical d*(*) ! { dg-error "Invalid character" }
1198f7
+        end
1198f7
-- 
1198f7
2.27.0
1198f7