The attached patch from Andre and input from Steve and Harald.

Regression tested on x86_64_linux_gnu.

Ok for mainline? Backport to 15 as it is a rejects valid?

Regards,

Jerry

commit 57bd42f0e96b37b34bc2860e1451568f987988f7
Author: Jerry DeLisle <[email protected]>
Date:   Wed Dec 10 10:38:29 2025 -0800

    Fortran: Fix wrongly rejected allocatable LOCK_TYPE [PR107406]

            PR fortran/107406

    gcc/fortran/ChangeLog:

            * parse.cc (check_component): Adjust the error check to
            require a codimension if the allocatable type has
            a non-coarray component.

    gcc/testsuite/ChangeLog:

            * gfortran.dg/coarray_lock_3.f90: Remove dg-error on
            valid code.
            * gfortran.dg/coarray_lock_4.f90: Likewise.
            * gfortran.dg/coarray_lock_5.f90: Likwise

            Co-Authored by: Andre Vehreschild <[email protected]>
commit 57bd42f0e96b37b34bc2860e1451568f987988f7
Author: Jerry DeLisle <[email protected]>
Date:   Wed Dec 10 10:38:29 2025 -0800

    Fortran: Fix wrongly rejected allocatable LOCK_TYPE [PR107406]
    
            PR fortran/107406
    
    gcc/fortran/ChangeLog:
    
            * parse.cc (check_component): Adjust the error check to
            require a codimension if the allocatable type has
            a non-coarray component.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/coarray_lock_3.f90: Remove dg-error on
            valid code.
            * gfortran.dg/coarray_lock_4.f90: Likewise.
            * gfortran.dg/coarray_lock_5.f90: Likwise
    
            Co-Authored by: Andre Vehreschild <[email protected]>

diff --git a/gcc/fortran/parse.cc b/gcc/fortran/parse.cc
index df8570bad28..813c5e3fe4a 100644
--- a/gcc/fortran/parse.cc
+++ b/gcc/fortran/parse.cc
@@ -3691,11 +3691,8 @@ check_component (gfc_symbol *sym, gfc_component *c, gfc_component **lockp,
                "of type LOCK_TYPE, which must have a codimension or be a "
                "subcomponent of a coarray", c->name, &c->loc);
 
-  if (lock_type && allocatable && !coarray)
-    gfc_error ("Allocatable component %s at %L of type LOCK_TYPE must have "
-               "a codimension", c->name, &c->loc);
-  else if (lock_type && allocatable && c->ts.type == BT_DERIVED
-           && c->ts.u.derived->attr.lock_comp)
+  if (lock_type && allocatable && !coarray && c->ts.type == BT_DERIVED
+      && c->ts.u.derived->attr.lock_comp)
     gfc_error ("Allocatable component %s at %L must have a codimension as "
                "it has a noncoarray subcomponent of type LOCK_TYPE",
                c->name, &c->loc);
diff --git a/gcc/testsuite/gfortran.dg/coarray_lock_3.f90 b/gcc/testsuite/gfortran.dg/coarray_lock_3.f90
index 388857307f0..2aec3717276 100644
--- a/gcc/testsuite/gfortran.dg/coarray_lock_3.f90
+++ b/gcc/testsuite/gfortran.dg/coarray_lock_3.f90
@@ -24,7 +24,7 @@ end module m
 module m2
   use iso_fortran_env
   type t2
-    type(lock_type), allocatable :: x ! { dg-error "Allocatable component x at .1. of type LOCK_TYPE must have a codimension" }
+    type(lock_type), allocatable :: x
   end type t2
 end module m2
 
diff --git a/gcc/testsuite/gfortran.dg/coarray_lock_4.f90 b/gcc/testsuite/gfortran.dg/coarray_lock_4.f90
index 787dfe04210..107dc19883f 100644
--- a/gcc/testsuite/gfortran.dg/coarray_lock_4.f90
+++ b/gcc/testsuite/gfortran.dg/coarray_lock_4.f90
@@ -49,7 +49,7 @@ subroutine more_tests
   type(t1) :: x1 ! OK
 
   type t2
-    type(lock_type), allocatable :: c1(:) ! { dg-error "Allocatable component c1 at .1. of type LOCK_TYPE must have a codimension" }
+    type(lock_type), allocatable :: c1(:)
   end type t2
 
   type t3
diff --git a/gcc/testsuite/gfortran.dg/coarray_lock_5.f90 b/gcc/testsuite/gfortran.dg/coarray_lock_5.f90
index b419606b0de..a92c7f52d8e 100644
--- a/gcc/testsuite/gfortran.dg/coarray_lock_5.f90
+++ b/gcc/testsuite/gfortran.dg/coarray_lock_5.f90
@@ -32,7 +32,7 @@ subroutine test2()
   use iso_fortran_env
   implicit none
   type t
-    type(lock_type), allocatable :: lock ! { dg-error "Allocatable component lock at .1. of type LOCK_TYPE must have a codimension" }
+    type(lock_type), allocatable :: lock
   end type t
   type t2
     type(lock_type) :: lock

Reply via email to