The following code triggers an ICE (also seen in some gfortran-7 version): module mod implicit none public contains function fun(cs) result(rs) character(len=:), allocatable :: rs character(len=*), intent(in) :: cs associate(l => len(cs)) allocate(character(len=1+l) :: rs) end associate end function fun end module mod gfortran -c assoc_len.f90 gives assoc_len.f90:13:0: allocate(character(len=1+l) :: rs) internal compiler error: in gfc_conv_expr_op, at fortran/trans-expr.c:3417 0x5b156d gfc_conv_expr_op ../.././gcc/fortran/trans-expr.c:3417 0x5b156d gfc_conv_expr(gfc_se*, gfc_expr*) ../.././gcc/fortran/trans-expr.c:7918 0x72bfb9 gfc_trans_allocate(gfc_code*) ../.././gcc/fortran/trans-stmt.c:6115 0x6bc4b7 trans_code ../.././gcc/fortran/trans.c:1996 0x72858f gfc_trans_block_construct(gfc_code*) ../.././gcc/fortran/trans-stmt.c:2058 0x6bc527 trans_code ../.././gcc/fortran/trans.c:1924 0x6e293b gfc_generate_function_code(gfc_namespace*) ../.././gcc/fortran/trans-decl.c:6507 0x6bfec9 gfc_generate_module_code(gfc_namespace*) ../.././gcc/fortran/trans.c:2222 0x6734f4 translate_all_program_units ../.././gcc/fortran/parse.c:6108 0x6734f4 gfc_parse_file() ../.././gcc/fortran/parse.c:6324 0x6b991f gfc_be_parse_file ../.././gcc/fortran/f95-lang.c:204 The problem is triggered by the combination of using associate in conjunction with the allocate. With a local variable instead of the associate for l, or with another statement instead of an allocate (i.e. "print *,l), gfortran compiles. Also when simplifying the length expression in the allocate from "len=1+l" to just "len=l" the ICE disappears.
Confirmed. The ICE is present since version 6. gfortran 5.5 rejects the example with this bogus error: associate(l => len(cs)) 1 Error: Symbol ‘l’ at (1) has no IMPLICIT type
Originally I came across this problem in a large module with a slightly more complex function with gfortran-7.3.0 (standard version from ubuntu 18.4). I got the same bogus error, which Janus has seen for this reduced testcase with gfortran-5.5. Taking out the function and related class I got the ICE, but was not able to reproduce this error. In short: I think that the bogus error is still present in recent gfortran versions (or is just another symptom of this problem).
With gfortran-12, the ICE is gone, but the produced error message does not look right. mod.f90:12:25: 12 | associate(l => len(cs)) | 1 Error: Symbol ‘l’ at (1) has no IMPLICIT type
Created attachment 56484 [details] Fix for this PR Somehow this missed being a blocker for the ASSOCIATE meta-bug. The patch is so unbelievably simple that I am going to think about it for 24 hours, even though it regtests just fine :-) module mod implicit none public contains function fun(cs) result(rs) character(len=:), allocatable :: rs character(len=*), intent(in) :: cs associate(l => len(cs)) allocate(character(len=1+l) :: rs) write (rs, '(i5)') l end associate end function fun end module mod use mod character(:), allocatable :: res res = fun("abcd") if (res /= ' 4') stop 1 end Gives the expected result. Paul
(In reply to Paul Thomas from comment #4) > Created attachment 56484 [details] > Fix for this PR > > Somehow this missed being a blocker for the ASSOCIATE meta-bug. > > The patch is so unbelievably simple that I am going to think about it for 24 > hours, even though it regtests just fine :-) And it is so simple that you seem to have forgotten about it... Are you going to pursue it?
Hi Harald, I had forgotten about this PR because the fix became incorporated in the patch for PR89645. In consequence, pr87448.f90 disappeared from the pr87477 failures :-) One of the last tasks before submitting the fix-up patch for PR89645 was to remove this chunk because I couldn't understand why it was there. Thanks for the reminder. Paul On Sun, 17 Dec 2023 at 17:49, anlauf at gcc dot gnu.org < gcc-bugzilla@gcc.gnu.org> wrote: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87448 > > --- Comment #5 from anlauf at gcc dot gnu.org --- > (In reply to Paul Thomas from comment #4) > > Created attachment 56484 [details] > > Fix for this PR > > > > Somehow this missed being a blocker for the ASSOCIATE meta-bug. > > > > The patch is so unbelievably simple that I am going to think about it > for 24 > > hours, even though it regtests just fine :-) > > And it is so simple that you seem to have forgotten about it... > > Are you going to pursue it? > > -- > You are receiving this mail because: > You are on the CC list for the bug.
This is fixed on 14-branch and mainline. Closing as fixed. Thanks for the report. Paul