See attached patch, 2 of 3 for PR128017. Assignment to an element of a
SELECT TYPE associate name was accepted when the selector was itself an
associate name for an expression, such as an allocatable function
result, which cannot be defined. gfc_check_vardef_context skipped the
check of the selector because the TYPE IS temporary is internally a
pointer, which it took as a definition through a pointer component. An
associate name never has the POINTER attribute (F2023 11.1.3.3), so the
patch stops treating the temporary as one.
The fix is in gfc_check_vardef_context in expr.cc; the rest is the new
test.
Regression tested on x86_64.
OK for mainline?
Regards,
Jerry
---
fortran: [PR128017]-2 Defining a SELECT TYPE name bound to an expression
Assigning to an element of a SELECT TYPE associate name was accepted when
the selector was an associate name for an expression, such as an
allocatable function result. The check of the selector was skipped
because the TYPE IS temporary is internally a pointer, which was taken to
mean that the definition went through a pointer component. The
associating entity never has the POINTER attribute (F2023 11.1.3.3), so
the temporary no longer counts as one.
Assisted-by: Claude Opus 5.5
PR fortran/128017
gcc/fortran/ChangeLog:
* expr.cc (gfc_check_vardef_context): Do not treat a SELECT TYPE
temporary as a pointer.
gcc/testsuite/ChangeLog:
* gfortran.dg/pr128017_3.f90: New test.
>From 5f54989be5fa6c2731f0f2c9064cb34219609f55 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sun, 15 Nov 2026 13:07:08 -0800
Subject: [PATCH] fortran: [PR128017]-2 Defining a SELECT TYPE name bound to an
expression
Assigning to an element of a SELECT TYPE associate name was accepted when
the selector was an associate name for an expression, such as an
allocatable function result. The check of the selector was skipped
because the TYPE IS temporary is internally a pointer, which was taken to
mean that the definition went through a pointer component. The
associating entity never has the POINTER attribute (F2023 11.1.3.3), so
the temporary no longer counts as one.
Assisted-by: Claude Opus 5.5
PR fortran/128017
gcc/fortran/ChangeLog:
* expr.cc (gfc_check_vardef_context): Do not treat a SELECT TYPE
temporary as a pointer.
gcc/testsuite/ChangeLog:
* gfortran.dg/pr128017_3.f90: New test.
---
gcc/fortran/expr.cc | 10 ++++--
gcc/testsuite/gfortran.dg/pr128017_3.f90 | 43 ++++++++++++++++++++++++
2 files changed, 50 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/pr128017_3.f90
diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc
index 291adb7791a..65ccc76bf19 100644
--- a/gcc/fortran/expr.cc
+++ b/gcc/fortran/expr.cc
@@ -6877,9 +6877,13 @@ gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
component. Note that (normal) assignment to procedure pointers is not
possible. */
check_intentin = !own_scope;
- ptr_component = (sym->ts.type == BT_CLASS && sym->ts.u.derived
- && CLASS_DATA (sym))
- ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
+ /* A SELECT TYPE temporary is a pointer only internally (F2023 11.1.3.3). */
+ if (sym->attr.select_type_temporary)
+ ptr_component = false;
+ else
+ ptr_component = (sym->ts.type == BT_CLASS && sym->ts.u.derived
+ && CLASS_DATA (sym))
+ ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
for (ref = e->ref; ref && check_intentin; ref = ref->next)
{
/* Associate-targets need special handling. Subobjects of an object with
diff --git a/gcc/testsuite/gfortran.dg/pr128017_3.f90 b/gcc/testsuite/gfortran.dg/pr128017_3.f90
new file mode 100644
index 00000000000..60447259777
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr128017_3.f90
@@ -0,0 +1,43 @@
+! { dg-do compile }
+!
+! PR 128017
+
+module m
+ implicit none
+ complex, target :: z(2) = [(1., 2.), (3., 4.)]
+contains
+ function f() result(r)
+ class(*), pointer :: r(:)
+ r => z
+ end function
+ function fa() result(r)
+ class(*), allocatable :: r(:)
+ r = z
+ end function
+end module
+
+program p
+ use m
+ implicit none
+
+ associate (a => fa())
+ select type (a) ! { dg-error "variable definition context" }
+ type is (complex)
+ a(1) = (0., 0.) ! { dg-error "variable definition context" }
+ end select
+ end associate
+
+ associate (a => (f()))
+ select type (a) ! { dg-error "variable definition context" }
+ type is (complex)
+ a(1) = (0., 0.) ! { dg-error "variable definition context" }
+ end select
+ end associate
+
+ associate (a => f())
+ select type (a)
+ type is (complex)
+ a(1) = (0., 0.)
+ end select
+ end associate
+end program
--
2.56.0