The initialization in following test case should be diagnosed as an error: $ cat nocomp7.f90 module nocomp7 implicit none type bad_t real, allocatable :: x(:) end type ! The following is illegal! type (bad_t) :: bad = bad_t ( (/ 1., 3., 5., 7., 9. /) ) end module $ $ gfortran --version GNU Fortran (GCC) 4.3.0 20071005 (experimental) [trunk revision 127783] Copyright (C) 2007 Free Software Foundation, Inc. GNU Fortran comes with NO WARRANTY, to the extent permitted by law. You may redistribute copies of GNU Fortran under the terms of the GNU General Public License. For more information about these matters, see the file named COPYING $ $ gfortran --pedantic -c nocomp7.f90 $
NAG f95: Error: adf.f90, line 10: Initialisation expression for BAD is not constant ifort: fortcom: Error: adf.f90, line 10: A data initialization-expr is not valid for this object. [BAD] type (bad_t) :: bad = bad_t ( (/ 1., 3., 5., 7., 9. /) ) ------------------^
(In reply to comment #0) > ! The following is illegal! > type (bad_t) :: bad = bad_t ( (/ 1., 3., 5., 7., 9. /) ) I don't get it. "Fortran 95/2003 explained" by Metcalf has exactly this in the example (figure 12.3, p243) for allocatable components. I don't have the standard section, but Metcalf states: "In a structure constructor, an expression corresponding to an allocatable component must be an array or a reference to the intrinsic function NULL with no arguments. [...] If it is an array, but not an allocatable array, the component is allocated with the same bounds and is assigned the same value." If compiled with "-std=f95", gfortran complains about allocatable components in general and accepts it with "-std=f2003". So, where's the actual problem?
(In reply to comment #2) > I don't get it. "Fortran 95/2003 explained" by Metcalf has exactly this in the > example (figure 12.3, p243) for allocatable components... So, where's the actual problem? The example on p243 correctly shows the use of a structure constructor in an assignment statement. This bug report is different in that it concerns a structure constructor in the initializer portion of a TYPE statement (where the derived type contains an ALLOCATABLE.)
(In reply to comment #3) > (In reply to comment #2) > > I don't get it. "Fortran 95/2003 explained" by Metcalf has exactly this in the > > example (figure 12.3, p243) for allocatable components... So, where's the actual problem? > > The example on p243 correctly shows the use of a structure constructor in an > assignment statement. This bug report is different in that it concerns a > structure constructor in the initializer portion of a TYPE statement (where > the derived type contains an ALLOCATABLE.) And while writing the above, I didn't take this as significant difference. However, it dawns on me that the crucial point is, that variables with initializer get the SAVE attribute which doesn't go well with the ALLOCATABLE components. Correct?
(In reply to comment #4) > ... it dawns on me that the crucial point is, that variables with > initializer get the SAVE attribute which doesn't go well with the ALLOCATABLE > components. Correct? I am not sure why they put the restriction in. But note that one *can* use null() in a structure constructor for the allocatable component. So the following is legal: type xyzzy integer, allocatable :: x(:) real :: y end type type(xyzzy) :: plugh = xyzzy (null (), 123.456) See 7.1.7(3) in F2003 (and 7.1.12(3) in the F2008 draft.)
(In reply to comment #5) > See 7.1.7(3) in F2003 (and 7.1.12(3) in the F2008 draft.) Walter, thanks for reference!
Subject: Bug 34402 Author: dfranke Date: Thu Dec 10 19:57:16 2009 New Revision: 155138 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=155138 Log: gcc/fortran/: 2009-12-10 Daniel Franke <franke.daniel@gmail.com> PR fortran/34402 * expr.c (check_alloc_comp_init): New. (check_init_expr): Verify that allocatable components are not data-initalized. gcc/testsuite/: 2009-12-10 Daniel Franke <franke.daniel@gmail.com> PR fortran/34402 * gfortran.dg/alloc_comp_init_expr.f03: New. Added: trunk/gcc/testsuite/gfortran.dg/alloc_comp_init_expr.f03 Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/expr.c trunk/gcc/testsuite/ChangeLog
Fixed in trunk. Closing.