In the following code, one case of the constructor gives an error and one does not. integer, parameter :: char_len = 32 character(char_len), allocatable :: ch_array(:) character(char_len) :: ch, bh allocate(ch_array(5)) print *,lbound(ch_array), ubound(ch_array) ch = "a" bh = "b" ! ch_array = [bh, "def", ch, ch, bh]! no error given ch_array = ["def", bh, ch, ch, ch] ! error given print *, ch_array deallocate(ch_array) end
Confirmed from 4.8 up to trunk (6.0).
Created attachment 37982 [details] Patch to generate error message The above patch generates consistent error messages: pr70233.f90:9:19: ch_array = [bh, "def", ch, ch, bh]! no error given 1 Error: Different CHARACTER lengths (32/3) in array constructor at (1) pr70233.f90:10:22: ch_array = ["def", bh, ch, ch, ch] ! error given 1 Error: Different CHARACTER lengths (3/32) in array constructor at (1) Is that what you want? Then feel free to use it. Harald
Harold, Thanks for the help and I will test
(In reply to Harald Anlauf from comment #2) > Created attachment 37982 [details] > Patch to generate error message This patch leads to serious regressions. Do not yet understand why. Withdrawing...
> This patch leads to serious regressions. Do not > yet understand why. Withdrawing... Confirmed. Many failures are of the kind /opt/gcc/_clean/gcc/testsuite/gfortran.dg/array_constructor_type_16.f03:21:59: carr = [ CHARACTER(len=6) :: "foo", [ CHARACTER(len=4) :: "foobar", "xyz" ] ] 1 Error: Different CHARACTER lengths (4/6) in array constructor at (1) Another one is /opt/gcc/_clean/gcc/testsuite/gfortran.dg/unlimited_polymorphic_1.f03:158:16: call foo([sun, " & rain"], res) 1 Error: Different CHARACTER lengths (8/7) in array constructor at (1)
The failures I looked at were becasue the constructors were using strings of different sizes. So my question was going to be what are the rules. Are the strings suppose to get padded to the length of the character array element? All elements must be of the same size so I assume if the length is say six, you can not use "abc" in a constructor and one must use "abc " I did not check all the failures, but at least one requires the -fbackslash and with the patch passes fine if I pad out the strings in the constructor.
(In reply to Jerry DeLisle from comment #6) > The failures I looked at were becasue the constructors were using strings of > different sizes. So my question was going to be what are the rules. Are the > strings suppose to get padded to the length of the character array element? Other compiler give error messages (if standard conformance is required) ifort: pr70233.f90(9): warning #8208: If type specification is omitted, each ac-value expression in the array constructor of type CHARACTER must have the same length type parameters. ['def'] ch_array = [bh, "def", ch, ch, bh]! no error given --------------------^ pr70233.f90(10): warning #8208: If type specification is omitted, each ac-value expression in the array constructor of type CHARACTER must have the same length type parameters. ['def'] ch_array = ["def", bh, ch, ch, ch] ! error given ----------------^ sunf95: ch_array = [bh, "def", ch, ch, bh]! no error given ^ "pr70233.f90", Line = 9, Column = 21: ERROR: Array constructor values of type character must all have the same length. ch_array = ["def", bh, ch, ch, ch] ! error given ^ "pr70233.f90", Line = 10, Column = 24: ERROR: Array constructor values of type character must all have the same length. ^ "pr70233.f90", Line = 10, Column = 28: ERROR: Array constructor values of type character must all have the same length. ^ "pr70233.f90", Line = 10, Column = 32: ERROR: Array constructor values of type character must all have the same length. ^ "pr70233.f90", Line = 10, Column = 36: ERROR: Array constructor values of type character must all have the same length. f90comp: 16 SOURCE LINES f90comp: 5 ERRORS, 0 WARNINGS, 0 OTHER MESSAGES, 0 ANSI So I'd say it's ok to give an error message here, unless you want to make silent padding a gnu extension. ;-) > All elements must be of the same size so I assume if the length is say six, > you can not use "abc" in a constructor and one must use "abc " > > I did not check all the failures, but at least one requires the -fbackslash > and with the patch passes fine if I pad out the strings in the constructor. The patch produces misleading error messages for valid code, as in: character(4), parameter :: chr (4) = (/"abcd", "efgh", "ijkl", "mnop"/) character(4), parameter :: chr1(2) = (/chr(2)(2:3) , chr(3)(3:4)/) ! OK character(4), parameter :: chr2(2) = (/chr(2:3)(2:3)/) ! Error? character(4), parameter :: chr3(2) = (/chr(2:2)(2:3), chr(3)(3:4)/) ! Error? end pr70233b.f90:1:48: character(4), parameter :: chr (4) = (/"abcd", "efgh", "ijkl", "mnop"/) 1 Error: Different CHARACTER lengths (2/4) in array constructor at (1) Could this be an issue with substrings of array sections?
> The failures I looked at were becasue the constructors were using strings > of different sizes. So my question was going to be what are the rules. > Are the strings suppose to get padded to the length of the character > array element? > > All elements must be of the same size so I assume if the length is say six, > you can not use "abc" in a constructor and one must use "abc " > > I did not check all the failures, but at least one requires the -fbackslash > and with the patch passes fine if I pad out the strings in the constructor. From my copy of the F2015 draft: 4.8 Construction of array values 1 An array constructor constructs a rank-one array value from a sequence of scalar values, array values, and implied DO loops. R469 array-constructor is (/ ac-spec /) or lbracket ac-spec rbracket R470 ac-spec is type-spec :: or [type-spec ::] ac-value-list R471 lbracket is [ R472 rbracket is ] R473 ac-value is expr or ac-implied-do R474 ac-implied-do is ( ac-value-list , ac-implied-do-control ) R475 ac-implied-do-control is [ integer-type-spec :: ] ac-do-variable = scalar-int-expr , scalar-int-expr [ , scalar-int-expr ] R476 ac-do-variable is do-variable ... 2 If type-spec is omitted, corresponding length type parameters of the declared type of each ac-value expression shall have the same value; in this case, the declared type and type parameters of the array constructor are those of the ac-value expressions. 3 If type-spec appears, it specifies the declared type and type parameters of the array constructor. Each ac-value expression in the array-constructor shall be compatible with intrinsic assignment to a variable of this type and type parameters. Each value is converted to the type and type parameters of the array-constructor in accordance with the rules of intrinsic assignment (7.2.1.3). From (2), I think call foo([sun, " & rain"], res) in gfortran.dg/unlimited_polymorphic_1.f03 is invalid (" & rain" should be replaced with " & rain ", note that compiling the test with -fcheck=all results in At line 113 of file /opt/gcc/_clean/gcc/testsuite/gfortran.dg/unlimited_polymorphic_1.f03 Fortran runtime error: Pointer actual argument 'u2' is not associated ). From (3), the constructs such as [ CHARACTER(len=4) :: "foobar", "xyz" ] are valid, "footer" being truncated to "food" and "xyz" padded to "xyz ".
Dominique, thanks for finding this. It even makes sense; if the typespec is given one knows what the size of the string is. If not given, how does one decide which is the right size, so require that they all be equal.