https://gcc.gnu.org/g:86353186dc2e477fa00df0edbaa389d338c2935c

commit r16-3589-g86353186dc2e477fa00df0edbaa389d338c2935c
Author: Paul Thomas <[email protected]>
Date:   Fri Sep 5 07:19:01 2025 +0100

    Fortran: Check PDT parameters are of integer type [PR83762, PR102457]
    
    2025-09-05  Paul Thomas  <[email protected]>
    
    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.

Diff:
---
 gcc/fortran/decl.cc                   | 10 ++++++++++
 gcc/testsuite/gfortran.dg/pdt_44.f03  | 28 ++++++++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/pr95090.f90 |  2 +-
 3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index fcbbc2f8c6e2..8b0d959dea62 100644
--- a/gcc/fortran/decl.cc
+++ b/gcc/fortran/decl.cc
@@ -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
index 000000000000..459001cab09c
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_44.f03
@@ -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
diff --git a/gcc/testsuite/gfortran.dg/pr95090.f90 
b/gcc/testsuite/gfortran.dg/pr95090.f90
index ec77802ec51a..714802fcbd93 100644
--- a/gcc/testsuite/gfortran.dg/pr95090.f90
+++ b/gcc/testsuite/gfortran.dg/pr95090.f90
@@ -13,7 +13,7 @@ module 
m2345678901234567890123456789012345678901234567890123456789_123
 contains
   subroutine s2345678901234567890123456789012345678901234567890123456789_123
     type(t2345678901234567890123456789012345678901234567890123456789_123 &
-        (n2345678901234567890123456789012345678901234567890123456789_123)) :: &
+        (n2345678901234567890123456789012345678901234567890123456789_123 = 4)) 
:: &
          z2345678901234567890123456789012345678901234567890123456789_123
    end
 end

Reply via email to