Bug 56138 - Deferred-length character RESULT: ICE in gfc_add_modify_loc
Deferred-length character RESULT: ICE in gfc_add_modify_loc
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.8.0
: P3 normal
: ---
Assigned To: Paul Thomas
: ice-on-valid-code
Depends on: 51976
Blocks: 45170
  Show dependency treegraph
 
Reported: 2013-01-29 08:29 UTC by Tobias Burnus
Modified: 2013-04-21 17:52 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-01-30 00:00:00


Attachments
Fix for testcase in comment #0 (1.66 KB, patch)
2013-01-30 06:10 UTC, Paul Thomas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2013-01-29 08:29:52 UTC
See report at http://gcc.gnu.org/ml/fortran/2013-01/msg00206.html,
see F200x's version of iso varying string at ftp://ftp.nag.co.uk/sc22wg5/N1901-N1950/N1913.txt

The following test case - with and without PURE - fails with:

foo.f90: In function 's_to_c':
foo.f90:5:0: internal compiler error: in gfc_add_modify_loc, at fortran/trans.c:159
     s_to_c = string
 ^
0x5b8e97 gfc_add_modify_loc(unsigned int, stmtblock_t*, tree_node*, tree_node*)
        ../../gcc/fortran/trans.c:158
0x5f3af0 alloc_scalar_allocatable_for_assignment
        ../../gcc/fortran/trans-expr.c:7629
0x5f3af0 gfc_trans_assignment_1
        ../../gcc/fortran/trans-expr.c:7788


 PURE FUNCTION s_to_c (string)
  CHARACTER(LEN=*), INTENT(IN)  :: string 
  CHARACTER(LEN=:), ALLOCATABLE :: s_to_c 
  s_to_c = string
 ENDFUNCTION s_to_c
Comment 1 Paul Thomas 2013-01-30 06:10:59 UTC
Created attachment 29305 [details]
Fix for testcase in comment #0

Whilst the patch fixes the testcase and allows John Reid's version of iso_varying_string (ISO/IEC JTC1/SC22/WG5 N1913) to compile, the test programme does not, for the lack of deferred character length components in gfortran.  Note that ifort correctly compiles both.

Bootstraps and regtests OK with:

