PING: [patch, fortran] PR 27997: Implement F2003-style array constructor with typespec
Tobias Burnus
burnus@net-b.de
Fri Apr 25 13:47:00 GMT 2008
FX wrote:
> I am wondering: could you add some testcases with failing conversion
> to the constructor type. I see four cases I'd like covered: a too long
> string, like that:
>
> write *, [ character(len=1) :: "I am too long", "me too" ]
I fail to see why this should fail. I think this is perfectly valid and
equivalent to ['I', 'm']; the point of the constructor is too allow
items which have different lengths. If they had all the same length,
there were no need for the typespec (in this case).
> and a value that doesn't fit in the constructor type, like that:
>
> write *, [ integer(4) :: huge(0_8) ]
I think one could add a range check here; with -fno-range-check it
should compile (and give with -Wconversion a warning), but with
-frange-check (= default) a overflow error should be printed.
> Also, I think we should also include a testcase with derived types:
Agreed.
> And constructors withing constructors, like this:
> print *, [ integer(kind=8) :: 4, [ integer(kind=4) :: 42, 12 ] ]
> print *, [ character(len=8) :: "foo", [ character(len=4) :: "bar",
> "gee" ] ]
I think this fails currently - at least for strings, possibly also with
-fbounds-check and other types; cf. PR35846. I think one problem are
global variables. Note the test case in the PR is without typespecs.
My suggestion is to get this patch in first and fix then the nested
constructors for inclusing typespec. One probably should add your tests
to the PR.
>> static void
>> gfc_trans_array_ctor_element (stmtblock_t * pblock, tree desc,
>> @@ -999,7 +1000,7 @@
>> - if (flag_bounds_check)
>> + if (flag_bounds_check && !typespec_ctor)
>
> I've tried to trigger that code and could not see it work. I cannot
> see a runtime error, neither with
> call test ("this is long")
> contains
> subroutine test(s)
> character(len=*) :: s
> print *, [ character(len=3) :: s, "foo" ]
I believe this program is valid and thus there should not be any
run-time error. This is similarly to:
character(len=5) :: a
a = 'foo'
a = 'foobar'
All of these are valid. For the first assignment the 'foo' is padded on
the right giving 'foo ' and for the second one it is truncated and
giving 'fooba'. There is no reason why constructors with typespec should
be different. Note also that there is in the Fortran 2003 standard the
following example:
(/ CHARACTER(LEN=7) :: ’Takata’, ’Tanaka’, ’Hayashi’ /)
which has a similar "problem": The first two names are too short.
Without the typespec the example were invalid as then all items have to
have the same length. [Without the typespec the compiler does not know
which length is the right one - six such as for "Takata" or seven as for
"Hayashi"; with -fbounds-checks gfortran prints an error in this case.]
Tobias
More information about the Fortran
mailing list