]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Fix some minor PDT parse errors [PR95543,PR103748]
authorPaul Thomas <pault@gcc.gnu.org>
Sun, 12 Oct 2025 07:21:11 +0000 (08:21 +0100)
committerPaul Thomas <pault@gcc.gnu.org>
Sun, 12 Oct 2025 07:21:11 +0000 (08:21 +0100)
2025-10-12  Paul Thomas  <pault@gcc.gnu.org>

gcc/fortran
PR fortran/95543
PR fortran/103748
* decl.cc (insert_parameter_exprs): Guard param->expr before
using it.
(gfc_get_pdt_instance): Substitute paramaters in kind default
initializers.
(gfc_match_decl_type_spec): Emit an error if a type paramter
specification list appears in a variable declaraion with a
non-parameterized type.
* primary.cc (gfc_match_rvalue): Emit an error if a type spec
list is empty.

gcc/testsuite/
PR fortran/95543
* gfortran.dg/pdt_17.f03: Change error message.
* gfortran.dg/pdt_57.f03: New test.

PR fortran/103748
* gfortran.dg/pdt_58.f03: New test.

gcc/fortran/decl.cc
gcc/fortran/primary.cc
gcc/testsuite/gfortran.dg/pdt_17.f03
gcc/testsuite/gfortran.dg/pdt_57.f03 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/pdt_58.f03 [new file with mode: 0644]

index 3fba8b1af3967a19f64de33f97ecf31043aa510e..5da3c267245662f9d91de89999abdbcb4d55bb3d 100644 (file)
@@ -3855,7 +3855,7 @@ insert_parameter_exprs (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
        if (strcmp (e->symtree->n.sym->name, param->name) == 0)
          break;
 
-      if (param)
+      if (param && param->expr)
        {
          copy = gfc_copy_expr (param->expr);
          *e = *copy;
@@ -4028,6 +4028,12 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
          /* Try simplification even for LEN expressions.  */
          bool ok;
          gfc_resolve_expr (kind_expr);
+
+         if (c1->attr.pdt_kind
+             && kind_expr->expr_type != EXPR_CONSTANT
+             && type_param_spec_list)
+         gfc_insert_parameter_exprs (kind_expr, type_param_spec_list);
+
          ok = gfc_simplify_expr (kind_expr, 1);
          /* Variable expressions seem to default to BT_PROCEDURE.
             TODO find out why this is and fix it.  */
@@ -4810,6 +4816,16 @@ gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
       return MATCH_ERROR;
     }
 
+  if (dt_sym && decl_type_param_list
+      && dt_sym->attr.flavor == FL_DERIVED
+      && !dt_sym->attr.pdt_type
+      && !dt_sym->attr.pdt_template)
+    {
+      gfc_error ("Type %qs is not parameterized and so the type parameter spec "
+                "list at %C may not appear", dt_sym->name);
+      return MATCH_ERROR;
+    }
+
   if (sym && sym->attr.flavor == FL_DERIVED
       && sym->attr.pdt_template
       && gfc_current_state () != COMP_DERIVED)
index fd03ceace51fe18cfe087e8db92ac85b773f25f5..cba4208a89fac72556edeead68c10a4d3c1bbaf8 100644 (file)
@@ -4083,7 +4083,13 @@ gfc_match_rvalue (gfc_expr **result)
                 NULL.  */
              if (gfc_peek_ascii_char() == '(')
                type_spec_list = true;
-
+             if (!actual_arglist && !type_spec_list)
+               {
+                 gfc_error_now ("F2023 R755: The empty type specification at %C "
+                                "is not allowed");
+                 m = MATCH_ERROR;
+                 break;
+               }
              /* Generate this instance using the type parameters from the
                 first argument list and return the parameter list in
                 ctr_arglist.  */
index d03e2d139a023d90ef25c76425a43bca9972f64e..eab9ee9b54eed3c761d7908a8cffdbe8bb738fd6 100644 (file)
@@ -6,6 +6,6 @@
 !
 program p
    type t(a)                   ! { dg-error "does not have a component" }
-      integer(kind=t()) :: x   ! { dg-error "Expected initialization expression" }
+      integer(kind=t()) :: x   ! { dg-error "empty type specification" }
    end type
 end
diff --git a/gcc/testsuite/gfortran.dg/pdt_57.f03 b/gcc/testsuite/gfortran.dg/pdt_57.f03
new file mode 100644 (file)
index 0000000..457ec79
--- /dev/null
@@ -0,0 +1,47 @@
+! { dg-do compile }
+!
+! Test the fix for pr95543. The variable declaration in each subroutine used to ICE
+! because the substitution of a in the default initializers of b was not being done.
+!
+! Contributed by Gerhard Steinmetz  <gscfq@t-online.de>
+!
+program p
+   call foo1
+   call foo2
+   call foo3
+   call foo4
+contains
+   subroutine foo1
+      type t(a, b)
+         integer, kind :: a = 4
+         integer, kind :: b = a + 4
+      end type
+      type(t()) :: z ! { dg-error "empty type specification" }
+      print *, z%b
+   end
+   subroutine foo2
+      type t(a, b)
+         integer, kind :: a = 1
+         integer, kind :: b = a
+      end type
+      type(t) :: z
+      print *, z%b
+   end
+   subroutine foo3
+      type t(a, b)
+         integer, kind :: a = 1
+         integer, kind :: b = a
+      end type
+      type(t(2)) :: z
+      print *, z%b
+   end
+   subroutine foo4
+      type t(a, b)
+         integer, kind :: a = 4
+         integer, kind :: b = a + 4
+      end type
+      type(t(b = 6)) :: z
+      print *, z%b
+   end
+end
+
diff --git a/gcc/testsuite/gfortran.dg/pdt_58.f03 b/gcc/testsuite/gfortran.dg/pdt_58.f03
new file mode 100644 (file)
index 0000000..cf26e8a
--- /dev/null
@@ -0,0 +1,14 @@
+! { dg-do compile }
+!
+! Test fix for PR103748.
+!
+! Contributed by Bastiaan Braams  <b.j.braams@cwi.nl>
+!
+program test
+  implicit none
+  type f_type
+     integer, allocatable :: x(:)
+  end type f_type
+  type (f_type(n=9)) :: f ! { dg-error "is not parameterized" }
+  stop
+end program test