https://gcc.gnu.org/g:d49facb09dfc467069b71a24b8a1de470dc472d1
commit r17-6520-gd49facb09dfc467069b71a24b8a1de470dc472d1 Author: Jerry DeLisle <[email protected]> Date: Sat Nov 7 18:14:31 2026 -0800 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. Diff: --- 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(-) diff --git a/gcc/fortran/primary.cc b/gcc/fortran/primary.cc index 84481cf35ae9..c644bb019835 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 359224de50c9..3372def44bc0 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 000000000000..6a32a21c1e22 --- /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 f467b8342928..379a2e7cbb77 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
