Bug 97694 - ICE with optional assumed rank class(*) argument
Summary: ICE with optional assumed rank class(*) argument
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 10.1.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-11-03 11:36 UTC by martin
Modified: 2021-09-10 20:23 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-11-04 00:00:00


Attachments
Fortran source code (294 bytes, text/plain)
2020-11-03 11:36 UTC, martin
Details
Fix for this PR and PR97723 (1.28 KB, patch)
2020-12-11 13:40 UTC, Paul Thomas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description martin 2020-11-03 11:36:53 UTC
Created attachment 49494 [details]
Fortran source code

The attached code crashs with an ICE. It requires that the argument has type class(*) and is optional.

classstar_rank.f90:14:0:

   14 |          s = '0'
      | 
internal compiler error: Segmentation fault
0xbe205f crash_signal
        ../../gcc-repo/gcc/toplev.c:328
0x793007 trans_associate_var
        ../../gcc-repo/gcc/fortran/trans-stmt.c:1765
0x798fe1 gfc_trans_block_construct(gfc_code*)
        ../../gcc-repo/gcc/fortran/trans-stmt.c:2283
0x724950 trans_code
        ../../gcc-repo/gcc/fortran/trans.c:1960
0x79a89f gfc_trans_select_rank_cases
        ../../gcc-repo/gcc/fortran/trans-stmt.c:3703
0x79a89f gfc_trans_select_rank(gfc_code*)
        ../../gcc-repo/gcc/fortran/trans-stmt.c:3758
0x724aa3 trans_code
        ../../gcc-repo/gcc/fortran/trans.c:1984
0x798f70 gfc_trans_block_construct(gfc_code*)
        ../../gcc-repo/gcc/fortran/trans-stmt.c:2276
0x724950 trans_code
        ../../gcc-repo/gcc/fortran/trans.c:1960
0x791065 gfc_trans_if_1
        ../../gcc-repo/gcc/fortran/trans-stmt.c:1448
0x798a8b gfc_trans_if(gfc_code*)
        ../../gcc-repo/gcc/fortran/trans-stmt.c:1480
0x724b23 trans_code
        ../../gcc-repo/gcc/fortran/trans.c:1952
0x74dd44 gfc_generate_function_code(gfc_namespace*)
        ../../gcc-repo/gcc/fortran/trans-decl.c:6835
0x728571 gfc_generate_module_code(gfc_namespace*)
        ../../gcc-repo/gcc/fortran/trans.c:2264
0x6ca905 translate_all_program_units
        ../../gcc-repo/gcc/fortran/parse.c:6293
0x6ca905 gfc_parse_file()
        ../../gcc-repo/gcc/fortran/parse.c:6545
0x721c4f gfc_be_parse_file
        ../../gcc-repo/gcc/fortran/f95-lang.c:210
Comment 1 martin 2020-11-03 11:40:48 UTC
There are two resolved bug reports which are very similar, bug 61881 and bug 91926. At least bug 91926 is working fine. The other is pretty old and lacks the optional attribute.
Comment 2 Martin Liška 2020-11-04 09:37:35 UTC
Started to ICE with r10-3563-g73a28634098cb1ab.
Comment 3 Paul Thomas 2020-12-11 13:40:34 UTC
Created attachment 49743 [details]
Fix for this PR and PR97723

This regtests and fixes both PRs

Paul

select_rank_5.f90:

! { dg-do run }
!
! Test the fixes for PR97723 and PR97694.
!
! Contributed by Martin  <mscfd@gmx.net>
!
module mod
implicit none
private
public cssel

contains

function cssel(x) result(s)
   character(len=:), allocatable :: s
   class(*), dimension(..), optional, intent(in) :: x
   if (present(x)) then
      select rank (x)
      rank (0)
         s = '0' ! PR97723: ‘assign’ at (1) is not a function
                 ! PR97694: ICE in trans-stmt.c(trans_associate_var)
      rank (1)
         s = '1' ! PR97723: ‘assign’ at (1) is not a function
      rank default
         s = '?' ! PR97723: ‘assign’ at (1) is not a function
      end select
   else
      s = '-'
   end if
end function cssel

end module mod

program classstar_rank
use mod
implicit none

integer :: x
real, dimension(1:3) :: y
logical, dimension(1:2,1:2) :: z

if (any ([cssel(x),cssel(y),cssel(z),cssel()] .ne. ['0','1','?','-'])) stop 1

end program classstar_rank
Comment 4 Paul Thomas 2020-12-11 13:52:37 UTC
I guess that it's mine :-)

Paul
Comment 5 martin 2020-12-14 07:07:29 UTC
Thanks a lot. The patch seems to work great. Tried it with the more complex code from which the two failed tests were boiled down.
Comment 6 martin 2020-12-17 09:18:39 UTC
There is still a somewhat related problem if a variable of a derived type with allocatable component is provided to cssel. See bug 98342 with a simplified example code (without class(*)).
Comment 7 GCC Commits 2020-12-27 14:59:49 UTC
The master branch has been updated by Paul Thomas <pault@gcc.gnu.org>:

https://gcc.gnu.org/g:c4a678981572c12d158709ace0d3f23dd04cf217

commit r11-6346-gc4a678981572c12d158709ace0d3f23dd04cf217
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Sun Dec 27 14:59:38 2020 +0000

    Fortran: Fix some select rank issues [PR97694 and 97723].
    
    2020-12-27  Paul Thomas  <pault@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/97694
            PR fortran/97723
            * check.c (allocatable_check): Select rank temporaries are
            permitted even though they are treated as associate variables.
            * resolve.c (gfc_resolve_code): Break on select rank as well as
            select type so that the block os resolved.
            * trans-stmt.c (trans_associate_var): Class associate variables
            that are optional dummies must use the backend_decl.
    
    gcc/testsuite/
            PR fortran/97694
            PR fortran/97723
            * gfortran.dg/select_rank_5.f90: New test.
Comment 8 Tobias Burnus 2021-03-01 08:37:02 UTC
Paul – what's the state of this PR? I see a mainline/GCC 11 commit from end of December. Is there more to do?
Comment 9 GCC Commits 2021-09-10 20:16:51 UTC
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:25d45b5dd41a9ab005a5ae8ee8e54be17f2467a2

commit r10-10109-g25d45b5dd41a9ab005a5ae8ee8e54be17f2467a2
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Sun Dec 27 14:59:38 2020 +0000

    Fortran: Fix some select rank issues [PR97694 and 97723].
    
    2020-12-27  Paul Thomas  <pault@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/97694
            PR fortran/97723
            * check.c (allocatable_check): Select rank temporaries are
            permitted even though they are treated as associate variables.
            * resolve.c (gfc_resolve_code): Break on select rank as well as
            select type so that the block os resolved.
            * trans-stmt.c (trans_associate_var): Class associate variables
            that are optional dummies must use the backend_decl.
    
    gcc/testsuite/
            PR fortran/97694
            PR fortran/97723
            * gfortran.dg/select_rank_5.f90: New test.
    
    (cherry picked from commit c4a678981572c12d158709ace0d3f23dd04cf217)
Comment 10 anlauf 2021-09-10 20:23:15 UTC
Backported Paul's fix to 10-branch after verifying that it does the job.

The backport to 9-branch would require more work, thus not done.  Closing.