Bug 104572 - ICE in gfc_resolve_finalizers, at fortran/resolve.cc:13646
Summary: ICE in gfc_resolve_finalizers, at fortran/resolve.cc:13646
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 12.0
: P4 normal
Target Milestone: 13.0
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2022-02-16 17:34 UTC by G. Steinmetz
Modified: 2023-03-22 19:11 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-02-16 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description G. Steinmetz 2022-02-16 17:34:47 UTC
Affects versions down to at least r5 :


$ cat z1.f90
module m
   type t
   contains
      final :: s
   end type
contains
   subroutine s(*)
   end
end


$ gfortran-12-20220213 -c z1.f90
f951: internal compiler error: Segmentation fault
0xcc8a9f crash_signal
        ../../gcc/toplev.cc:322
0x75abf3 gfc_resolve_finalizers
        ../../gcc/fortran/resolve.cc:13646
0x7726fd resolve_fl_derived
        ../../gcc/fortran/resolve.cc:15112
0x76c1f7 resolve_symbol
        ../../gcc/fortran/resolve.cc:15504
0x78b442 do_traverse_symtree
        ../../gcc/fortran/symbol.cc:4174
0x76f7a4 resolve_types
        ../../gcc/fortran/resolve.cc:17454
0x76ac3c gfc_resolve(gfc_namespace*)
        ../../gcc/fortran/resolve.cc:17569
0x752d72 gfc_parse_file()
        ../../gcc/fortran/parse.cc:6792
0x7a066f gfc_be_parse_file
        ../../gcc/fortran/f95-lang.cc:216
Comment 1 kargls 2022-02-16 19:49:44 UTC
Self explanatory.

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 835a4783718..1e7c3a07d0e 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -13643,6 +13653,13 @@ gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
 	}
       arg = dummy_args->sym;
 
+      if (!arg)
+	{
+	  gfc_error ("Argument of FINAL procedure at %L cannot be an "
+		     "alternate return", &list->proc_sym->declared_at);
+	  goto error;
+	}
+
       /* This argument must be of our type.  */
       if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
 	{
Comment 2 anlauf 2023-03-21 21:02:14 UTC
(In reply to kargl from comment #1)
> Self explanatory.
> 
> diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc

Yes.  Either this or:

@@ -13986,6 +13995,13 @@ gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
        }
       arg = dummy_args->sym;
 
+      if (!arg)
+       {
+         gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
+                    &list->proc_sym->declared_at, derived->name);
+         goto error;
+       }
+
       if (arg->as && arg->as->type == AS_ASSUMED_RANK
          && ((list != derived->f2k_derived->finalizers) || list->next))
        {
Comment 3 Steve Kargl 2023-03-21 21:10:12 UTC
On Tue, Mar 21, 2023 at 09:02:14PM +0000, anlauf at gcc dot gnu.org wrote:
> --- Comment #2 from anlauf at gcc dot gnu.org ---
> (In reply to kargl from comment #1)
> > Self explanatory.
> > 
> > diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
> 
> Yes.  Either this or:

I'm fine with gfortran emitting either error message.
As we've both put eyes on the bug, go ahead and use
your error message and commit.
Comment 4 GCC Commits 2023-03-22 18:25:56 UTC
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:3e791f45ded89626bc1f9f8013728f6e035801b2

commit r13-6812-g3e791f45ded89626bc1f9f8013728f6e035801b2
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Wed Mar 22 19:20:41 2023 +0100

    Fortran: improve checking of FINAL subroutine arguments [PR104572]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/104572
            * resolve.cc (gfc_resolve_finalizers): Argument of a FINAL subroutine
            cannot be an alternate return.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/104572
            * gfortran.dg/pr104572.f90: New test.
    
    Co-authored-by: Steven G. Kargl <kargl@gcc.gnu.org>
Comment 5 anlauf 2023-03-22 19:11:32 UTC
(In reply to Steve Kargl from comment #3)
> I'm fine with gfortran emitting either error message.
> As we've both put eyes on the bug, go ahead and use
> your error message and commit.

When using -std=f2018, the new testcase emits:

gcc/testsuite/gfortran.dg/pr104572.f90:12:17:

   12 |   subroutine s(*) ! { dg-error "Argument of FINAL procedure" }
      |                 1
Warning: Obsolescent feature: Alternate-return argument at (1)
gcc/testsuite/gfortran.dg/pr104572.f90:12:14:

   12 |   subroutine s(*) ! { dg-error "Argument of FINAL procedure" }
      |              1
Error: Argument of FINAL procedure at (1) must be of type 't'

The error message is also close to that emitted by other brands.

***

Closing.