Bug 39988 - F2008: Default initialization, structure constructors, and allocatable components
Summary: F2008: Default initialization, structure constructors, and allocatable compon...
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.4.0
: P3 enhancement
Target Milestone: ---
Assignee: Paul Thomas
Depends on:
Blocks: F2008
  Show dependency treegraph
Reported: 2009-05-01 05:17 UTC by Walter Spector
Modified: 2015-02-13 14:15 UTC (History)
1 user (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2011-03-13 15:09:07

Un-regtested patch for the PR (340 bytes, patch)
2011-03-13 15:09 UTC, Paul Thomas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Walter Spector 2009-05-01 05:17:35 UTC
Consider the following test case:

program defallocatable
  implicit none

! Demonstrate structure constructors with default initialization
! and allocatable arrays.  (Unspecified in F2003, initialized to
! unallocated in F2008.)

  type mytype_t
    integer, allocatable :: data(:)
  end type

  type(mytype_t) :: my_object

  my_object = mytype_t ()
  print *, 'allocated =', allocated (my_object%data)

end program

When compiling with gfortran, we get:

$ gfortran defallocatable.f90

  my_object = mytype_t ()
Error: No initializer for component 'data' given in the structure constructor at (1)!

This may be a valid error in F2003.  In F2003 section 4.5.9 paragraph 3, it
states that "If a component with default initialization has no corresponding
component-data-source, then the default initialization is applied to that component."  But when a component is allocatable, it is not legal to specify
a component-data-source.  This creates a difference in how default initialization
works with structure constructors, and other places where default initialization
must take place.

In F2008 the above paragraph, this time in section 4.5.10 paragraph 3, adds:
"If an allocatable component has no corresponding component-data-source,
then that component has an allocation status of unallocated."  This closes
the hole and makes default initialization of allocatable components in
structure constructors work like it does everywhere else.

So this enhancement request is to consider gfortran allow the code to compile
under the F2008 semantics (which are seemingly undefined in F2003.)

FWIW, g95 and ifort have the same problem.
Comment 1 Paul Thomas 2011-03-13 15:09:07 UTC
Created attachment 23645 [details]
Un-regtested patch for the PR

Will regtest and submit once trunk is open again.

Comment 2 Dominique d'Humieres 2015-02-13 14:15:53 UTC
This PR seems fixed between revisions r219070 (2014-12-26) and r219099 (2014-12-29), likely r219098 (pr60357). Closing as FIXED.