The following valid program is rejected with integer(1), parameter :: ARR1(len(HEX1)) = [( MSKa1(i), i=1,len(HEX1) )] 1 Error: index in dimension 1 is out of bounds at (1) module abuse_mod implicit none integer i character(8), parameter :: HEX1 = '40490FDB' integer(1), parameter :: MSKa1(len(HEX1)) = [(1,i=1,len(HEX1))] integer(1), parameter :: ARR1(len(HEX1)) = [( MSKa1(i), i=1,len(HEX1) )] end module abuse_mod For a longer example (which uses supported vendor extensions), see: http://groups.google.com/group/comp.lang.fortran/msg/2499294cb9a33299
4.2 gives a second error: integer(1), parameter :: ARR1(len(HEX1)) = [( MSKa1(i), i=1,len(HEX1) )] 1 Error: index in dimension 1 is out of bounds at (1) pr34476.f90:5.44: integer(1), parameter :: MSKa1(len(HEX1)) = [(1,i=1,len(HEX1))] 1 Error: Parameter array 'mska1' at (1) cannot be automatic or assumed shape Paul
Here is an additional variant of this bug: program init_bug implicit none integer :: i character(11), parameter :: string="hello world" ! This compiles: character, parameter :: up_string(len (string)) = & (/ (string(i:i), i=1, len (string)) /) integer, parameter :: bytes(len (string)) = & (/ (iachar (string(i:i)), i=1, len (string)) /) integer, parameter :: up_bytes(len (string)) = & (/ (iachar (up_string(i)), i=1, size (up_string)) /) print *, string print *, up_string print *, bytes print *, up_bytes end program Besides the out-of-bounds error, it also gives a 'cannot appear in the expression error': (/ (iachar (up_string(i)), i=1, size (up_string)) /) 1 Error: index in dimension 1 is out of bounds at (1) abuse_init4.f90:12.25: (/ (iachar (string(i:i)), i=1, len (string)) /) 1 Error: Variable 'i' cannot appear in the expression at (1) abuse_init4.f90:21.19: print *, up_bytes 1 Error: Symbol 'up_bytes' at (1) has no IMPLICIT type
The first part of this PR is fixed with: Index: gcc/fortran/expr.c =================================================================== *** gcc/fortran/expr.c (revision 130811) --- gcc/fortran/expr.c (working copy) *************** find_array_element (gfc_constructor *con *** 1026,1035 **** } /* Check the bounds. */ ! if (ar->as->upper[i] ! && (mpz_cmp (e->value.integer, ar->as->upper[i]->value.integer) > 0 ! || mpz_cmp (e->value.integer, ! ar->as->lower[i]->value.integer) < 0)) { gfc_error ("index in dimension %d is out of bounds " "at %L", i + 1, &ar->c_where[i]); --- 1026,1039 ---- } /* Check the bounds. */ ! if ((ar->as->upper[i] ! && ar->as->upper[i]->expr_type == EXPR_CONSTANT ! && mpz_cmp (e->value.integer, ! ar->as->upper[i]->value.integer) > 0) ! || ! (ar->as->lower[i]->expr_type == EXPR_CONSTANT ! && mpz_cmp (e->value.integer, ! ar->as->lower[i]->value.integer) < 0)) { gfc_error ("index in dimension %d is out of bounds " "at %L", i + 1, &ar->c_where[i]); The second is due to the gfc_error at expr.c(check_restricted):2495 There is some attribute that 'i' has which should make it an exception - I just do not quite see what it is right now. Paul
Created attachment 14898 [details] A fix and testcase for this PR This is regtesting right now. Paul
Subject: Bug 34476 Author: pault Date: Tue Jan 8 15:14:33 2008 New Revision: 131396 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131396 Log: 2008-01-08 Paul Thomas <pault@gcc.gnu.org> PR fortran/34476 * expr.c (find_array_element): Check that the array bounds are constant before using them. Use lower, as well as upper bound. (check_restricted): Allow implied index variable. 2008-01-08 Paul Thomas <pault@gcc.gnu.org> PR fortran/34476 * gfortran.dg/parameter_array_init_3.f90: New test. Added: trunk/gcc/testsuite/gfortran.dg/parameter_array_init_3.f90 Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/expr.c trunk/gcc/testsuite/ChangeLog
Fixed on trunk Paul