User account creation filtered due to spam.

Bug 49630 - [OOP] ICE on obsolescent deferred-length type bound character function
Summary: [OOP] ICE on obsolescent deferred-length type bound character function
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords:
: 69965 (view as bug list)
Depends on:
Blocks: 68241
  Show dependency treegraph
 
Reported: 2011-07-04 15:31 UTC by Hans-Werner Boschmann
Modified: 2016-04-28 06:39 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-06-12 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hans-Werner Boschmann 2011-07-04 15:31:52 UTC
Here is one more weird piece of code:

module abc
  implicit none
  type,abstract::abc_abstract
   contains
     procedure(abc_interface),deferred::abc_function
  end type abc_abstract
  type,extends(abc_abstract)::abc_type
   contains
     procedure::abc_function
  end type abc_type
  abstract interface
     function abc_interface(this)
       import abc_abstract
       class(abc_abstract),intent(in)::this
       character(len=*)::abc_interface !obsolescent feature
     end function abc_interface
  end interface
contains
  function abc_function(this)
    class(abc_type),intent(in)::this
    character(len=5)::abc_function
    abc_function="hello"
  end function abc_function
  subroutine do_something(this)
    class(abc_abstract),intent(in)::this
    print *,this%abc_function()
  end subroutine do_something
end module abc

gcc 4.7 terminates with a segmentation fault. I get an ICE error message on my full program, but it turned to a segfault in this reduced module.

Anyway this code doesn't look right, so I have tried some workarounds like allocatable characters. But those ended up in different compiler errors.

So what is the state of allocatable character functions? Are they supposed to work?
Comment 1 Tobias Burnus 2011-07-04 15:53:29 UTC
Ragarding assumed-length functions: They should be supported, but no one has tried something fancy (i.e. beyond F77 style of features) with them. They are also really ugly.

Your test case fails for me with:

test.f90: In function ‘do_something’:
test.f90:26:0: internal compiler error: in build_int_cst_wide, at tree.c:1218

However, I think it is invalid to place an assumed-character-length function into an INTERFACE. See PR 41604. (Cf. also PR 46588.)


> gcc 4.7 terminates with a segmentation fault. I get an ICE error message on my
> full program, but it turned to a segfault in this reduced module.

On my system with a slightly dated (20110629) gfortran, I get:

test.f90: In function ‘do_something’:
test.f90:26:0: internal compiler error: in build_int_cst_wide, at tree.c:1218


> So what is the state of allocatable character functions? Are they supposed to
> work?

You think mean those with deferred-length type parameter - the others should already work just fine.

For deferred length: I think basic support is there, but there are still some issues. Cf. also PR 49110 and PR 45170 comment 9.
Comment 2 Hans-Werner Boschmann 2011-07-04 19:29:50 UTC
"C417: A function name shall not be declared with an asterisk type-param-value unless it is of type CHAR- ACTER and is the name of the result of an external function or the name of a dummy function."

So it is allowed to put an assumed character length into an interface, but not in the way as I did in the code above. My example is invalid.

But how would I declare an abstract interface of a function with variable length? I am allowed to use any scalar integer expression as length parameter of a character function, but I cannot leave it empty in the interface. That's why I use character(:),allocatable::res with the NAG fortran compiler. As far a I know, this is the only valid way and that's why I am looking forward for gfortran to implement it.
Comment 3 janus 2011-07-04 20:21:55 UTC
Here is a related deferred-length example:


module abc
  implicit none

  type::abc_type
   contains
     procedure::abc_function
  end type abc_type

contains

  function abc_function(this)
    class(abc_type),intent(in)::this
    character(:),allocatable::abc_function
    allocate(abc_function,source="hello")
  end function abc_function

  subroutine do_something(this)
    class(abc_type),intent(in)::this
    print *,this%abc_function()
  end subroutine do_something

end module abc


use abc
type(abc_type) :: a
call do_something(a)
end


This currently ICEs, but when changing the type-bound call in the "print" line to a plain function call, i.e. "abc_function(this)", then it is accepted and gives the expected output.
Comment 4 Dominique d'Humieres 2013-06-12 10:01:31 UTC
For the code in comment #0, I get the following ICE:

In function 'do_something':
Segmentation fault
     print *,this%abc_function()
 ^

Internal compiler error: Error reporting routines re-entered.
gfcc: internal compiler error: Abort trap (program f951)

or

pr49630.f90: In function 'do_something':
pr49630.f90:26:0: internal compiler error: in build_int_cst_wide, at tree.c:1213
     print *,this%abc_function()
 ^

