See attached patch, 3 of 3 for PR128017. A parenthesized CLASS variable
as an ASSOCIATE selector, as in associate (b => (a)), had no rank at
parse time, so b was taken to be a scalar and a reference such as
b(2)%c did not parse. The patch takes the type and rank from the
variable, as 1/3 does for a function. A scalar selector of this kind
is then associated with the copy that gfc_conv_expr_op makes of a
parenthesized CLASS variable, which my PR128478 patch adds, so this
patch has to go in after that one and after 1/3.

The fix is in move_associates_to_block in parse.cc. pr128017_2.f90
checks the values and bounds of the associate name at run time, and
pr128017_4.f90 that it cannot be defined.

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

fortran: [PR128017]-3 Parenthesized CLASS variable as ASSOCIATE selector

A parenthesized CLASS variable as an ASSOCIATE selector had no rank at
parse time, so the associate name was taken to be a scalar and element
references such as b(2)%c did not parse.  Take the type and rank from
the variable.  A scalar selector of this kind is then associated with
the copy that gfc_conv_expr_op makes of a parenthesized CLASS variable.

This patch depends on the PR128478 patch, which adds that copy.

Assisted-by: Claude Opus 5.5

        PR fortran/128017

gcc/fortran/ChangeLog:

        * parse.cc (move_associates_to_block): Take the type and rank of a
        parenthesized CLASS or derived-type variable selector from the
        variable.

gcc/testsuite/ChangeLog:

        * gfortran.dg/pr128017_2.f90: New test.
        * gfortran.dg/pr128017_4.f90: New test.
>From f93842e30252cf5cb66a18709b4e9403a66926fa Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sun, 15 Nov 2026 13:07:09 -0800
Subject: [PATCH] fortran: [PR128017]-3 Parenthesized CLASS variable as
 ASSOCIATE selector

A parenthesized CLASS variable as an ASSOCIATE selector had no rank at
parse time, so the associate name was taken to be a scalar and element
references such as b(2)%c did not parse.  Take the type and rank from
the variable.  A scalar selector of this kind is then associated with
the copy that gfc_conv_expr_op makes of a parenthesized CLASS variable.

This patch depends on the PR128478 patch, which adds that copy.

Assisted-by: Claude Opus 5.5

	PR fortran/128017

gcc/fortran/ChangeLog:

	* parse.cc (move_associates_to_block): Take the type and rank of a
	parenthesized CLASS or derived-type variable selector from the
	variable.

gcc/testsuite/ChangeLog:

	* gfortran.dg/pr128017_2.f90: New test.
	* gfortran.dg/pr128017_4.f90: New test.
---
 gcc/fortran/parse.cc                     | 14 ++++-
 gcc/testsuite/gfortran.dg/pr128017_2.f90 | 72 ++++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/pr128017_4.f90 | 27 +++++++++
 3 files changed, 111 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr128017_2.f90
 create mode 100644 gcc/testsuite/gfortran.dg/pr128017_4.f90

diff --git a/gcc/fortran/parse.cc b/gcc/fortran/parse.cc
index 478708f221d..d90ed7809a9 100644
--- a/gcc/fortran/parse.cc
+++ b/gcc/fortran/parse.cc
@@ -5444,8 +5444,8 @@ move_associates_to_block ()
 	sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
 
       /* If the function has been parsed, go straight to the result to
-	 obtain the expression rank.  A parenthesized function reference
-	 has the same type and rank.  */
+	 obtain the expression rank.  A parenthesized selector has the type
+	 and rank of its operand.  */
       gfc_expr *fn = target;
       while (fn->expr_type == EXPR_OP
 	     && fn->value.op.op == INTRINSIC_PARENTHESES)
@@ -5478,6 +5478,16 @@ move_associates_to_block ()
 	      target->corank = fn->corank;
 	    }
 	}
+      else if (fn != target && fn->expr_type == EXPR_VARIABLE
+	       && (fn->ts.type == BT_CLASS || fn->ts.type == BT_DERIVED)
+	       && !IS_INFERRED_TYPE (fn)
+	       && (!fn->ref || gfc_resolve_ref (fn)))
+	{
+	  gfc_expression_rank (fn);
+	  sym->ts = target->ts = fn->ts;
+	  target->rank = fn->rank;
+	  target->corank = fn->corank;
+	}
 
       /* Check if the target expression is array valued. This cannot be done
 	 by calling gfc_resolve_expr because the context is unavailable.
diff --git a/gcc/testsuite/gfortran.dg/pr128017_2.f90 b/gcc/testsuite/gfortran.dg/pr128017_2.f90
new file mode 100644
index 00000000000..25fb28417cc
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr128017_2.f90
@@ -0,0 +1,72 @@
+! { dg-do run }
+!
+! PR 128017
+
+module m
+  implicit none
+  type :: t
+    integer :: c = 7
+    integer, allocatable :: v(:)
+  end type
+  type, extends(t) :: u
+    character(:), allocatable :: s
+  end type
+end module
+
+program p
+  use m
+  implicit none
+  complex, target :: zs = (5., 6.)
+  character(len=5), target :: str = 'hello'
+  type(t), target :: dt(2)
+  class(*), pointer :: q
+  class(t), pointer :: qp(:)
+  class(t), allocatable :: ca
+
+  q => zs
+  associate (a => (q))
+    zs = (0., 0.)
+    select type (a)
+    type is (complex)
+      if (a /= (5., 6.)) stop 1
+    class default
+      stop 2
+    end select
+  end associate
+
+  q => str
+  associate (a => (q))
+    str = 'xxxxx'
+    select type (a)
+    type is (character(*))
+      if (a /= 'hello' .or. len (a) /= 5) stop 3
+    class default
+      stop 4
+    end select
+  end associate
+
+  allocate (ca, source = u (3, [1, 2, 3], 'abc'))
+  associate (a => (ca))
+    ca%v(1) = 99
+    select type (a)
+    type is (u)
+      if (any (a%v /= [1, 2, 3]) .or. a%s /= 'abc' .or. a%c /= 3) stop 5
+    class default
+      stop 6
+    end select
+  end associate
+
+  dt%c = [1, 2]
+  qp => dt
+  associate (b => (qp))
+    if (any (lbound (b) /= 1) .or. any (ubound (b) /= 2)) stop 7
+    if (b(2)%c /= 2) stop 8
+    if (any (b(2:1:-1)%c /= [2, 1])) stop 9
+    select type (b)
+    type is (t)
+      if (b(1)%c /= 1) stop 10
+    class default
+      stop 11
+    end select
+  end associate
+end program
diff --git a/gcc/testsuite/gfortran.dg/pr128017_4.f90 b/gcc/testsuite/gfortran.dg/pr128017_4.f90
new file mode 100644
index 00000000000..97dab93bebb
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr128017_4.f90
@@ -0,0 +1,27 @@
+! { dg-do compile }
+!
+! PR 128017
+
+program p
+  implicit none
+  complex, target :: z(2) = [(1., 2.), (3., 4.)]
+  class(*), pointer :: q(:)
+  type :: t
+    integer :: c = 7
+  end type
+  type(t), target :: dt(2)
+  class(t), pointer :: qp(:)
+
+  q => z
+  associate (a => (q))
+    select type (a) ! { dg-error "variable definition context" }
+    type is (complex)
+      a(1) = (0., 0.) ! { dg-error "variable definition context" }
+    end select
+  end associate
+
+  qp => dt
+  associate (b => (qp))
+    b(2)%c = 3 ! { dg-error "variable definition context" }
+  end associate
+end program
-- 
2.56.0

Reply via email to