User account creation filtered due to spam.

Bug 33888 - ICE - CHARACTER expression using an ELEMENTAL FUNCTION as actual arg
Summary: ICE - CHARACTER expression using an ELEMENTAL FUNCTION as actual arg
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
Keywords: ice-on-valid-code
Depends on:
Blocks: 32834 33998
  Show dependency treegraph
Reported: 2007-10-25 01:34 UTC by Walter Spector
Modified: 2007-12-16 11:41 UTC (History)
3 users (show)

See Also:
Known to work:
Known to fail: 4.1.2 4.2.2 4.3.0
Last reconfirmed: 2007-11-30 17:05:04


Note You need to log in before you can comment on or make changes to this bug.
Description Walter Spector 2007-10-25 01:34:38 UTC
The following causes an ICE:

$ cat ftn95bug.f90
program ftn95bug
  implicit none

  character(8) :: indata(4) = (/  &
    '12344321', '98766789', 'abcdefgh', 'ABCDEFGH'  &

  call process (myfunc (indata))  ! <- This causes a gfortran ICE !


  elemental function myfunc (s)
    character(*), intent(in) :: s
    character(len (s)) :: myfunc

    myfunc = s

  end function

  subroutine process (strings)
    character(*), intent(in) :: strings(:)

    print *, strings

  end subroutine

end program

wws@gallifrey /cygdrive/d/usr/wws/fortran/utils
$ gfortran --version
GNU Fortran (GCC) 4.3.0 20071005 (experimental) [trunk revision 127783]
Copyright (C) 2007 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING

wws@gallifrey /cygdrive/d/usr/wws/fortran/utils
$ gfortran ftn95bug.f90
ftn95bug.f90:8: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <> for instructions.

wws@gallifrey /cygdrive/d/usr/wws/fortran/utils
Comment 1 Tobias Burnus 2007-10-25 06:30:06 UTC

==28333== Invalid read of size 8
==28333==    at 0x73B006: get_frame_type (tree-nested.c:198)
==28333==    by 0x73B387: get_chain_decl (tree-nested.c:304)
==28333==    by 0x73C55C: get_nonlocal_debug_decl (tree-nested.c:851)
==28333==    by 0x7401EA: convert_nonlocal_reference (tree-nested.c:922)
==28333==    by 0x872C1C: walk_tree_1 (tree.c:8369)

where in gcc/tree-nested.c:
get_frame_type (struct nesting_info *info)
  tree type = info->frame_type; // <------- line 198
Comment 2 Paul Thomas 2007-10-25 09:20:40 UTC
(In reply to comment #1)
For some reason, the interface mechanism in trans-expr.c is failing for this case of an elemental function (try a constant length for my_func or to make it non-elemental and array valued - both work fince).  You can see this in the code that the testcase produces.  The internal length variable, in my_func, '..length' is being referenced in the main program, with inevitable consequences!

A workaround, for now, is to write a temporary, which can be allocatable if necessary.

character(8) :: temp(4)
temp = myfunc (indata))
call process (temp)

A tricky alternative, to make use of automatic allocation and cleanup of allocatable components, would be

type mytype
  character(8), allocatable :: c(:)
end type mytype
type(mytype) :: temp
temp%c = myfunc (indata))
call process (temp%c)

However, this segfaults for the same reason as the original. *sigh*

Comment 3 Paul Thomas 2007-11-30 17:05:04 UTC
I can see what to do - watch this spot.

Comment 4 Paul Thomas 2007-12-16 11:34:29 UTC
Subject: Bug 33888

Author: pault
Date: Sun Dec 16 11:34:08 2007
New Revision: 130988

2007-12-16  Paul Thomas  <>

	PR fortran/31213
	PR fortran/33888
	PR fortran/33998
	* trans-array.c (gfc_trans_array_constructor_value): If the
	iterator variable does not have a backend_decl, use a local
	(get_elemental_fcn_charlen): New function to map the character
	length of an elemental function onto its actual arglist.
	(gfc_conv_expr_descriptor): Call the above so that the size of
	the temporary can be evaluated.
	* trans-expr.c : Include arith.h and change prototype of
	gfc_apply_interface_mapping_to_expr to return void.  Change all
	references to gfc_apply_interface_mapping_to_expr accordingly.
	(gfc_free_interface_mapping): Free the 'expr' field.
	(gfc_add_interface_mapping): Add an argument for the actual
	argument expression. This is copied to the 'expr' field of the
	mapping.  Only stabilize the backend_decl if the se is present.
	Copy the character length expression and only add it's backend
	declaration if se is present.  Return without working on the
	backend declaration for the new symbol if se is not present.
	(gfc_map_intrinsic_function) : To simplify intrinsics 'len',
	'size', 'ubound' and 'lbound' and then to map the result.
	(gfc_map_fcn_formal_to_actual): Performs the formal to actual
	mapping for the case of a function found in a specification
	expression in the interface being mapped.
	(gfc_apply_interface_mapping_to_ref): Remove seen_result and
	all its references. Remove the inline simplification of LEN
	and call gfc_map_intrinsic_function instead.  Change the
	order of mapping of the actual arguments and simplifying
	intrinsic functions.  Finally, if a function maps to an
	actual argument, call gfc_map_fcn_formal_to_actual.
	(gfc_conv_function_call): Add 'e' to the call to
	* dump-parse-tree.c (gfc_show_symbol_n): New function for
	diagnostic purposes.
	* gfortran.h : Add prototype for gfc_show_symbol_n.
	* trans.h : Add 'expr' field to gfc_add_interface_mapping.
	Add 'expr' to prototype for gfc_show_symbol_n.
	* resolve.c (resolve_generic_f0): Set specific function as

2007-12-16  Paul Thomas  <>

	PR fortran/31213
	PR fortran/33888
	PR fortran/33998
	* gfortran.dg/mapping_1.f90: New test.
	* gfortran.dg/mapping_2.f90: New test.
	* gfortran.dg/mapping_3.f90: New test.


Comment 5 Paul Thomas 2007-12-16 11:41:48 UTC
Fixed on trunk