Bug 82923 - Automatic allocation of deferred length character using function result
Summary: Automatic allocation of deferred length character using function result
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 7.2.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-11-09 16:43 UTC by werner.blokbuster
Modified: 2018-05-20 10:18 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-11-09 00:00:00


Attachments
Fix for the bug (429 bytes, patch)
2017-11-17 19:30 UTC, Paul Thomas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description werner.blokbuster 2017-11-09 16:43:50 UTC
I can't believe that this isn't a duplicate although I can't find it. The following code gives an ICE for me on gfortran 7.2, if the line mine=getchars(2,4) is included:


module m
    implicit none
contains
    function getchars(my_len,my_size)
        integer, intent(in) :: my_len, my_size
        character(my_len) :: getchars(my_size)
            getchars = 'A-'
    end function getchars
end module m

program testca

    use m, only: getchars
    implicit none
    character(:), allocatable :: mine(:)

    ! this works:
    write(*,*) getchars(2,4)
    write(*,*) len(getchars(2,4))
    write(*,*) size(getchars(2,4))

    ! this works:
    mine = [character(2) :: 'A-','A-']
    write(*,*) mine

    ! ICE occurs if this line is included:
    mine = getchars(2,4)

end program testca

WB
Comment 1 Dominique d'Humieres 2017-11-09 17:07:18 UTC
Confirmed from 4.8 up to trunk (8.0). Note that if the compiler is configured with --enable-checking=yes, I get

pr82923.f90:11:0:

 program testca
 
Error: Local declaration from a different function
..__result
pr82923.f90:27:0:

     mine = getchars(2,4)
 
note: in statement
.mine = ..__result;
pr82923.f90:11:0:

 program testca
 
Error: Local declaration from a different function
..__result
pr82923.f90:27:0:

     mine = getchars(2,4)
 
note: in statement
_101 = (integer(kind=8)) ..__result;
pr82923.f90:11:0:

 program testca
 
Error: Local declaration from a different function
..__result
pr82923.f90:27:0:

     mine = getchars(2,4)
 
note: in statement
D.3753 = (sizetype) ..__result;
during GIMPLE pass: cfg
pr82923.f90:11:0:

 program testca
 
internal compiler error: verify_gimple failed

instead of

during RTL pass: expand
pr82923.f90:27:0:

     mine = getchars(2,4)
 
internal compiler error: in expand_expr_real_1, at expr.c:9865

for 5.5.0 to 8.0 and

pr82923.f90:27:0: internal compiler error: in gimplify_var_or_parm_decl, at gimplify.c:1741
     mine = getchars(2,4)
 ^
for 4.8 and 4.9.

No duplicate in store.
Comment 2 Paul Thomas 2017-11-15 09:44:33 UTC
Ha! That's an 'amusing' one. I too am surprised that there is no duplicate.

I'll take it.

Paul
Comment 3 Paul Thomas 2017-11-17 19:30:42 UTC
Created attachment 42637 [details]
Fix for the bug

This one does the job and it regtests OK. However, I'll wait until I get PR78990 out of the way before submitting.

Paul
Comment 4 Dominique d'Humieres 2017-11-19 10:49:48 UTC
> Created attachment 42637 [details]
> Fix for the bug
>
> This one does the job and it regtests OK.

Confirmed, however I see an ICE when compiling the "type" variant of pr65381.
Comment 5 Dominique d'Humieres 2017-11-19 19:15:24 UTC
> Confirmed, however I see an ICE when compiling the "type" variant of pr65381.

I have forgotten to say that the ICE is similar to the one in comment 1

pr65381_red.f90:15:0:

     pure function fixedStringTable(this) result(fixed)
 
Error: Local declaration from a different function
D.3828
pr65381_red.f90:15:0:

     pure function fixedStringTable(this) result(fixed)
 
note: in statement
D.3828 = MAX_EXPR <_8, 0>;
pr65381_red.f90:15:0:

     pure function fixedStringTable(this) result(fixed)
 
Error: Local declaration from a different function
D.3828
pr65381_red.f90:15:0:

     pure function fixedStringTable(this) result(fixed)
 
note: in statement
_11 = (sizetype) D.3828;
pr65381_red.f90:15:0:

     pure function fixedStringTable(this) result(fixed)
 
Error: Local declaration from a different function
D.3828
pr65381_red.f90:15:0:

     pure function fixedStringTable(this) result(fixed)
 
note: in statement
D.3894 = (sizetype) D.3828;
pr65381_red.f90:15:0:

     pure function fixedStringTable(this) result(fixed)
 
Error: Local declaration from a different function
D.3828
pr65381_red.f90:26:0:

                 fixed(k) = this(i)%list(j)%chars
 