pr49630.f90:26:0: internal compiler error: Abort trap
gfca: internal compiler error: Abort trap (program f951)

for versions configured with --enable-checking=release.

AFAICT the code in comment #1 compiles with 4.8 and trunk (it is not supported in 4.7).
Comment 5 Vittorio Zecca 2014-04-29 16:16:18 UTC
Running the test case with gfortran 4.9.0 
I get a shift larger than HOST_BITS_PER_WIDE_INT (64 on x86-64)
in double-int.c:675 "m = ((unsigned HOST_WIDE_INT) 2 << (prec - 1)) - 1;"
because prec is 295.
Maybe this is connected to the original bug?
Comment 6 Paul Thomas 2016-01-15 20:34:31 UTC
Author: pault
Date: Fri Jan 15 20:33:58 2016
New Revision: 232450

URL: https://gcc.gnu.org/viewcvs?rev=232450&root=gcc&view=rev
Log:
2016-01-15  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/64324
	* resolve.c (check_uop_procedure): Prevent deferred length
	characters from being trapped by assumed length error.

	PR fortran/49630
	PR fortran/54070
	PR fortran/60593
	PR fortran/60795
	PR fortran/61147
	PR fortran/64324
	* trans-array.c (gfc_conv_scalarized_array_ref): Pass decl for
	function as well as variable expressions.
	(gfc_array_init_size): Add 'expr' as an argument. Use this to
	correctly set the descriptor dtype for deferred characters.
	(gfc_array_allocate): Add 'expr' to the call to
	'gfc_array_init_size'.
	* trans.c (gfc_build_array_ref): Expand logic for setting span
	to include indirect references to character lengths.
	* trans-decl.c (gfc_get_symbol_decl): Ensure that deferred
	result char lengths that are PARM_DECLs are indirectly
	referenced both for directly passed and by reference.
	(create_function_arglist): If the length type is a pointer type
	then store the length as the 'passed_length' and make the char
	length an indirect reference to it.
	(gfc_trans_deferred_vars): If a character length has escaped
	being set as an indirect reference, return it via the 'passed
	length'.
	* trans-expr.c (gfc_conv_procedure_call): The length of
	deferred character length results is set TREE_STATIC and set to
	zero.
	(gfc_trans_assignment_1): Do not fix the rse string_length if
	it is a variable, a parameter or an indirect reference. Add the
	code to trap assignment of scalars to unallocated arrays.
	* trans-stmt.c (gfc_trans_allocate): Remove 'def_str_len' and
	all references to it. Instead, replicate the code to obtain a
	explicitly defined string length and provide a value before
	array allocation so that the dtype is correctly set.
	trans-types.c (gfc_get_character_type): If the character length
	is a pointer, use the indirect reference.

2016-01-15  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/49630
	* gfortran.dg/deferred_character_13.f90: New test for the fix
	of comment 3 of the PR.

	PR fortran/54070
	* gfortran.dg/deferred_character_8.f90: New test
	* gfortran.dg/allocate_error_5.f90: New test

	PR fortran/60593
	* gfortran.dg/deferred_character_10.f90: New test

	PR fortran/60795
	* gfortran.dg/deferred_character_14.f90: New test

	PR fortran/61147
	* gfortran.dg/deferred_character_11.f90: New test

	PR fortran/64324
	* gfortran.dg/deferred_character_9.f90: New test

Added:
    trunk/gcc/testsuite/gfortran.dg/allocate_error_5.f90
    trunk/gcc/testsuite/gfortran.dg/deferred_character_10.f90
    trunk/gcc/testsuite/gfortran.dg/deferred_character_11.f90
    trunk/gcc/testsuite/gfortran.dg/deferred_character_12.f90
    trunk/gcc/testsuite/gfortran.dg/deferred_character_13.f90
    trunk/gcc/testsuite/gfortran.dg/deferred_character_14.f90
    trunk/gcc/testsuite/gfortran.dg/deferred_character_8.f90
    trunk/gcc/testsuite/gfortran.dg/deferred_character_9.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/fortran/trans-types.c
    trunk/gcc/fortran/trans.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Paul Thomas 2016-01-16 09:57:58 UTC
Fixed on trunk. I will wait a few weeks before fixing on 5-branch.

Paul
Comment 8 Dominique d'Humieres 2016-02-26 02:42:23 UTC
*** Bug 69965 has been marked as a duplicate of this bug. ***
Comment 9 Paul Thomas 2016-03-09 20:50:31 UTC
Author: pault
Date: Wed Mar  9 20:49:58 2016
New Revision: 234093

