User account creation filtered due to spam.

Bug 35846 - ICE on nested character constructors
Summary: ICE on nested character constructors
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: Daniel Kraft
URL:
Keywords: ice-on-valid-code
Depends on:
Blocks: 32834
  Show dependency treegraph
 
Reported: 2008-04-06 16:19 UTC by Tobias Burnus
Modified: 2008-09-21 15:36 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.4.0
Last reconfirmed: 2008-09-18 18:22:59


Attachments
Backtrace (1.79 KB, text/plain)
2008-04-13 20:32 UTC, Thomas Koenig
Details
Hackish patch (280 bytes, patch)
2008-05-20 07:33 UTC, Daniel Kraft
Details | Diff
Testcase without nested array constructors (260 bytes, text/plain)
2008-05-21 15:06 UTC, Daniel Kraft
Details
Testcase without nested array constructors (260 bytes, text/plain)
2008-05-21 15:06 UTC, Daniel Kraft
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2008-04-06 16:19:29 UTC
The following program gives an ICE. One reason could be the global variables. (cf. PR27997 comment 21).

implicit none
integer i
character(len=2) :: c(3)
c = 'a'
c = [ [ trim(c(1)), 'a' ]//'c', 'cd' ]
print *, c
end

Variants to check (or combinations of those):
- Compile with -fbounds-check
- Use  c = '' or  c = 'ab'
- Use typespec after PR27997 has been fixed, either on the inner or on the outer loop. E.g.
  c = [ [ character(1):: trim(c(1)), 'a' ]//'c', 'cd' ]
etc.
Comment 1 Thomas Koenig 2008-04-13 20:32:48 UTC
Created attachment 15474 [details]
Backtrace

Confirmed.  Backtrace is attached.
Comment 2 Daniel Kraft 2008-05-20 07:33:10 UTC
Created attachment 15655 [details]
Hackish patch

It seems the problem lies in gfc_conv_string_length, that can be called with a gfc_charlen whose length is NULL.   This patch is somewhat a prove of concept of this, just creating a dummy length if it is NULL there; this prevents the ICEs described above.

Of course this is surely not the correct place to do this initialization, I'll try to find it out later and work out a real patch.
Comment 3 Daniel Kraft 2008-05-21 15:06:00 UTC
Created attachment 15663 [details]
Testcase without nested array constructors

Actually, this does not depend on nested array constructors as this test-case proves (and also has nothing to do with the globals used for bounds-checking, it seems).

I believe the problem is that the gfc_expr* corresponding to the concatenation doesn't have a cl->length set on its ts (because the arguments don't have one when resolution is called); this NULL later causes the ICE.  When this expression turns up in an assignment, this does not matter as the expected length is known from the target's character length definition.

But when there is no "expected" charlen (like in the nested constructor or in a function call as in this test) it is needed and the NULL pointer dereferenced.  Hopefully I can soon come up with a real solution to this patch.
Comment 4 Daniel Kraft 2008-05-21 15:06:07 UTC
Created attachment 15664 [details]
Testcase without nested array constructors

Actually, this does not depend on nested array constructors as this test-case proves (and also has nothing to do with the globals used for bounds-checking, it seems).

I believe the problem is that the gfc_expr* corresponding to the concatenation doesn't have a cl->length set on its ts (because the arguments don't have one when resolution is called); this NULL later causes the ICE.  When this expression turns up in an assignment, this does not matter as the expected length is known from the target's character length definition.

But when there is no "expected" charlen (like in the nested constructor or in a function call as in this test) it is needed and the NULL pointer dereferenced.  Hopefully I can soon come up with a real solution to this problem.
Comment 5 Daniel Kraft 2008-09-20 11:52:53 UTC
After coming back to this bug, I believe the problem is that gfc_conv_expr takes care of finding out string lengths' for things like TRIM(x) which don't have a cl->length, but gfc_conv_expr_descriptor which is called e.g. for the array constructors does not do so and calls gfc_conv_string_length with a cl whose length is NULL.

As the problem seems to be specific to array constructors and all array constructor elements must have the same character length, I propose to re-use gfc_conv_expr in gfc_conv_string_length to find out the string length if it is NULL by taking the first element of each array constructor and then calling gfc_conv_expr to get the result's string_length.

I'll submit a patch shortly if no further problems occur.
Comment 6 Daniel Kraft 2008-09-21 15:35:09 UTC
Subject: Bug 35846

Author: domob
Date: Sun Sep 21 15:33:37 2008
New Revision: 140529

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=140529
Log:
2008-09-21  Daniel Kraft  <d@domob.eu>

	PR fortran/35846
	* trans.h (gfc_conv_string_length): New argument `expr'.
	* trans-expr.c (flatten_array_ctors_without_strlen): New method.
	(gfc_conv_string_length): New argument `expr' that is used in a new
	special case handling if cl->length is NULL.
	(gfc_conv_subref_array_arg): Pass expr to gfc_conv_string_length.
	* trans-array.c (gfc_conv_expr_descriptor): Ditto.
	(gfc_trans_auto_array_allocation): Pass NULL as new expr.
	(gfc_trans_g77_array), (gfc_trans_dummy_array_bias): Ditto.
	(gfc_trans_deferred_array): Ditto.
	(gfc_trans_array_constructor): Save and restore old values of globals
	used for bounds checking.
	* trans-decl.c (gfc_trans_dummy_character): Ditto.
	(gfc_trans_auto_character_variable): Ditto.

2008-09-21  Daniel Kraft  <d@domob.eu>

	PR fortran/35846
	* gfortran.dg/nested_array_constructor_1.f90: New test.
	* gfortran.dg/nested_array_constructor_2.f90: New test.
	* gfortran.dg/nested_array_constructor_3.f90: New test.
	* gfortran.dg/nested_array_constructor_4.f90: New test.
	* gfortran.dg/nested_array_constructor_5.f90: New test.
	* gfortran.dg/nested_array_constructor_6.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/nested_array_constructor_1.f90
    trunk/gcc/testsuite/gfortran.dg/nested_array_constructor_2.f90
    trunk/gcc/testsuite/gfortran.dg/nested_array_constructor_3.f90
    trunk/gcc/testsuite/gfortran.dg/nested_array_constructor_4.f90
    trunk/gcc/testsuite/gfortran.dg/nested_array_constructor_5.f90
    trunk/gcc/testsuite/gfortran.dg/nested_array_constructor_6.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog

Comment 7 Daniel Kraft 2008-09-21 15:36:36 UTC
Fixed on trunk.