note: in statement
D.3883 = D.3828;
during GIMPLE pass: cfg
pr65381_red.f90:15:0:

     pure function fixedStringTable(this) result(fixed)
 
internal compiler error: verify_gimple failed
Comment 6 Dominique d'Humieres 2018-05-19 09:25:46 UTC
> I can't believe that this isn't a duplicate although I can't find it. ...

PRs 66694 and 82617 are "fixed" by the patch at https://gcc.gnu.org/ml/fortran/2018-05/msg00046.html.
Comment 7 Paul Thomas 2018-05-19 10:50:22 UTC
Author: pault
Date: Sat May 19 10:49:50 2018
New Revision: 260392

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

	PR fortran/82923
	PR fortran/66694
	PR fortran/82617
	* trans-array.c (gfc_alloc_allocatable_for_assignment): Set the
	charlen backend_decl of the rhs expr to ss->info->string_length
	so that the value in the current scope is used.

2018-05-19  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/82923
	* gfortran.dg/allocate_assumed_charlen_4.f90: New test. Note
	that the patch fixes PR66694 & PR82617, although the testcases
	are not explicitly included.


Added:
    trunk/gcc/testsuite/gfortran.dg/allocate_assumed_charlen_4.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/testsuite/ChangeLog
Comment 8 Paul Thomas 2018-05-19 11:33:02 UTC
Author: pault
Date: Sat May 19 11:32:30 2018
New Revision: 260395

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

	PR fortran/82923
	PR fortran/66694
	PR fortran/82617
	* trans-array.c (gfc_alloc_allocatable_for_assignment): Set the
	charlen backend_decl of the rhs expr to ss->info->string_length
	so that the value in the current scope is used.

2018-05-19  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/82923
	* gfortran.dg/allocate_assumed_charlen_4.f90: New test. Note
	that the patch fixes PR66694 & PR82617, although the testcases
	are not explicitly included.


Added:
    branches/gcc-8-branch/gcc/testsuite/gfortran.dg/allocate_assumed_charlen_4.f90
Modified:
    branches/gcc-8-branch/gcc/fortran/ChangeLog
    branches/gcc-8-branch/gcc/fortran/trans-array.c
    branches/gcc-8-branch/gcc/testsuite/ChangeLog
Comment 9 Paul Thomas 2018-05-19 14:54:30 UTC
Author: pault
Date: Sat May 19 14:53:58 2018
New Revision: 260399

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

	PR fortran/82923
	PR fortran/66694
	PR fortran/82617
	* trans-array.c (gfc_alloc_allocatable_for_assignment): Set the
	charlen backend_decl of the rhs expr to ss->info->string_length
	so that the value in the current scope is used.

2018-05-19  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/82923
	* gfortran.dg/allocate_assumed_charlen_4.f90: New test. Note
	that the patch fixes PR66694 & PR82617, although the testcases
	are not explicitly included.


Added:
    branches/gcc-7-branch/gcc/testsuite/gfortran.dg/allocate_assumed_charlen_4.f90
Modified:
    branches/gcc-7-branch/gcc/fortran/ChangeLog
    branches/gcc-7-branch/gcc/fortran/trans-array.c
    branches/gcc-7-branch/gcc/testsuite/ChangeLog
Comment 10 Paul Thomas 2018-05-19 14:56:29 UTC
Fixed on 7-branch through to trunk.

Thanks for the report and sorry that it took so long.

Paul
Comment 11 Paul Thomas 2018-05-19 14:57:02 UTC
I suppose that I have to mark it as resolved :-)
Comment 12 Paul Thomas 2018-05-20 10:00:29 UTC
Author: pault
Date: Sun May 20 09:59:54 2018
New Revision: 260413

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

	PR fortran/82923
	PR fortran/66694
	PR fortran/82617
	* trans-array.c (gfc_alloc_allocatable_for_assignment): Set the
	charlen backend_decl of the rhs expr to ss->info->string_length
	so that the value in the current scope is used.

2018-05-19  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/82923
	* gfortran.dg/allocate_assumed_charlen_4.f90: New test. Note
	that the patch fixes PR66694 & PR82617, although the testcases
	are not explicitly included.


Added:
    trunk/gcc/testsuite/gfortran.dg/select_type_42.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/match.c
    trunk/gcc/testsuite/ChangeLog
Comment 13 Paul Thomas 2018-05-20 10:18:44 UTC
> 
> Added:
>     trunk/gcc/testsuite/gfortran.dg/select_type_42.f90
> Modified:
>     trunk/gcc/fortran/ChangeLog
>     trunk/gcc/fortran/match.c
>     trunk/gcc/testsuite/ChangeLog

Sorry, this last was the patch for PR80657

Paul.