------- Comment #1 from pault at gcc dot gnu dot org  2008-04-17 05:59 -------
Here is a further reduced version of the testcase:

program testprog
  type :: t_type
    integer, dimension(:), allocatable :: chars
  end type t_type
  integer, save :: callnb = 0
  type(t_type) :: this
  allocate ( this % chars ( 4))
  print * , "match :", recursivefunc ( this ), callnb
contains
  recursive function recursivefunc ( this ) result ( match )
    type(t_type), intent(in) :: this
    type(t_type) :: subpattern

    logical :: match

    callnb = callnb + 1
    match = (callnb == 10)

    if ((.NOT. allocated (this % chars)) .OR. match) return
    allocate ( subpattern % chars ( 4 ) )
    match = recursivefunc ( subpattern )
  end function recursivefunc
end program testprog

The code that the function produces is:

recursivefunc (struct t_type & this)
{
  logical(kind=4) match;
  static struct t_type subpattern = {};

  subpattern.chars.data = 0B;
  callnb = callnb + 1;
  match = callnb == 10;
  if ((integer(kind=4)[0:] *) this->chars.data == 0B || match) goto
__return_recursivefunc;
  {
    void * D.998;

    subpattern.chars.dtype = 265;
    subpattern.chars.dim[0].lbound = 1;
    subpattern.chars.dim[0].ubound = 4;
    subpattern.chars.dim[0].stride = 1;
    if (subpattern.chars.data == 0B)
      {
        {
          void * D.999;

          D.999 = __builtin_malloc (16);
          if (D.999 == 0B)
            {
              _gfortran_os_error (&"Out of memory"[1]{lb: 1 sz: 1});
            }
          D.998 = D.999;
        }
      }
    else
      {
        _gfortran_runtime_error (&"Attempting to allocate already allocated
array"[1]{lb: 1 sz: 1});
      }
    subpattern.chars.data = D.998;
    subpattern.chars.offset = -1;
  }
  match = recursivefunc (&subpattern);
  __return_recursivefunc:;
  if (subpattern.chars.data != 0B)
    {
      __builtin_free (subpattern.chars.data);
    }
  subpattern.chars.data = 0B;
  return match;
}

Being a local variable in a recursive procedure, it is declared to be static. 
Having allocatable components, it is nullified on entry to the procedure.  This
explains the observed behaviour.

I believe that it is a bug but will have to take a close look at the standard
to confirm it - in fact, I have a suspicion that it might not be covered.  If
the latter is the case, I would be inclined to modify gfortran because the
unexpectd behaviour.

Confirmed for now

Paul


-- 

pault at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pault at gcc dot gnu dot org
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2008-04-17 05:59:41
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959

Reply via email to