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

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