See attached patch, 1 of 3 for PR128017. SELECT TYPE on an associate
name whose selector is a polymorphic function reference failed in
several ways. A complex part or type parameter inquiry on the TYPE IS
name crashed: the reference is simplified, and so resolved, at parse
time, before SELECT TYPE has set the target of the temporary, and the
inferred-type fixup dereferenced the missing target. A parenthesized
selector, (f()), had no type or rank at parse time, which gave bogus
errors or a crash; once compiled, its associate name had lower bounds
of zero and an allocatable result was never freed.

Start with resolve_variable in resolve.cc, a one-line guard that defers
the fixup until the target is set. Then move_associates_to_block in
parse.cc, which looks through the parentheses to the function for the
type and rank. The two hunks in trans_associate_var (trans-stmt.cc)
give a parenthesized CLASS array selector lower bounds of one and send
a parenthesized non-pointer function result through the existing path
that frees it. pr128017.f90 runs all the cases.

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

fortran: [PR128017]-1 ICEs for SELECT TYPE on a function ASSOCIATE name

SELECT TYPE on an associate name whose selector is a polymorphic function
reference failed in two ways.  A complex part or type parameter inquiry
on the TYPE IS associate name crashed, because simplifying the reference
at parse time resolves it, and the inferred-type fixup then ran before
SELECT TYPE had given the temporary its target.  With a parenthesized
selector the associate name had no type or rank at parse time, which
gave bogus errors or a crash.  The fixup now waits for the target, and a
parenthesized function reference takes its type and rank from the
function.  Such an associate name also gets lower bounds of one, and an
allocatable function result is freed at the end of the construct.

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 function selector from the function.
        * resolve.cc (resolve_variable): Do not fix up the inferred type of
        a SELECT TYPE temporary before its target is set.
        * trans-stmt.cc (trans_associate_var): Give the associate name of a
        parenthesized CLASS array selector lower bounds of one.  Free the
        result of a parenthesized non-pointer function selector.

gcc/testsuite/ChangeLog:

        * gfortran.dg/pr128017.f90: New test.
>From e53ce40fa14dd8b505f7582a0c993842dec1f9b4 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sun, 15 Nov 2026 13:07:07 -0800
Subject: [PATCH] fortran: [PR128017]-1 ICEs for SELECT TYPE on a function
 ASSOCIATE name

SELECT TYPE on an associate name whose selector is a polymorphic function
reference failed in two ways.  A complex part or type parameter inquiry
on the TYPE IS associate name crashed, because simplifying the reference
at parse time resolves it, and the inferred-type fixup then ran before
SELECT TYPE had given the temporary its target.  With a parenthesized
selector the associate name had no type or rank at parse time, which
gave bogus errors or a crash.  The fixup now waits for the target, and a
parenthesized function reference takes its type and rank from the
function.  Such an associate name also gets lower bounds of one, and an
allocatable function result is freed at the end of the construct.

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 function selector from the function.
	* resolve.cc (resolve_variable): Do not fix up the inferred type of
	a SELECT TYPE temporary before its target is set.
	* trans-stmt.cc (trans_associate_var): Give the associate name of a
	parenthesized CLASS array selector lower bounds of one.  Free the
	result of a parenthesized non-pointer function selector.

gcc/testsuite/ChangeLog:

	* gfortran.dg/pr128017.f90: New test.
---
 gcc/fortran/parse.cc                   |  27 +++++--
 gcc/fortran/resolve.cc                 |   3 +-
 gcc/fortran/trans-stmt.cc              |  21 ++++-
 gcc/testsuite/gfortran.dg/pr128017.f90 | 108 +++++++++++++++++++++++++
 4 files changed, 147 insertions(+), 12 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr128017.f90

