Bug 100218 - Allow target of the pointer resulting from the evaluation of function-reference in a variable definition context
Summary: Allow target of the pointer resulting from the evaluation of function-referen...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: anlauf
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2021-04-22 19:09 UTC by anlauf
Modified: 2021-04-27 21:23 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-04-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description anlauf 2021-04-22 19:09:00 UTC
While analyzing PR100154, Tobias pointed me to the following:

F2018:R902: function-reference shall have a data pointer result

A variable is either the data object denoted by designator or the target of the pointer resulting from the evaluation of function-reference; this pointer shall
be associated.

He also gave an example in that other PR, for which I have a fix.  In my
interpretation the following code should thus also be valid and is accepted
by two other compilers I tested (Intel, Nvidia) and gives the right result.

program p
  implicit none
  integer, target :: z = 0
  call g (f ())
  print *, z
contains
  function f () result (r)
    integer, pointer :: r
    r => z
  end function f
  subroutine g (x)
    integer, intent(out) :: x
    x = 1
  end subroutine g
end program p


The following patch seems to help:

diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c
index 92a6700568d..696b9f1daac 100644
--- a/gcc/fortran/expr.c
+++ b/gcc/fortran/expr.c
@@ -6121,7 +6132,9 @@ gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
     }
   if (!pointer && sym->attr.flavor != FL_VARIABLE
       && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result)
-      && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer))
+      && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer)
+      && !(sym->attr.flavor == FL_PROCEDURE
+	   && sym->attr.function && sym->attr.pointer))
     {
       if (context)
 	gfc_error ("%qs in variable definition context (%s) at %L is not"
Comment 2 GCC Commits 2021-04-24 18:39:30 UTC
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:32c4d970ea3a9fc330d6aa8fd83f9dae0b9afc64

commit r12-106-g32c4d970ea3a9fc330d6aa8fd83f9dae0b9afc64
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Sat Apr 24 20:38:06 2021 +0200

    Fortran - allow target of pointer from evaluation of function-reference
    
    Fortran allows the target of a pointer from the evaluation of a
    function-reference in a variable definition context (e.g. F2018:R902).
    
    gcc/fortran/ChangeLog:
    
            PR fortran/100218
            * expr.c (gfc_check_vardef_context): Extend check to allow pointer
            from a function reference.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/100218
            * gfortran.dg/ptr-func-4.f90: New test.
Comment 3 GCC Commits 2021-04-27 21:13:41 UTC
The releases/gcc-11 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:5aee8c2a33ae0d3f375ed0ca6e03718b284d2574

commit r11-8308-g5aee8c2a33ae0d3f375ed0ca6e03718b284d2574
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Sat Apr 24 20:38:06 2021 +0200

    Fortran - allow target of pointer from evaluation of function-reference
    
    Fortran allows the target of a pointer from the evaluation of a
    function-reference in a variable definition context (e.g. F2018:R902).
    
    gcc/fortran/ChangeLog:
    
            PR fortran/100218
            * expr.c (gfc_check_vardef_context): Extend check to allow pointer
            from a function reference.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/100218
            * gfortran.dg/ptr-func-4.f90: New test.
    
    (cherry picked from commit 32c4d970ea3a9fc330d6aa8fd83f9dae0b9afc64)
Comment 4 anlauf 2021-04-27 21:23:58 UTC
Fixed on mainline for gcc-12, and on 11-branch.  Closing.