From 86353186dc2e477fa00df0edbaa389d338c2935c Mon Sep 17 00:00:00 2001 From: Paul Thomas Date: Fri, 5 Sep 2025 07:19:01 +0100 Subject: Fortran: Check PDT parameters are of integer type [PR83762, PR102457] 2025-09-05 Paul Thomas 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 | 10 ++++++++++ gcc/testsuite/gfortran.dg/pdt_44.f03 | 28 ++++++++++++++++++++++++++++ gcc/testsuite/gfortran.dg/pr95090.f90 | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/pdt_44.f03 (limited to 'gcc') diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index fcbbc2f..8b0d959 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 0000000..459001c --- /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 ec77802..714802f 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 -- cgit v1.1