diff --git a/gcc/fortran/parse.cc b/gcc/fortran/parse.cc
index c8e60c9d665..478708f221d 100644
--- a/gcc/fortran/parse.cc
+++ b/gcc/fortran/parse.cc
@@ -5444,11 +5444,16 @@ 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.  */
-      if (target->expr_type == EXPR_FUNCTION && target->symtree
-	  && target->symtree->n.sym)
+	 obtain the expression rank.  A parenthesized function reference
+	 has the same type and rank.  */
+      gfc_expr *fn = target;
+      while (fn->expr_type == EXPR_OP
+	     && fn->value.op.op == INTRINSIC_PARENTHESES)
+	fn = fn->value.op.op1;
+      if (fn->expr_type == EXPR_FUNCTION && fn->symtree
+	  && fn->symtree->n.sym)
 	{
-	  tsym = target->symtree->n.sym;
+	  tsym = fn->symtree->n.sym;
 	  if (!tsym->result)
 	    tsym->result = tsym;
 	  sym->ts = tsym->result->ts;
@@ -5456,15 +5461,21 @@ move_associates_to_block ()
 	    {
 	      if (CLASS_DATA (sym)->as)
 		{
-		  target->rank = CLASS_DATA (sym)->as->rank;
-		  target->corank = CLASS_DATA (sym)->as->corank;
+		  fn->rank = CLASS_DATA (sym)->as->rank;
+		  fn->corank = CLASS_DATA (sym)->as->corank;
 		}
 	      sym->attr.class_ok = 1;
 	    }
 	  else
 	    {
-	      target->rank = tsym->result->as ? tsym->result->as->rank : 0;
-	      target->corank = tsym->result->as ? tsym->result->as->corank : 0;
+	      fn->rank = tsym->result->as ? tsym->result->as->rank : 0;
+	      fn->corank = tsym->result->as ? tsym->result->as->corank : 0;
+	    }
+	  if (fn != target)
+	    {
+	      target->ts = sym->ts;
+	      target->rank = fn->rank;
+	      target->corank = fn->corank;
 	    }
 	}
 
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 5a8099ad132..d9cd4c90189 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -6691,7 +6691,8 @@ resolve_variable (gfc_expr *e)
        the now-resolved associate-name symbol.  */
     e->ts = sym->ts;
   else if (sym->attr.select_type_temporary
-	   && sym->ns->assoc_name_inferred)
+	   && sym->ns->assoc_name_inferred
+	   && sym->assoc && sym->assoc->target)
     gfc_fixup_inferred_type_refs (e);
 
   /* For variables that are used in an associate (target => object) where
diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index 53300dbd2a8..3bdc9e55157 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -2348,19 +2348,27 @@ trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block)
   else if (gfc_is_associate_pointer (sym))
     {
       gfc_se se;
+      gfc_expr *fn = e;
 
       gcc_assert (!sym->attr.dimension && !sym->attr.codimension);
 
       gfc_init_se (&se, NULL);
 
+      /* Parentheses around a non-pointer function result add nothing.  */
+      while (fn->expr_type == EXPR_OP
+	     && fn->value.op.op == INTRINSIC_PARENTHESES)
+	fn = fn->value.op.op1;
+      if (fn->expr_type != EXPR_FUNCTION || gfc_expr_attr (fn).pointer)
+	fn = e;
+
       /* Class associate-names come this way because they are
 	 unconditionally associate pointers and the symbol is scalar.  */
-      if (sym->ts.type == BT_CLASS && e->expr_type == EXPR_FUNCTION)
+      if (sym->ts.type == BT_CLASS && fn->expr_type == EXPR_FUNCTION)
 	{
-	  gfc_conv_expr (&se, e);
+	  gfc_conv_expr (&se, fn);
 	  se.expr = gfc_evaluate_now (se.expr, &se.pre);
 	  /* Finalize the expression and free if it is allocatable.  */
-	  gfc_finalize_tree_expr (&se, NULL, gfc_expr_attr (e), e->rank);
+	  gfc_finalize_tree_expr (&se, NULL, gfc_expr_attr (fn), fn->rank);
 	  gfc_add_block_to_block (&se.post, &se.finalblock);
 	  need_len_assign = false;
 	}
