A little pointer on POINTER in data would be helpful

Steve Kargl sgk@troutmask.apl.washington.edu
Thu Mar 22 23:38:00 GMT 2018


I don't use POINTERs in my codes, so have little
experience with them; especially when derived types
are involved.  In PR 70870, Gerhard added the code 

program p
   type t
      integer :: n
   end type
   type(t), pointer :: z
   data z%n / 3 /
   print *, z%n
end

which results in an ICE.  I'm trying to parse

  F2008:C567 (R536) A data-i-do-object or a variable
  that appears as a data-stmt-object shall not be an
  object designator in which a pointer appears other
  than as the entire rightmost part-ref.

My understanding is that the POINTER attribute applied
to z, which is not the rightmost part-ref.  The rightmost
is n.  Thus, the above code is violates a constraint.  I
think I know where to fix this situation.

So, I now, re-arrange the above code to

program p
   type t
      integer, pointer :: n
   end type
   type(t) :: z
   data z%n / 3 /
   print *, z%n
end

Here, the POINTER attribute applies to the component n,
and so it is the rightmost part.  This code actually
compiles, but segfaults on execution, which may be 
expected as '3' is not a TARGET.  The code is invalid,
but gfortran is not required to issues an error message.
The standard goes on to say 

   A data-stmt-constant shall be null-init or initial-data-target
   if and only if the corresponding data-stmt-object has the
   POINTER attribute.  If data-stmt-constant is null-init, the initial
   association status of the corresponding data statement object is
   disassociated.

OK, so change the code to

program p
   type t
      integer, pointer :: n
   end type
   type(t) :: z
   data z%n / null() /
   print *, associated(z%n)
end

% gfcx -o z b.f90 && ./z
 F

Good, gfortran gets this correct.  The stand goes on to say

   If data-stmt-constant is initial-data-target the corresponding
   data statement object shall be data-pointer-initialization
   compatible with the initial data target; the data statement
   object is initially associated with the target.

This is where it gets interesting.

R443 initial-data-target  is designator
R601 designator  is object-name
                 or array-element
                 or array-section
                 or coindexed-named-object
                 or complex-part-designator
                 or structure-component
                 or substring

After bouncing around abit, one finds

  C766  A designator that is an initial-data-target shall designate
  a nonallocatable, noncoindexed variable that has the TARGET and
  SAVE attributes and does not have a vector subscript.  Every
  subscript, section subscript, substring starting point, and
  substring ending point in designator shall be a constant expression.

program p
   integer, target, save :: m = 3
   type t
      integer, pointer :: n
   end type
   type(t) :: z
   data z%n / m /
   m = 3
   print *, z%n
end

% gfcx -o z b.f90
b.f90:7:15:

    data z%n / m /
               1
Error: Symbol 'm' must be a PARAMETER in DATA statement at (1)

Whoops.

-- 
Steve



More information about the Fortran mailing list