Bug 85603 - ICE with character array substring assignment
Summary: ICE with character array substring assignment
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 9.0
: P4 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords:
Depends on:
Blocks: 68241
  Show dependency treegraph
 
Reported: 2018-05-02 01:33 UTC by Walter Spector
Modified: 2019-01-25 19:02 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-05-03 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Walter Spector 2018-05-02 01:33:08 UTC
The following test case causes an ICE.  First noticed with v5.4, but also happens with v4.8 and a 4/26/2018 snapshot of v9.0.0.  Intel and NAG compile and run this.  PGI v17.10 also gets an ICE...:

program strlen_bug
  implicit none

  character(:), allocatable :: strings(:)
  integer :: maxlen

  strings = [ character(32) ::  &
      'short',  &
      'somewhat longer' ]
  maxlen = maxval (len_trim (strings))
  print *, 'max length =', maxlen
  ! causes ICE !
  strings = strings(:)(:maxlen)
  print *, strings

end program

wws@w6ws-4:/rootsda5/home/wws/fortran/array_cons$ /usr/local/gcc-trunk/bin/gfortran --version
GNU Fortran (GCC) 9.0.0 20180426 (experimental)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

wws@w6ws-4:/rootsda5/home/wws/fortran/array_cons$ /usr/local/gcc-trunk/bin/gfortran strlen_bug.f90
strlen_bug.f90:13:0:

   strings = strings(:)(:maxlen)
 
internal compiler error: Segmentation fault
0xce260f crash_signal
	../../gcc-trunk/gcc/toplev.c:325
0x7722a7 gfc_alloc_allocatable_for_assignment(gfc_loopinfo*, gfc_expr*, gfc_expr*)
	../../gcc-trunk/gcc/fortran/trans-array.c:9915
0x7a3c24 gfc_trans_assignment_1
	../../gcc-trunk/gcc/fortran/trans-expr.c:10329
0x754537 trans_code
	../../gcc-trunk/gcc/fortran/trans.c:1828
0x7e7b2f gfc_trans_block_construct(gfc_code*)
	../../gcc-trunk/gcc/fortran/trans-stmt.c:2058
0x754327 trans_code
	../../gcc-trunk/gcc/fortran/trans.c:1924
0x788f45 gfc_generate_function_code(gfc_namespace*)
	../../gcc-trunk/gcc/fortran/trans-decl.c:6507
0x7081d0 translate_all_program_units
	../../gcc-trunk/gcc/fortran/parse.c:6121
0x7081d0 gfc_parse_file()
	../../gcc-trunk/gcc/fortran/parse.c:6324
0x75107f gfc_be_parse_file
	../../gcc-trunk/gcc/fortran/f95-lang.c:204
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
wws@w6ws-4:/rootsda5/home/wws/fortran/array_cons$
Comment 1 Dominique d'Humieres 2018-05-03 09:32:12 UTC
Confirmed from at least 4.8 up to trunk (9.0).

I am pretty sure this is a duplicate.

The code compiles if I replace

  strings = strings(:)(:maxlen)

with

  strings(1) = strings(1)(:maxlen)
  strings(2) = strings(2)(:maxlen)

but this does not set the length of 'strings' to 'maxlen'.

IIRC I have recently seen a post saying that substrings of string array sections are forbidden by the standard.
Comment 2 Paul Thomas 2018-09-22 09:40:47 UTC
Hi Walt,

I am on a roll with deferred length string bugs so I will take this one.

I already have a fix for it which I will transmit to the list in the next few days. (I am just setting off for a 'break' in Wales. Both my wife and daughter have work to do so I might as well join them:-) )

Paul
Comment 3 Paul Thomas 2018-09-22 10:21:57 UTC
Author: pault
Date: Sat Sep 22 10:21:25 2018
New Revision: 264502

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

	PR fortran/85603
	* trans-array.c (gfc_alloc_allocatable_for_assignment): Test
	the charlen backend_decl before using the VAR_P macro.

2018-09-22  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/85603
	* gfortran.dg/deferred_character_23.f90 : New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/deferred_character_23.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Walter Spector 2018-09-23 23:37:34 UTC
Hi Paul,

I built an updated compiler that includes your fix.  The ICE is gone - thanks!  However the assignment is still not correctly compiled.

The example should be reallocating the character string length of the array to 15.  Unfortunately it remains at 32.  Slightly longer example:

program strlen_bug
  implicit none

  character(:), allocatable :: strings(:)
  integer :: maxlen

  strings = [ character(32) ::  &
      'short',  &
      'somewhat longer' ]
  maxlen = maxval (len_trim (strings))
  print *, 'max length =', maxlen

! Used to cause an ICE
  strings = strings(:)(:maxlen) ! Should realloc
  print *, strings
  print *, 'string length =', len (strings)

end program

wws@w6ws-4:/rootsda5/home/wws/fortran/gfortran$ /usr/local/gcc-9/bin/gfortran -frealloc-lhs strlen_bug.f90
wws@w6ws-4:/rootsda5/home/wws/fortran/gfortran$ a.out
 max length =          15
 short                           somewhat longer                 
 string length =          32
wws@w6ws-4:/rootsda5/home/wws/fortran/gfortran$ /usr/local/gcc-9/bin/gfortran --version
GNU Fortran (GCC) 9.0.0 20180922 (experimental)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

wws@w6ws-4:/rootsda5/home/wws/fortran/gfortran$ 

Note that I tried explicitly adding the -frealloc-lhs option, but it didn't make a difference either way.
Comment 5 Paul Thomas 2018-10-23 08:28:07 UTC
Author: pault
Date: Tue Oct 23 08:27:14 2018
New Revision: 265412

URL: https://gcc.gnu.org/viewcvs?rev=265412&root=gcc&view=rev
Log:
2018-10-23  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/85603
	* frontend-passes.c (get_len_call): New function to generate a
	call to intrinsic LEN.
	(create_var): Use this to make length expressions for variable
	rhs string lengths.
	Clean up some white space issues.

2018-10-23  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/85603
	* gfortran.dg/deferred_character_23.f90 : Check reallocation is
	occurring as it should and a regression caused by version 1 of
	this patch.


Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/frontend-passes.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/deferred_character_23.f90
Comment 6 Paul Thomas 2019-01-25 19:02:49 UTC
Fixed on trunk. Thanks for the report.

Paul