Bug 87448 - ICE at trans-expr:3417 in allocate statement with type signature using an associated variable
Summary: ICE at trans-expr:3417 in allocate statement with type signature using an ass...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 8.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-valid-code
Depends on:
Blocks: associate
  Show dependency treegraph
 
Reported: 2018-09-27 04:45 UTC by martin
Modified: 2024-07-20 06:55 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-09-27 00:00:00


Attachments
Fix for this PR (323 bytes, patch)
2023-11-01 18:08 UTC, Paul Thomas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description martin 2018-09-27 04:45:44 UTC
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.
Comment 1 janus 2018-09-27 07:57:36 UTC
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
Comment 2 martin 2018-09-27 08:14:41 UTC
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).
Comment 3 martin 2022-01-25 08:37:41 UTC
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
Comment 4 Paul Thomas 2023-11-01 18:08:00 UTC
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
Comment 5 anlauf 2023-12-17 17:49:05 UTC
(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?
Comment 6 paul.richard.thomas@gmail.com 2023-12-18 06:59:30 UTC
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.
Comment 7 Paul Thomas 2024-07-20 06:55:25 UTC
This is fixed on 14-branch and mainline. Closing as fixed.

Thanks for the report.

Paul