See attached patch. gfortran represents a polymorphic associate name by
a class container, and gfc_variable_attr gave the associate name that
container's POINTER attribute. In a TYPE IS block the associate name
refers to the selector through the hidden _data component, which is
always a pointer, so the TARGET check also saw a pointer. The patch
clears POINTER and ALLOCATABLE for every associate name except that of
SELECT RANK, and looks through _data to the selector for TARGET. Trunk
reports four of the seven errors in the new test pr54322.f90; with the
patch it reports all seven, at the same lines as flang.

The fix is in gfc_variable_attr in primary.cc; the rest is tests.
associate_42.f90 pointer-assigned the associate name. It now
pointer-assigns obj%p after END ASSOCIATE rather than inside the
block, because inside the block END ASSOCIATE undoes the assignment
(PR128735). select_type_30.f03 passed an associate name to a POINTER
dummy, which is valid only if the dummy is INTENT(IN).

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

fortran: [PR54322] Associate names are neither POINTER nor ALLOCATABLE

An associate name has neither the POINTER nor the ALLOCATABLE attribute,
and has the TARGET attribute only if the selector is a variable with the
TARGET or POINTER attribute (F2023 11.1.3.3).  gfortran gave a
polymorphic associate name the POINTER attribute of its class container,
so it accepted pointer assignment to the associate name and, in a TYPE
IS block, pointer assignment to it from a selector that was neither
TARGET nor POINTER.

Clear POINTER and ALLOCATABLE for an associate name other than that of
SELECT RANK, and take TARGET from a SELECT TYPE selector rather than
from the _data component that narrows it.  Two tests relied on the old
behavior: associate_42.f90 pointer-assigned an associate name, and
select_type_30.f03 passed one to a POINTER dummy without INTENT(IN).

Assisted-by: Claude Opus 5.5

        PR fortran/54322

gcc/fortran/ChangeLog:

        * primary.cc (gfc_variable_attr): Clear POINTER and ALLOCATABLE for
        an associate name, except for SELECT RANK.  Take TARGET from a
        SELECT TYPE selector rather than from its _data component.

gcc/testsuite/ChangeLog:

        * gfortran.dg/associate_42.f90: Pointer-assign the selector, not
        the associate name.
        * gfortran.dg/select_type_30.f03: Make the POINTER dummy
        INTENT(IN).
        * gfortran.dg/pr54322.f90: New test.
>From d85bce4f045ff7d1517dd4f3540be006f8168fa1 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 7 Nov 2026 18:14:31 -0800
Subject: [PATCH] fortran: [PR54322] Associate names are neither POINTER nor
 ALLOCATABLE

An associate name has neither the POINTER nor the ALLOCATABLE attribute,
and has the TARGET attribute only if the selector is a variable with the
TARGET or POINTER attribute (F2023 11.1.3.3).  gfortran gave a
polymorphic associate name the POINTER attribute of its class container,
so it accepted pointer assignment to the associate name and, in a TYPE
IS block, pointer assignment to it from a selector that was neither
TARGET nor POINTER.

Clear POINTER and ALLOCATABLE for an associate name other than that of
SELECT RANK, and take TARGET from a SELECT TYPE selector rather than
from the _data component that narrows it.  Two tests relied on the old
behavior: associate_42.f90 pointer-assigned an associate name, and
select_type_30.f03 passed one to a POINTER dummy without INTENT(IN).

Assisted-by: Claude Opus 5.5

	PR fortran/54322

gcc/fortran/ChangeLog:

	* primary.cc (gfc_variable_attr): Clear POINTER and ALLOCATABLE for
	an associate name, except for SELECT RANK.  Take TARGET from a
	SELECT TYPE selector rather than from its _data component.

gcc/testsuite/ChangeLog:

	* gfortran.dg/associate_42.f90: Pointer-assign the selector, not
	the associate name.
	* gfortran.dg/select_type_30.f03: Make the POINTER dummy
	INTENT(IN).
	* gfortran.dg/pr54322.f90: New test.
---
 gcc/fortran/primary.cc                       | 25 +++++++-
 gcc/testsuite/gfortran.dg/associate_42.f90   |  2 +-
 gcc/testsuite/gfortran.dg/pr54322.f90        | 62 ++++++++++++++++++++
 gcc/testsuite/gfortran.dg/select_type_30.f03 |  2 +-
 4 files changed, 86 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr54322.f90

diff --git a/gcc/fortran/primary.cc b/gcc/fortran/primary.cc
index 84481cf35ae..c644bb01983 100644
--- a/gcc/fortran/primary.cc
+++ b/gcc/fortran/primary.cc
@@ -3180,20 +3180,39 @@ gfc_variable_attr (gfc_expr *expr)
   if (pointer || attr.proc_pointer)
     target = 1;
 
