]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Check PDT parameters are of integer type [PR83762, PR102457]
authorPaul Thomas <pault@gcc.gnu.org>
Fri, 5 Sep 2025 06:19:01 +0000 (07:19 +0100)
committerPaul Thomas <pault@gcc.gnu.org>
Fri, 5 Sep 2025 06:19:01 +0000 (07:19 +0100)
2025-09-05  Paul Thomas  <pault@gcc.gnu.org>

gcc/fortran
PR fortran/83762
PR fortran/102457
* decl.cc (gfc_get_pdt_instance): Check that variable PDT parm
expressions are of type integer. Note that the symbol must be
tested since the expression often appears as BT_PROCEDURE.

gcc/testsuite/
PR fortran/83762
PR fortran/102457
* gfortran.dg/pdt_44.f03: New test.
* gfortran.dg/pr95090.f90: Give the PDT parameter a value to
suppress the type error.

gcc/fortran/decl.cc
gcc/testsuite/gfortran.dg/pdt_44.f03 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/pr95090.f90

index fcbbc2f8c6e253096f16eac0345e9400d341df25..8b0d959dea62b9e8d0a3105c10f4e47a231d6dd1 100644 (file)
@@ -3952,6 +3952,16 @@ gfc_get_pdt_instance (gfc_actual_arglist *param_list, gfc_symbol **sym,
            }
        }
 
+      if (kind_expr && kind_expr->expr_type == EXPR_VARIABLE
+         && kind_expr->ts.type != BT_INTEGER
+         && kind_expr->symtree->n.sym->ts.type != BT_INTEGER)
+       {
+         gfc_error ("The type parameter expression at %L must be of INTEGER "
+                    "type and not %s", &kind_expr->where,
+                    gfc_basic_typename (kind_expr->symtree->n.sym->ts.type));
+         goto error_return;
+       }
+
       /* Store the current parameter expressions in a temporary actual
         arglist 'list' so that they can be substituted in the corresponding
         expressions in the PDT instance.  */
diff --git a/gcc/testsuite/gfortran.dg/pdt_44.f03 b/gcc/testsuite/gfortran.dg/pdt_44.f03
new file mode 100644 (file)
index 0000000..459001c
--- /dev/null
@@ -0,0 +1,28 @@
+! { dg-do compile }
+!
+! Test the fix for PRs83762 and 102457, in which type parameter expressions that
+! are not of INTEGER type were either not being diagnosed or were inadequately
+! diagnosed.
+!
+! PR83762
+module bar
+  implicit none
+  type :: foo(n)
+     integer, len :: n=10
+  end type foo
+contains
+  subroutine main
+    type(foo(undefined)) :: x ! { dg-error "must be of INTEGER type and not UNKNOWN" }
+  end subroutine main
+end module bar
+
+! PR102457
+subroutine s
+   real :: m = 2
+   type t(n)
+      integer, len :: n = 1
+      character(n*n) :: c
+   end type
+   type(t(m)) :: x ! { dg-error "must be of INTEGER type and not REAL" }
+   call h(x)
+end
index ec77802ec51a9c2a7689378fd7e8d7736f29c75b..714802fcbd9310687e1710d008ded5c28a26fb5e 100644 (file)
@@ -13,7 +13,7 @@ module m2345678901234567890123456789012345678901234567890123456789_123
 contains
   subroutine s2345678901234567890123456789012345678901234567890123456789_123
     type(t2345678901234567890123456789012345678901234567890123456789_123 &
-        (n2345678901234567890123456789012345678901234567890123456789_123)) :: &
+        (n2345678901234567890123456789012345678901234567890123456789_123 = 4)) :: &
          z2345678901234567890123456789012345678901234567890123456789_123
    end
 end