Bug 106121 - ICE in gfc_simplify_extends_type_of, at fortran/simplify.cc:3109
Summary: ICE in gfc_simplify_extends_type_of, at fortran/simplify.cc:3109
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 13.0
: P4 normal
Target Milestone: 10.5
Assignee: anlauf
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2022-06-28 17:05 UTC by G. Steinmetz
Modified: 2022-07-03 20:30 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-06-28 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-06-28 17:05:12 UTC
Affects versions down to at least r5 :
(missing attribute allocatable or pointer)


$ cat z1.f90
program p
   type t
   end type
   type(t) :: x
   class(t) :: y
   print *, extends_type_of(x, y)
end


$ cat z2.f90
program p
   type t
   end type
   type(t) :: x
   class(t) :: y
   stop extends_type_of(x, y)
end


$ gfortran-13-20220626 -c z1.f90
z1.f90:5:16:

    5 |    class(t) :: y
      |                1
Error: CLASS variable 'y' at (1) must be dummy, allocatable or pointer
(null):0: confused by earlier errors, bailing out


$ gfortran-13-20220626 -c z2.f90
f951: internal compiler error: Segmentation fault
0xcd816f crash_signal
        ../../gcc/toplev.cc:322
0x78d799 gfc_simplify_extends_type_of(gfc_expr*, gfc_expr*)
        ../../gcc/fortran/simplify.cc:3109
0x70e0b6 do_simplify
        ../../gcc/fortran/intrinsic.cc:4670
0x718fca gfc_intrinsic_func_interface(gfc_expr*, int)
        ../../gcc/fortran/intrinsic.cc:5056
0x76db08 resolve_unknown_f
        ../../gcc/fortran/resolve.cc:2990
0x76db08 resolve_function
        ../../gcc/fortran/resolve.cc:3347
0x76db08 gfc_resolve_expr(gfc_expr*)
        ../../gcc/fortran/resolve.cc:7187
0x6fdfa4 gfc_reduce_init_expr(gfc_expr*)
        ../../gcc/fortran/expr.cc:3163
0x72b268 gfc_match_stopcode
        ../../gcc/fortran/match.cc:3157
0x751af1 match_word
        ../../gcc/fortran/parse.cc:67
0x75744d decode_statement
        ../../gcc/fortran/parse.cc:561
0x757a6a next_free
        ../../gcc/fortran/parse.cc:1397
0x757a6a next_statement
        ../../gcc/fortran/parse.cc:1629
0x758ffb parse_spec
        ../../gcc/fortran/parse.cc:4168
0x75c19c parse_progunit
        ../../gcc/fortran/parse.cc:6210
0x75d861 gfc_parse_file()
        ../../gcc/fortran/parse.cc:6755
0x7ab3ef gfc_be_parse_file
        ../../gcc/fortran/f95-lang.cc:229
Comment 1 G. Steinmetz 2022-06-28 17:05:51 UTC
$ cat z3.f90
program p
   type t
      integer :: a
   end type
   type(t) :: x
   class(t) :: y
   print *, extends_type_of(x, y)
end


$ cat z4.f90
program p
   type t
      integer :: a
   end type
   type(t) :: x
   class(t) :: y
   stop extends_type_of(x, y)
end


$ gfortran-13-20220626 -c z3.f90
z3.f90:6:16:

    6 |    class(t) :: y
      |                1
Error: CLASS variable 'y' at (1) must be dummy, allocatable or pointer


$ gfortran-13-20220626 -c z4.f90
f951: internal compiler error: gfc_compare_derived_types: invalid derived type
0x73a249 gfc_report_diagnostic
        ../../gcc/fortran/error.cc:883
0x73bdc7 gfc_internal_error(char const*, ...)
        ../../gcc/fortran/error.cc:1503
0x748458 gfc_compare_derived_types(gfc_symbol*, gfc_symbol*)
        ../../gcc/fortran/interface.cc:619
0x7e032e gfc_type_is_extension_of(gfc_symbol*, gfc_symbol*)
        ../../gcc/fortran/symbol.cc:5116
0x7cfd01 gfc_simplify_extends_type_of(gfc_expr*, gfc_expr*)
        ../../gcc/fortran/simplify.cc:3109
0x750296 do_simplify
        ../../gcc/fortran/intrinsic.cc:4670
0x75b25a gfc_intrinsic_func_interface(gfc_expr*, int)
        ../../gcc/fortran/intrinsic.cc:5056
0x7b0068 resolve_unknown_f
        ../../gcc/fortran/resolve.cc:2990
0x7b0068 resolve_function
        ../../gcc/fortran/resolve.cc:3347
0x7b0068 gfc_resolve_expr(gfc_expr*)
        ../../gcc/fortran/resolve.cc:7187
0x740184 gfc_reduce_init_expr(gfc_expr*)
        ../../gcc/fortran/expr.cc:3163
