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

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