@@ -2378,6 +2386,13 @@ trans_associate_var (gfc_symbol *sym, gfc_wrapped_block *block)
 
 	  desc = gfc_class_data_get (se.expr);
 
+	  /* An expression selector yields a temporary with lower bounds of
+	     zero; the associate name has lower bounds of one.  */
+	  if (e->expr_type == EXPR_OP)
+	    for (int dim = 0; dim < e->rank; ++dim)
+	      gfc_conv_shift_descriptor_lbound (&se.pre, desc, dim,
+						gfc_index_one_node);
+
 	  if (need_len_assign)
 	    {
 	      if (e->symtree
diff --git a/gcc/testsuite/gfortran.dg/pr128017.f90 b/gcc/testsuite/gfortran.dg/pr128017.f90
new file mode 100644
index 00000000000..382f2ba7aed
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr128017.f90
@@ -0,0 +1,108 @@
+! { dg-do run }
+!
+! PR 128017
+
+module m
+  implicit none
+  type :: t
+    integer :: c = 7
+  end type
+  complex, target :: z(2) = [(1., 2.), (3., 4.)]
+  type(t), target :: dt(2) = [t(1), t(2)]
+contains
+  function f() result(r)
+    class(*), pointer :: r(:)
+    r => z
+  end function
+  function g() result(r)
+    class(t), pointer :: r(:)
+    r => dt
+  end function
+  function fa() result(r)
+    class(*), allocatable :: r
+    r = (5., 6.)
+  end function
+end module
+
+program p
+  use m
+  implicit none
+
+  associate (a => f())
+    select type (a)
+    type is (complex)
+      if (any (a%re /= [1., 3.])) stop 1
+      if (a(2)%im /= 4.) stop 2
+      if (a%kind /= kind (z)) stop 3
+      a(2)%re = 9.
+    class default
+      stop 4
+    end select
+  end associate
+  if (z(2)%re /= 9.) stop 5
+  z(2)%re = 3.
+
+  associate (a => f())
+    select type (x => a)
+    type is (complex)
+      if (any (x%im /= [2., 4.])) stop 6
+    class default
+      stop 7
+    end select
+  end associate
+
+  associate (b => g())
+    select type (b)
+    class is (t)
+      if (any (b%c /= [1, 2])) stop 8
+    class default
+      stop 9
+    end select
+  end associate
+
+  associate (a => (f()))
+    select type (a)
+    type is (complex)
+      if (any (a%re /= [1., 3.])) stop 10
+      if (any (lbound (a) /= 1) .or. any (ubound (a) /= 2)) stop 11
+      if (a(2)%im /= 4.) stop 12
+    class default
+      stop 13
+    end select
+  end associate
+
+  associate (a => ((f())))
+    select type (x => a)
+    type is (complex)
+      if (any (x(2:1:-1)%re /= [3., 1.])) stop 14
+    class default
+      stop 15
+    end select
+  end associate
+
+  associate (b => (g()))
+    if (any (lbound (b) /= 1) .or. any (ubound (b) /= 2)) stop 16
+    if (any (b(2:1:-1)%c /= [2, 1])) stop 17
+    select type (b)
+    class is (t)
+      if (any (b%c /= [1, 2])) stop 18
+    class default
+      stop 19
+    end select
+    select type (x => b)
+    class is (t)
+      if (x(2)%c /= 2) stop 20
+    class default
+      stop 21
+    end select
+  end associate
+
+  associate (a => (fa()))
+    select type (a)
+    type is (complex)
+      if (a%re /= 5. .or. a%im /= 6.) stop 22
+    class default
+      stop 23
+    end select
+  end associate
+end program
-- 
2.56.0

Reply via email to