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

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