-  /* F2018:11.1.3.3: Other attributes of associate names
+  /* F2023:11.1.3.3: Other attributes of associate names
      "The associating entity does not have the ALLOCATABLE or POINTER
      attributes; it has the TARGET attribute if and only if the selector is
      a variable and has either the TARGET or POINTER attribute."  */
   if (sym->attr.associate_var && sym->assoc && sym->assoc->target)
     {
-      if (sym->assoc->target->expr_type == EXPR_VARIABLE)
+      gfc_expr *tgt = sym->assoc->target;
+
+      if (tgt->expr_type == EXPR_VARIABLE)
 	{
 	  symbol_attribute tgt_attr;
-	  tgt_attr = gfc_expr_attr (sym->assoc->target);
+
+	  /* The _data reference that narrows a SELECT TYPE selector is a
+	     pointer whatever the selector is, so query the selector.  */
+	  if (tgt->ref && !tgt->ref->next && tgt->ref->type == REF_COMPONENT
+	      && tgt->ref->u.c.component
+	      && strcmp (tgt->ref->u.c.component->name, "_data") == 0)
+	    {
+	      gfc_expr *bare = gfc_get_variable_expr (tgt->symtree);
+	      tgt_attr = gfc_expr_attr (bare);
+	      gfc_free_expr (bare);
+	    }
+	  else
+	    tgt_attr = gfc_expr_attr (tgt);
 	  target = (tgt_attr.pointer || tgt_attr.target);
 	}
       else
 	target = 0;
+
+      /* SELECT RANK's associating entity has the selector's ALLOCATABLE
+	 and POINTER attributes (F2023:11.1.10.3).  */
+      if (!sym->attr.select_rank_temporary)
+	pointer = allocatable = 0;
     }
 
   for (ref = expr->ref; ref; ref = ref->next)
diff --git a/gcc/testsuite/gfortran.dg/associate_42.f90 b/gcc/testsuite/gfortran.dg/associate_42.f90
index 359224de50c..3372def44bc 100644
--- a/gcc/testsuite/gfortran.dg/associate_42.f90
+++ b/gcc/testsuite/gfortran.dg/associate_42.f90
@@ -22,8 +22,8 @@ contains
         type is (character(*))
           if (any (point .ne. tgt1)) stop 1
       end select
-      point => tgt2
     end associate
+    obj%p => tgt2
 
     select type (z => obj%p)
       type is (character(*))
diff --git a/gcc/testsuite/gfortran.dg/pr54322.f90 b/gcc/testsuite/gfortran.dg/pr54322.f90
new file mode 100644
index 00000000000..6a32a21c1e2
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr54322.f90
@@ -0,0 +1,62 @@
+! { dg-do compile }
+!
+! PR 54322
+! An associate name has neither the POINTER nor the ALLOCATABLE attribute,
+! and has the TARGET attribute only if the selector is a variable with the
+! TARGET or POINTER attribute (F2023 11.1.3.3).
+
+module m
+  implicit none
+  type :: t
+    real :: x
+  end type
+  type, extends(t) :: t3
+    real :: z
+  end type
+end module
+
+program p
+  use m
+  implicit none
+  class(t), allocatable :: ca
+  class(t), pointer :: cp
+  class(t3), pointer :: p3
+  integer, pointer :: q
+  integer, target :: i
+  type(t), target :: tt
+
+  allocate (t3 :: ca)
+  q => i
+  cp => tt
+
+  select type (an => ca)
+  type is (t3)
+    p3 => an                ! { dg-error "neither TARGET nor POINTER" }
+  class is (t)
+    print *, allocated (an) ! { dg-error "must be ALLOCATABLE" }
+  end select
+
+  associate (a => q)
+    a => i                  ! { dg-error "Non-POINTER in pointer association" }
+    nullify (a)             ! { dg-error "Non-POINTER in pointer association" }
+    print *, associated (a) ! { dg-error "must be a POINTER" }
+  end associate
+
+  associate (a => cp)
+    a => tt                 ! { dg-error "Non-POINTER in pointer association" }
+  end associate
+
+  select type (an => cp)
+  type is (t)
+    an => tt                ! { dg-error "Non-POINTER in pointer association" }
+  end select
+
+  ! A pointer selector makes the associate name a target.
+  select type (an => cp)
+  type is (t3)
+    p3 => an
+  end select
+  associate (a => q)
+    q => a
+  end associate
+end program
diff --git a/gcc/testsuite/gfortran.dg/select_type_30.f03 b/gcc/testsuite/gfortran.dg/select_type_30.f03
index f467b834292..379a2e7cbb7 100644
--- a/gcc/testsuite/gfortran.dg/select_type_30.f03
+++ b/gcc/testsuite/gfortran.dg/select_type_30.f03
@@ -22,7 +22,7 @@
 contains
 
   subroutine DestroyNode( theNode, lstatus )
-    type(treeNode), pointer :: theNode
+    type(treeNode), pointer, intent(in) :: theNode
     logical, intent(out) :: lstatus
   end subroutine
   
-- 
2.56.0

Reply via email to