0x76d538 gfc_match_stopcode
        ../../gcc/fortran/match.cc:3157
0x794051 match_word
        ../../gcc/fortran/parse.cc:67
0x7999ad decode_statement
        ../../gcc/fortran/parse.cc:561
0x799fca next_free
        ../../gcc/fortran/parse.cc:1397
0x799fca next_statement
        ../../gcc/fortran/parse.cc:1629
0x79b55b parse_spec
        ../../gcc/fortran/parse.cc:4168
0x79e6fc parse_progunit
        ../../gcc/fortran/parse.cc:6210
0x79fdc1 gfc_parse_file()
        ../../gcc/fortran/parse.cc:6755
0x7ee53f gfc_be_parse_file
        ../../gcc/fortran/f95-lang.cc:229
Comment 2 kargls 2022-06-28 19:24:26 UTC
Infamous NULL pointer dereference.

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index c8f2ef9fbf4..1a33f26932a 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3084,6 +3084,8 @@ is_last_ref_vtab (gfc_expr *e)
 gfc_expr *
 gfc_simplify_extends_type_of (gfc_expr *a, gfc_expr *mold)
 {
+  gfc_component *ac, *mc;
+
   /* Avoid simplification of resolved symbols.  */
   if (is_last_ref_vtab (a) || is_last_ref_vtab (mold))
     return NULL;
@@ -3096,31 +3098,28 @@ gfc_simplify_extends_type_of (gfc_expr *a, gfc_expr *mold)
   if (UNLIMITED_POLY (a) || UNLIMITED_POLY (mold))
     return NULL;
 
+  ac = a->ts.u.derived->components;
+  if (a->ts.type == BT_CLASS && !ac)
+    return NULL;
+
+  mc = mold->ts.u.derived->components;
+  if (mold->ts.type == BT_CLASS && !mc)
+    return NULL;
+
   /* Return .false. if the dynamic type can never be an extension.  */
   if ((a->ts.type == BT_CLASS && mold->ts.type == BT_CLASS
-       && !gfc_type_is_extension_of
-			(mold->ts.u.derived->components->ts.u.derived,
-			 a->ts.u.derived->components->ts.u.derived)
-       && !gfc_type_is_extension_of
-			(a->ts.u.derived->components->ts.u.derived,
-			 mold->ts.u.derived->components->ts.u.derived))
+       && !gfc_type_is_extension_of (mc->ts.u.derived, ac->ts.u.derived)
+       && !gfc_type_is_extension_of (ac->ts.u.derived, mc->ts.u.derived))
       || (a->ts.type == BT_DERIVED && mold->ts.type == BT_CLASS
-	  && !gfc_type_is_extension_of
-			(mold->ts.u.derived->components->ts.u.derived,
-			 a->ts.u.derived))
+	  && !gfc_type_is_extension_of (mc->ts.u.derived, a->ts.u.derived))
       || (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED
-	  && !gfc_type_is_extension_of
-			(mold->ts.u.derived,
-			 a->ts.u.derived->components->ts.u.derived)
-	  && !gfc_type_is_extension_of
-			(a->ts.u.derived->components->ts.u.derived,
-			 mold->ts.u.derived)))
+	  && !gfc_type_is_extension_of (mold->ts.u.derived, ac->ts.u.derived)
+	  && !gfc_type_is_extension_of (ac->ts.u.derived, mold->ts.u.derived)))
     return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false);
 
   /* Return .true. if the dynamic type is guaranteed to be an extension.  */
   if (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED
-      && gfc_type_is_extension_of (mold->ts.u.derived,
-				   a->ts.u.derived->components->ts.u.derived))
+      && gfc_type_is_extension_of (mold->ts.u.derived, ac->ts.u.derived))
     return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, true);
 
   return NULL;