URL: https://gcc.gnu.org/viewcvs?rev=234093&root=gcc&view=rev
Log:
2016-03-09  Paul Thomas  <pault@gcc.gnu.org>

	Backport from trunk.
	PR fortran/69423
	* trans-decl.c (create_function_arglist): Deferred character
	length functions, with and without declared results, address
	the passed reference type as '.result' and the local string
	length as '..result'.
	(gfc_null_and_pass_deferred_len): Helper function to null and
	return deferred string lengths, as needed.
	(gfc_trans_deferred_vars): Call it, thereby reducing repeated
	code, add call for deferred arrays and reroute pointer function
	results. Avoid using 'tmp' for anything other that a temporary
	tree by introducing 'type_of_array' for the arrayspec type.

2016-03-09  Paul Thomas  <pault@gcc.gnu.org>

	Backport from trunk.
	PR fortran/64324
	* resolve.c (check_uop_procedure): Prevent deferred length
	characters from being trapped by assumed length error.

	Backport from trunk.
	PR fortran/49630
	PR fortran/54070
	PR fortran/60593
	PR fortran/60795
	PR fortran/61147
	PR fortran/64324
	* trans-array.c (gfc_conv_scalarized_array_ref): Pass decl for
	function as well as variable expressions.
	(gfc_array_init_size): Add 'expr' as an argument. Use this to
	correctly set the descriptor dtype for deferred characters.
	(gfc_array_allocate): Add 'expr' to the call to
	'gfc_array_init_size'.
	* trans.c (gfc_build_array_ref): Expand logic for setting span
	to include indirect references to character lengths.
	* trans-decl.c (gfc_get_symbol_decl): Ensure that deferred
	result char lengths that are PARM_DECLs are indirectly
	referenced both for directly passed and by reference.
	(create_function_arglist): If the length type is a pointer type
	then store the length as the 'passed_length' and make the char
	length an indirect reference to it.
	(gfc_trans_deferred_vars): If a character length has escaped
	being set as an indirect reference, return it via the 'passed
	length'.
	* trans-expr.c (gfc_conv_procedure_call): The length of
	deferred character length results is set TREE_STATIC and set to
	zero.
	(gfc_trans_assignment_1): Do not fix the rse string_length if
	it is a variable, a parameter or an indirect reference. Add the
	code to trap assignment of scalars to unallocated arrays.
	* trans-stmt.c (gfc_trans_allocate): Remove 'def_str_len' and
	all references to it. Instead, replicate the code to obtain a
	explicitly defined string length and provide a value before
	array allocation so that the dtype is correctly set.
	trans-types.c (gfc_get_character_type): If the character length
	is a pointer, use the indirect reference.

2016-03-09  Paul Thomas  <pault@gcc.gnu.org>

	Backport from trunk.
	PR fortran/69423
	* gfortran.dg/deferred_character_15.f90 : New test.

2016-03-09  Paul Thomas  <pault@gcc.gnu.org>

	Backport from trunk.
	PR fortran/49630
	* gfortran.dg/deferred_character_13.f90: New test for the fix
	of comment 3 of the PR.

	Backport from trunk.
	PR fortran/54070
	* gfortran.dg/deferred_character_8.f90: New test
	* gfortran.dg/allocate_error_5.f90: New test

	Backport from trunk.
	PR fortran/60593
	* gfortran.dg/deferred_character_10.f90: New test

	Backport from trunk.
	PR fortran/60795
	* gfortran.dg/deferred_character_14.f90: New test

	Backport from trunk.
	PR fortran/61147
	* gfortran.dg/deferred_character_11.f90: New test

	Backport from trunk.
	PR fortran/64324
	* gfortran.dg/deferred_character_9.f90: New test


Added:
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/allocate_error_5.f90
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/deferred_character_10.f90
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/deferred_character_11.f90
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/deferred_character_12.f90
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/deferred_character_13.f90
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/deferred_character_14.f90
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/deferred_character_15.f90
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/deferred_character_8.f90
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/deferred_character_9.f90
Modified:
    branches/gcc-5-branch/gcc/fortran/ChangeLog
    branches/gcc-5-branch/gcc/fortran/resolve.c
    branches/gcc-5-branch/gcc/fortran/trans-array.c
    branches/gcc-5-branch/gcc/fortran/trans-decl.c
    branches/gcc-5-branch/gcc/fortran/trans-expr.c
    branches/gcc-5-branch/gcc/fortran/trans-stmt.c
    branches/gcc-5-branch/gcc/fortran/trans-types.c
    branches/gcc-5-branch/gcc/fortran/trans.c
    branches/gcc-5-branch/gcc/testsuite/ChangeLog
