Paul Richard Thomas wrote:
+ /* Unlimited polymorphic pointers should have their vptr nullified. */
>+ if (UNLIMITED_POLY (sym) && CLASS_DATA (sym)->attr.pointer)
>+ gfc_defer_symbol_init (sym);
>
>
>Why? If the pointer has never been pointer-associated, one shouldn't access
It's so that SAME_TYPE_AS and EXTENDS_TYPE_OF do the right thing with
unassociated pointers. I am not sure that I understand your concern
if the code is needed.
It's not really a concern. I just see it as missed code-size/performance
optimization as I believe that it is not needed.
Adding
ptr->_vptr = NULL
directly after declaration of a local pointer variable is pointless.
Such a variable has an undefined pointer association status. And for
same_type_as and extends_type_of the argument may not be such a pointer.
Only unassociated pointers are allowed. In order to create such a
pointer, one either needs an initialization like in
class(*), pointer :: ptr => null()
or has to explicitly call "ptr=>null()" or nullify(ptr)". That's not
different to other pointers and in particular not different to
CLASS(<name>) types.
In other words, the following program is invalid as "ptr" has an
undefined pointer association status:
type(t), target :: x
class(*), pointer :: ptr
print *, same_type_as (ptr, x) ! <<< invalid: "foo" is an undefined pointer
while the following code is valid (and causes an ICE):
type(t), target :: x
class(*), pointer :: ptr => NULL() ! pointer initialization
print *, same_type_as (ptr, x)
as is
type(t), target :: x
class(*), pointer :: ptr
! ptr => x ! optionally
ptr => NULL()
print *, same_type_as (ptr, x)
* * *
By the way, the following code also causes an ICE. I think it is valid
since Fortran 2008:
type t
end type t
type(t), target :: x
class(*), pointer :: ptr => x
print *, same_type_as (ptr, x)
end
Namely:
"R442 component-initialization is [...] or => initial-data-target"
"C460 (R442) If initial-data-target appears, component-name shall be
data-pointer-initialization compatible with it."
"If initial-data-target appears for a data pointer component, that
component in any object of the type is initially associated with the
target or becomes associated with the target as specied in 16.5.2.3."
Tobias