Comment 3 anlauf 2022-06-28 19:43:39 UTC
(In reply to kargl from comment #2)
> Infamous NULL pointer dereference.

Yes.

Shorter fix:

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index e8e3ec63669..b5112da441a 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3096,6 +3096,10 @@ gfc_simplify_extends_type_of (gfc_expr *a, gfc_expr *mold)
   if (UNLIMITED_POLY (a) || UNLIMITED_POLY (mold))
     return NULL;
 
+  if ((a->ts.type == BT_CLASS && !gfc_expr_attr (a).class_ok)
+      || (mold->ts.type == BT_CLASS && !gfc_expr_attr (mold).class_ok))
+    return NULL;
+
   /* Return .false. if the dynamic type can never be an extension.  */
   if ((a->ts.type == BT_CLASS && mold->ts.type == BT_CLASS
        && !gfc_type_is_extension_of

I think we need a good (better) set of macros to concisely handle problems
like the above to improve error recovery.
Comment 4 Steve Kargl 2022-06-28 20:02:25 UTC
On Tue, Jun 28, 2022 at 07:43:39PM +0000, anlauf at gcc dot gnu.org wrote:
> (In reply to kargl from comment #2)
> > Infamous NULL pointer dereference.
> 
> Yes.
> 
> Shorter fix:
> 
> diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
> index e8e3ec63669..b5112da441a 100644
> --- a/gcc/fortran/simplify.cc
> +++ b/gcc/fortran/simplify.cc
> @@ -3096,6 +3096,10 @@ gfc_simplify_extends_type_of (gfc_expr *a, gfc_expr
> *mold)
>    if (UNLIMITED_POLY (a) || UNLIMITED_POLY (mold))
>      return NULL;
> 
> +  if ((a->ts.type == BT_CLASS && !gfc_expr_attr (a).class_ok)
> +      || (mold->ts.type == BT_CLASS && !gfc_expr_attr (mold).class_ok))
> +    return NULL;
> +
>    /* Return .false. if the dynamic type can never be an extension.  */
>    if ((a->ts.type == BT_CLASS && mold->ts.type == BT_CLASS
>         && !gfc_type_is_extension_of
> 

Shorter is definitely better.  One thing that my patch
allowed was shortening the tortured if-statement that
is spread across a dozen of so lines.  In any event,
if you're patch survives regression testing, ok to
commit.
Comment 5 anlauf 2022-06-28 20:38:54 UTC
(In reply to Steve Kargl from comment #4)
> Shorter is definitely better.  One thing that my patch
> allowed was shortening the tortured if-statement that
> is spread across a dozen of so lines.  In any event,
> if you're patch survives regression testing, ok to
> commit.

It did regtest fine.  I nevertheless submitted here

https://gcc.gnu.org/pipermail/fortran/2022-June/057966.html

and will wait for 24h for comments.
Comment 6 GCC Commits 2022-06-29 17:13:37 UTC
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

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

commit r13-1349-gb8f284d3673004dffae714b56ed663467c2a52a7
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Jun 28 22:29:28 2022 +0200

    Fortran: improve error recovery for EXTENDS_TYPE_OF() [PR106121]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/106121
            * simplify.cc (gfc_simplify_extends_type_of): Do not attempt to
            simplify when one of the arguments is a CLASS variable that was
            not properly declared.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/106121
            * gfortran.dg/extends_type_of_4.f90: New test.
    
    Co-authored-by: Steven G. Kargl <kargl@gcc.gnu.org>
Comment 7 anlauf 2022-06-29 17:16:26 UTC
Fixed for gcc-13.

Thanks for the report!
Comment 8 GCC Commits 2022-07-03 19:41:34 UTC
The releases/gcc-12 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

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

commit r12-8543-gcf12a703cf7af34d91a6346883600caac2156e6e
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Jun 28 22:29:28 2022 +0200

    Fortran: improve error recovery for EXTENDS_TYPE_OF() [PR106121]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/106121
            * simplify.cc (gfc_simplify_extends_type_of): Do not attempt to
            simplify when one of the arguments is a CLASS variable that was
            not properly declared.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/106121
            * gfortran.dg/extends_type_of_4.f90: New test.
    
    Co-authored-by: Steven G. Kargl <kargl@gcc.gnu.org>
    (cherry picked from commit b8f284d3673004dffae714b56ed663467c2a52a7)
Comment 9 GCC Commits 2022-07-03 20:13:39 UTC
The releases/gcc-11 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:19f6e8ddfc447ac3b6198ab4b0176323e75a65cc

commit r11-10106-g19f6e8ddfc447ac3b6198ab4b0176323e75a65cc
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Jun 28 22:29:28 2022 +0200

    Fortran: improve error recovery for EXTENDS_TYPE_OF() [PR106121]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/106121
            * simplify.c (gfc_simplify_extends_type_of): Do not attempt to
            simplify when one of the arguments is a CLASS variable that was
            not properly declared.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/106121
            * gfortran.dg/extends_type_of_4.f90: New test.
    
    Co-authored-by: Steven G. Kargl <kargl@gcc.gnu.org>
    (cherry picked from commit b8f284d3673004dffae714b56ed663467c2a52a7)
Comment 10 GCC Commits 2022-07-03 20:29:44 UTC
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:8bb7567f5a3e67ab91614d4538eb3a14a5a76274

commit r10-10881-g8bb7567f5a3e67ab91614d4538eb3a14a5a76274
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Jun 28 22:29:28 2022 +0200

    Fortran: improve error recovery for EXTENDS_TYPE_OF() [PR106121]
    
    gcc/fortran/ChangeLog:
    
            PR fortran/106121
            * simplify.c (gfc_simplify_extends_type_of): Do not attempt to
            simplify when one of the arguments is a CLASS variable that was
            not properly declared.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/106121
            * gfortran.dg/extends_type_of_4.f90: New test.
    
    Co-authored-by: Steven G. Kargl <kargl@gcc.gnu.org>
    (cherry picked from commit b8f284d3673004dffae714b56ed663467c2a52a7)