Comment 10 Paul Thomas 2016-03-09 20:54:06 UTC
Fixed on trunk and 5-branch.

Thanks for the report.

Paul
Comment 11 Vittorio Zecca 2016-04-28 06:39:55 UTC
I am still having an ICE with gfortran 5.3.0 on the following

      module abc
      implicit none
      type,abstract::abc_abstract
      contains
      procedure(abc_interface),deferred::abc_function
      end type abc_abstract
      type,extends(abc_abstract)::abc_type
      contains
      procedure::abc_function
      end type abc_type
      abstract interface
      function abc_interface(this)
       import abc_abstract
       class(abc_abstract),intent(in)::this
       character(len=*)::abc_interface
      end function abc_interface
      end interface
      contains
      function abc_function(this)
      class(abc_type),intent(in)::this
      character(len=5)::abc_function
      abc_function="hello"
      end function abc_function
      subroutine do_something(this)
      class(abc_abstract),intent(in)::this
      print *,this%abc_function()
      end subroutine do_something
      end module abc

gfbug111.f:30:0:

       print *,this%abc_function()
 1
internal compiler error: in wide_int_to_tree, at tree.c:1465
0xc3eae3 wide_int_to_tree(tree_node*,
generic_wide_int<wide_int_ref_storage<false> > const&)
	/home/vitti/gcc-5.3.0/gcc/tree.c:1465
0xc3ebb9 build_int_cst(tree_node*, long)
	/home/vitti/gcc-5.3.0/gcc/tree.c:1273
0x83a9aa round_up_loc(unsigned int, tree_node*, unsigned int)
	/home/vitti/gcc-5.3.0/gcc/fold-const.c:16043
0xa555bb finalize_type_size
	/home/vitti/gcc-5.3.0/gcc/stor-layout.c:1802
0xa56766 layout_type(tree_node*)
	/home/vitti/gcc-5.3.0/gcc/stor-layout.c:2442
0xc3cb73 build_array_type_1
	/home/vitti/gcc-5.3.0/gcc/tree.c:7968
0x709a64 gfc_get_character_type_len_for_eltype(tree_node*, tree_node*)
	/home/vitti/gcc-5.3.0/gcc/fortran/trans-types.c:1048
0x6d16d7 gfc_conv_procedure_call(gfc_se*, gfc_symbol*,
gfc_actual_arglist*, gfc_expr*, vec<tree_node*, va_gc, vl_embed>*)
	/home/vitti/gcc-5.3.0/gcc/fortran/trans-expr.c:5644
0x6d1d6f gfc_conv_function_expr
	/home/vitti/gcc-5.3.0/gcc/fortran/trans-expr.c:6293
0x6d789a gfc_conv_expr_reference(gfc_se*, gfc_expr*)
	/home/vitti/gcc-5.3.0/gcc/fortran/trans-expr.c:7349
0x6f277f gfc_trans_transfer(gfc_code*)
	/home/vitti/gcc-5.3.0/gcc/fortran/trans-io.c:2394
0x6a4e47 trans_code
	/home/vitti/gcc-5.3.0/gcc/fortran/trans.c:1868
0x6f03a7 build_dt
	/home/vitti/gcc-5.3.0/gcc/fortran/trans-io.c:1921
0x6a4e67 trans_code
	/home/vitti/gcc-5.3.0/gcc/fortran/trans.c:1840
0x6c4ce3 gfc_generate_function_code(gfc_namespace*)
	/home/vitti/gcc-5.3.0/gcc/fortran/trans-decl.c:5851
0x6a8421 gfc_generate_module_code(gfc_namespace*)
	/home/vitti/gcc-5.3.0/gcc/fortran/trans.c:2036
0x662d4d translate_all_program_units
	/home/vitti/gcc-5.3.0/gcc/fortran/parse.c:5330
0x662d4d gfc_parse_file()
	/home/vitti/gcc-5.3.0/gcc/fortran/parse.c:5540
0x6a22b5 gfc_be_parse_file
	/home/vitti/gcc-5.3.0/gcc/fortran/f95-lang.c:229
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <http://gcc.gnu.org/bugs.html> for instructions.

Maybe it is related to this issue?