Bug 36526 - pointer in pure function
Summary: pointer in pure function
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.1
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2008-06-13 12:57 UTC by Bálint Aradi
Modified: 2008-11-27 22:23 UTC (History)
1 user (show)

See Also:
Host:
Target: x86_64-unknown-linux-gnu
Build:
Known to work: 4.4.0
Known to fail:
Last reconfirmed: 2008-06-20 10:43:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bálint Aradi 2008-06-13 12:57:38 UTC
Hi, 

 I discovered a problem with gfortran 4.3.1 when compiling code with pure function with pointer argument. Inside the pure function, the argument is passed further to an other function. This seems to work, as long this other function is an intrinsic function, but seems to fail for user defined pure functions. The effect is demonstrated by the code below.

  Best regards,

    Bálint

--------------------------------------------------------
module TestPure
  implicit none
  
  type T1
    character(10) :: str
  end type T1

contains

  pure function getT1Len(self) result(t1len)
    type(T1), pointer :: self
    integer :: t1len

    t1len = getStrLen(self%str)
    !t1len = len_trim(self%str)   ! <- this would work
    
  end function getT1Len


  pure function getStrLen(str) result(length)
    character(*), intent(in) :: str
    integer :: length

    length = len_trim(str)

  end function getStrLen

end module TestPure


program Test
  use TestPure
  implicit none

  type(T1), pointer :: pT1

  allocate(pT1)
  pT1%str = "test"
  write (*,*) getT1Len(pT1)
  deallocate(pT1)
  
end program Test
------------------------------------------------------
Comment 1 Tobias Burnus 2008-06-13 17:52:33 UTC
CONFIRM. Works with other compilers. The constraint checked for is:

"C1272 In a pure subprogram any designator with a base object that is in common or accessed by host or use association, is a dummy argument of a pure function, is a dummy argument with INTENT (IN) of a pure subroutine, or an object that is storage associated with any such variable, shall not be used in the following contexts: [...]
(5) As an actual argument associated with a dummy argument with INTENT (OUT) or INTENT (INOUT) or with the POINTER attribute." (From F2003.)

At a glance, it seems as if this applies, but as the non-normative note indicates, C1272 does not apply in this case:

"NOTE 12.45  Pure subroutines are included to allow subroutine calls from pure procedures in a safe way [...] The constraints for pure subroutines are
based on the same principles as for pure functions, except that side effects to INTENT (OUT), INTENT (INOUT), and pointer dummy arguments are permitted."

However, I failed to find this in the normative part; I will try a bit more to figure out why C1272 does not apply.
Comment 2 Paul Thomas 2008-06-20 10:43:30 UTC
(In reply to comment #1)
This is due to an error in interface.c.  The following fixes the fault, bootstraps and regtests OK.  I will apply as obvious just as soon as I can.

Paul

Index: gcc/fortran/interface.c
===================================================================
*** gcc/fortran/interface.c     (revision 136870)
--- gcc/fortran/interface.c     (working copy)
*************** check_intents (gfc_formal_arglist *f, gf
*** 2379,2385 ****
              return FAILURE;
            }
  
!         if (a->expr->symtree->n.sym->attr.pointer)
            {
              gfc_error ("Procedure argument at %L is local to a PURE "
                         "procedure and has the POINTER attribute",
--- 2379,2385 ----
              return FAILURE;
            }
  
!         if (f->sym->attr.pointer)
            {
              gfc_error ("Procedure argument at %L is local to a PURE "
                         "procedure and has the POINTER attribute",
Comment 3 Paul Thomas 2008-06-25 23:05:18 UTC
Subject: Bug 36526

Author: pault
Date: Wed Jun 25 23:04:33 2008
New Revision: 137125

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=137125
Log:
2008-06-25  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/36526
	* interface.c (check_intents):  Correct error where the actual
	arg was checked for a pointer argument, rather than the formal.

2008-06-25  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/36526
	* gfortran.dg/proc_formal_proc_2.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/pure_formal_proc_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/testsuite/ChangeLog

Comment 4 Joost VandeVondele 2008-08-08 22:23:37 UTC
this bug seems fixed in 4.4.0, should it be closed?
Comment 5 Paul Thomas 2008-11-18 10:01:59 UTC
(In reply to comment #4)
> this bug seems fixed in 4.4.0, should it be closed?
> 

Joost,

I forgot the PR and missed your prompt - I'll apply it to 4.3 and close it.

Thanks

Paul
Comment 6 Paul Thomas 2008-11-27 22:21:48 UTC
Subject: Bug 36526

Author: pault
Date: Thu Nov 27 22:20:27 2008
New Revision: 142248

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=142248
Log:
2008-11-27  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/36526
	* interface.c (check_intents):  Correct error where the actual
	arg was checked for a pointer argument, rather than the formal.

2008-11-27  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/36526
	* gfortran.dg/pure_formal_proc_2.f90: New test.


Added:
    branches/gcc-4_3-branch/gcc/testsuite/gfortran.dg/pure_formal_proc_2.f90
Modified:
    branches/gcc-4_3-branch/gcc/fortran/ChangeLog
    branches/gcc-4_3-branch/gcc/fortran/interface.c
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 7 Paul Thomas 2008-11-27 22:23:34 UTC
Fixed on trunk and 4.3.

Thanks for the report.

Paul