(Class-) array finalization and initialization question

Andre Vehreschild vehre@gmx.de
Wed Feb 18 13:56:00 GMT 2015


Dear all,

while resolving pr60322 I ran into a regression with the test
gfortran.dg/finalize_15.f90 (one of two remaining to fix 60322 completely w/o
regressions). Side note: although the tests ends in f90 it tests for F2008
features. May be rename to .f08 is more appropriate.

I have reduced the testcase as attached. The crucial point is the finalize
routine:
  
  type t1
    integer :: i = 42
  contains
    final :: fin
  end type t1

  elemental subroutine fin(x)
    type(t1), intent(inout) :: x
    x%i = -13 * x%i
  end subroutine fin

The finalizer multiply with -13. In connection with the later defined init():

  subroutine init(x)
    class(t1), intent(out) :: x(:,:)
  end subroutine init

Note the intent(out) here, which is the second crucial point and in combination
with the call to init() in the main program:

  type(t1), allocatable :: x(:,:)
  allocate(t1 :: x(5,5))
  x%i = 1
  call init(x(::2, ::3))

one gets the remarkable result, that now x%i is -13 for the entries selected by
the strides. My question now is: why are those values -13 and not 42 as I would
expect from the default initializer? The array elements selected by the strides
have been undefined when calling init() as of F2008 5.3.10 §3 (first sentence)
hence calling the finalizer. But from that same sentence I would expect the
default-initializer to be set for the values instead.

Looking at the pseudo-code for the init()-routine one sees that:

init (struct __class_module_fin3_T1_2_0t & restrict x)
{
  if (x->_vptr->_final != 0B)
    {
      x->_vptr->_final (&x->_data, x->_vptr->_size, 0);
    }
  x->_vptr->_copy (x->_vptr->_def_init, &x->_data);
}

the finalizer *and* the copy of the initial-value is applied in the same way.
If one remove the "elemental" from the finalizer, one stays with the unchanged
field of all entries set to 1 (x%i = 1).

If one modifies the init()-routine to be elemental (and reseting fin() back
to elemental):
  
  elemental subroutine init(x)
    class(t1), intent(out) :: x

one goes with the behavior I expect of having the values selected by the
strides set to 42.

Now the questions:

1. Why shouldn't the effects of 'elemental subroutine init()', 'elemental
subroutine fin()', 'subroutine init()' and 'subroutine fin()' be identical in
that the array is changed in the same way?

2. Why would one expect -13 in the array for 'elemental fin()' and 'subroutine
init(x); class(t1), intent(out):: x(:,:)'? IMHO is the processing of the
default initialization erroneous for this case.

3. When fin is not elemental, why is it not executed to finalize the
elements selected by the stride in the call to init()? Do we need an
array-version of it?

4. In the case where the finalizer is elemental and init() an array-valued
intent(out) routine, is the pseudo-code generated for it wrong? IMHO there at
least the default initializer should be in a loop over the array elements.

Sorry, for these many questions, but fixing these last regressions is really
bothering me.

Regards,
	Andre
-- 
Andre Vehreschild * Email: vehre ad gmx dot de 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fin3.f90
Type: text/x-fortran
Size: 586 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20150218/51df7651/attachment.bin>


More information about the Fortran mailing list