Bug 70233 - Missing diagnostic in array constructor, different size strings
Summary: Missing diagnostic in array constructor, different size strings
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-15 05:56 UTC by Jerry DeLisle
Modified: 2019-01-20 15:16 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-03-15 00:00:00


Attachments
Patch to generate error message (341 bytes, patch)
2016-03-15 20:58 UTC, Harald Anlauf
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jerry DeLisle 2016-03-15 05:56:13 UTC
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
Comment 1 Dominique d'Humieres 2016-03-15 09:02:11 UTC
Confirmed from 4.8 up to trunk (6.0).
Comment 2 Harald Anlauf 2016-03-15 20:58:46 UTC
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
Comment 3 Jerry DeLisle 2016-03-16 03:55:22 UTC
Harold,

Thanks for the help and I will test
Comment 4 Harald Anlauf 2016-03-17 20:48:45 UTC
(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...
Comment 5 Dominique d'Humieres 2016-03-17 20:58:12 UTC
> 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)
Comment 6 Jerry DeLisle 2016-03-18 01:24:46 UTC
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.
Comment 7 Harald Anlauf 2016-03-18 19:21:36 UTC
(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?
Comment 8 Dominique d'Humieres 2016-03-21 10:08:20 UTC
> 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 ".
Comment 9 Jerry DeLisle 2016-03-21 19:09:58 UTC
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.