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

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