! { dg-do run }
! Checks the fix for PR56138.
!
! Taken from ISO/IEC JTC1/SC22/WG5 N1913 by John Reid
! Reported by Tobias Burnus  <burnus@gcc.gnu.org>
!
  character(Len = :), allocatable :: bar
  bar = "bar"
  if (LEN (s_to_c ("foo"//bar)) .ne. 6) call abort
  If (s_to_c (bar//"foo") .ne. "barfoo") call abort
contains
 PURE FUNCTION s_to_c (string)
  CHARACTER(LEN=*), INTENT(IN)  :: string
  CHARACTER(LEN=:), ALLOCATABLE :: s_to_c
  s_to_c = string
 ENDFUNCTION s_to_c
end

This PR, therefore becomes one to fix deferred character length components.
Comment 2 Paul Thomas 2013-01-30 06:14:53 UTC
The latter is pr51976.
Comment 3 Tobias Burnus 2013-01-30 07:47:20 UTC
Author: burnus
Date: Wed Jan 30 07:47:14 2013
New Revision: 195570

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195570
Log:
2012-01-30  Tobias Burnus  <burnus@net-b.de>

        PR fortran/56138
        * trans-decl.c (gfc_trans_deferred_vars): Fix deferred-length
        results for functions without extra result variable.

2012-01-30  Tobias Burnus  <burnus@net-b.de>

        PR fortran/56138
        * gfortran.dg/allocatable_function_6.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/allocatable_function_6.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Tobias Burnus 2013-01-30 07:54:25 UTC
FIXED on the 4.8 trunk.

Paul: Thanks for the review and sorry for causing work duplication.
Comment 5 Dominique d'Humieres 2013-01-30 13:05:48 UTC
Compiling the test in comment #0 still gives the same ICE with revision 195570. The tests in comment #1 and gfortran.dg/allocatable_function_6.f90 compile and execute without error.

One thing I don't understand is that

  CHARACTER(LEN=:), ALLOCATABLE :: s_to_c

is accepted in the test in comment #1, but rejected with (3 times)

  CHARACTER(LEN=:),ALLOCATABLE :: chars
                                       1
Error: Deferred-length character component 'chars' at (1) is not yet supported

in the code at ftp://ftp.nag.co.uk/sc22wg5/N1901-N1950/N1913.txt.

This seems to be due to

> type VARYING_STRING
>   CHARACTER(LEN=:),ALLOCATABLE :: chars
> end type VARYING_STRING

i.e.,  "CHARACTER(LEN=:),ALLOCATABLE :: chars" being in a type definition. Would it be possible to have a more explicit error?
Comment 6 Tobias Burnus 2013-01-30 13:49:27 UTC
REOPEN.

As Dominique showed (thanks!), this bug is not fixed. It is only fixed if the function is 'contained' in another one (e.g. main program) or in a module.

Additionally, the gfortran.dg/allocatable_function_6.f90 also passes without the patch.


Paul's patch fixes the issue (similarly to my original patch, which, however, regressed).

As Paul's patch doesn't regress: OK with reverting my patch and changing the test case to a noncontained procedure (plus interface block in the caller).
Comment 7 Paul Thomas 2013-01-31 07:59:51 UTC
(In reply to comment #6)
> REOPEN.
> 
> As Dominique showed (thanks!), this bug is not fixed. It is only fixed if the
> function is 'contained' in another one (e.g. main program) or in a module.
> 
> Additionally, the gfortran.dg/allocatable_function_6.f90 also passes without
> the patch.
> 
> 
> Paul's patch fixes the issue (similarly to my original patch, which, however,
> regressed).
> 
> As Paul's patch doesn't regress: OK with reverting my patch and changing the
> test case to a noncontained procedure (plus interface block in the caller).

I probably cannot attend to this until next week - I'll come back to you about it tomorrow.

Cheers

Paul
Comment 8 Dominique d'Humieres 2013-02-02 12:21:28 UTC
> Paul's patch fixes the issue (similarly to my original patch, which, however,
> regressed).

Confirmed.

> As Paul's patch doesn't regress: OK with reverting my patch and changing the
> test case to a noncontained procedure (plus interface block in the caller).

With the patch the following test compiles and runs:

! { dg-do run }
!
! PR fortran/56138
!
! Contributed by John Chludzinski, using the code of John Reid
!
implicit none
interface
PURE FUNCTION s_to_c(string)
  CHARACTER(LEN=*),INTENT(IN)   :: string
  CHARACTER(LEN=:),ALLOCATABLE :: s_to_c
ENDFUNCTION s_to_c
end interface
CHARACTER(LEN=:),ALLOCATABLE :: str 
if (s_to_c("ABCdef") /= "ABCdef" .or. len(s_to_c("ABCdef")) /= 6) call abort()
str = s_to_c("ABCdef")
if (str /= "ABCdef" .or. len(str) /= 6) call abort()
str(1:3) = s_to_c("123")
if (str /= "123def" .or. len(str) /= 6) call abort()

end

PURE FUNCTION s_to_c(string) 
  CHARACTER(LEN=*),INTENT(IN)   :: string 
  CHARACTER(LEN=:),ALLOCATABLE :: s_to_c 
  s_to_c = string
ENDFUNCTION s_to_c 

In addition the first test in pr54070 comment #0 compiles, while the two others still give an ICE at gimplify.c:2082. AFAICT the critical difference between these tests and the ones in this PR is that the former have an assumed shape for the allocatable result.
Comment 9 Tobias Burnus 2013-02-14 09:37:58 UTC
Author: burnus
Date: Thu Feb 14 09:37:53 2013
New Revision: 196047

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196047
Log:
2013-02-14  Paul Thomas  <pault@gcc.gnu.org>
            Tobias Burnus  <burnus@net-b.de>

        PR testsuite/56138
        * trans-decl.c (gfc_get_symbol_decl): Fix deferred-length
        results for functions without extra result variable.

        Revert:
        2013-01-30  Tobias Burnus  <burnus@net-b.de>

        PR fortran/56138
        * trans-decl.c (gfc_trans_deferred_vars): Fix deferred-length
        results for functions without extra result variable.

2013-02-14  Dominique d'Humieres  <dominiq@lps.ens.fr>
            Tobias Burnus  <burnus@net-b.de>

        PR testsuite/56138
        * gfortran.dg/allocatable_function_7.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/allocatable_function_7.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/testsuite/ChangeLog
Comment 10 Tobias Burnus 2013-02-14 10:09:31 UTC
Should now be really FIXED.