Dear all,

F2003:C626 was only partly implemented: we missed the case of ALLOCATE
of character dummy arguments with assumed length, where the type-spec
must use asterisk, i.e. (*).

Regtesting found one testcase that had a previously undetected error
and needed adjustment, which I chose such that the original error
was kept.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald

From b17beebd9c7c6475d7f89eef99c0075a562df0b8 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anl...@gmx.de>
Date: Mon, 17 Mar 2025 22:34:19 +0100
Subject: [PATCH] Fortran: check type-spec in ALLOCATE of dummy with assumed
 length [PR119338]

	PR fortran/119338

gcc/fortran/ChangeLog:

	* resolve.cc (resolve_allocate_expr): Check F2003:C626: Type-spec
	in ALLOCATE of an assumed-length character dummy argument shall be
	an asterisk.

gcc/testsuite/ChangeLog:

	* gfortran.dg/deferred_character_18.f90: Adjust testcase.
	* gfortran.dg/allocate_assumed_charlen_5.f90: New test.
---
 gcc/fortran/resolve.cc                          | 16 ++++++++++++++++
 .../gfortran.dg/allocate_assumed_charlen_5.f90  | 17 +++++++++++++++++
 .../gfortran.dg/deferred_character_18.f90       |  3 ++-
 3 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/allocate_assumed_charlen_5.f90

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index d64edff8507..ddd98270230 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -8987,6 +8987,22 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
       goto failure;
     }
 
+  /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
+     if and only if each allocate-object is a dummy argument for which the
+     corresponding type parameter is assumed.  */
+  if (code->ext.alloc.ts.type == BT_CHARACTER
+      && code->ext.alloc.ts.u.cl->length != NULL
+      && e->ts.type == BT_CHARACTER && !e->ts.deferred
+      && e->ts.u.cl->length == NULL
+      && e->symtree->n.sym->attr.dummy)
+    {
+      gfc_error ("The type parameter in ALLOCATE statement with type-spec "
+		 "shall be an asterisk as allocate object %qs at %L is a "
+		 "dummy argument with assumed type parameter",
+		 sym->name, &e->where);
+      goto failure;
+    }
+
   /* Check F08:C632.  */
   if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
       && !UNLIMITED_POLY (e))
diff --git a/gcc/testsuite/gfortran.dg/allocate_assumed_charlen_5.f90 b/gcc/testsuite/gfortran.dg/allocate_assumed_charlen_5.f90
new file mode 100644
index 00000000000..bc75dbe47ad
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/allocate_assumed_charlen_5.f90
@@ -0,0 +1,17 @@
+! { dg-do compile }
+! PR fortran/119338 - check F2003:C626
+
+module m
+  implicit none
+contains
+  subroutine sub (s, c)
+    character(len=*), allocatable, intent(out) :: s(:)
+    character(len=*), allocatable, intent(out) :: c
+    allocate(s(5))                      ! OK
+    allocate(c)                         ! OK
+    allocate(character(len=*)  :: s(5)) ! OK
+    allocate(character(len=*)  :: c)    ! OK
+    allocate(character(len=10) :: s(5)) ! { dg-error "shall be an asterisk" }
+    allocate(character(len=10) :: c)    ! { dg-error "shall be an asterisk" }
+  end subroutine sub
+end module m
diff --git a/gcc/testsuite/gfortran.dg/deferred_character_18.f90 b/gcc/testsuite/gfortran.dg/deferred_character_18.f90
index 1b1457fa293..b1229c2485e 100644
--- a/gcc/testsuite/gfortran.dg/deferred_character_18.f90
+++ b/gcc/testsuite/gfortran.dg/deferred_character_18.f90
@@ -11,7 +11,8 @@ contains
     character(*), allocatable, intent(out) :: str
 !  Note: Star ^ should have been a colon (:)
 
-    allocate (character(n)::str)
+!   allocate (character(n)::str) ! original invalid version from pr82367
+    allocate (character(*)::str) ! corrected (see F2003:C626 and pr119338)
 
   end subroutine
 
-- 
2.43.0

Reply via email to