Bug 86551 - [OOP] ICE on invalid code with select type
Summary: [OOP] ICE on invalid code with select type
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 8.1.1
: P3 normal
Target Milestone: ---
Assignee: janus
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2018-07-17 15:04 UTC by Daan van Vugt
Modified: 2021-10-26 20:28 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-07-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daan van Vugt 2018-07-17 15:04:59 UTC
The following program produces an internal compiler error for me.

module a
  type, abstract :: t1
  end type
  type, extends(t1) :: t2
  end type
contains
  subroutine b
    implicit none
    class(t1) :: c2

    select type (d => c2)
    end select type
  end subroutine b
end module a


Removing the select type statement gives the expected error:
a.f90:9:19:

     class(t1) :: c2
                   1
Error: CLASS variable ‘c2’ at (1) must be dummy, allocatable or pointer

Adding allocatable or pointer attributes fixes the ICE.
Comment 1 Martin Liška 2018-07-17 19:36:29 UTC
Confirmed, all releases I have (4.5.0) ICE.
Comment 2 janus 2018-09-15 21:16:40 UTC
Further reduced test case:

program a
  implicit none
  type :: t1
  end type
  class(t1) :: c2
  select type (d => c2)
  end select type
end
Comment 3 janus 2018-09-15 21:33:41 UTC
The ICE (and another one that is triggered by the same test case) is fixed with this patch:


diff --git a/gcc/fortran/match.c b/gcc/fortran/match.c
index 85247dd8334..6cf816be511 100644
--- a/gcc/fortran/match.c
+++ b/gcc/fortran/match.c
@@ -6009,7 +6009,7 @@ copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
   else
     assoc_sym->as = NULL;
 
-  if (selector->ts.type == BT_CLASS)
+  if (selector->ts.type == BT_CLASS && gfc_expr_attr (selector).class_ok)
     {
       /* The correct class container has to be available.  */
       assoc_sym->ts.type = BT_CLASS;
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index ea0ce800743..97f5402bf29 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -8862,7 +8862,8 @@ resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
       return;
     }
 
-  if (!code->expr1->symtree->n.sym->attr.class_ok)
+  if (!code->expr1->symtree->n.sym->attr.class_ok
+      || !gfc_expr_attr (code->expr2).class_ok)
     return;
 
   if (code->expr2)
Comment 4 janus 2018-09-16 11:35:35 UTC
The patch in comment #3 shows lots of regressions in the testsuite, but this one should be better:


diff --git a/gcc/fortran/match.c b/gcc/fortran/match.c
index 85247dd8334..6cf816be511 100644
--- a/gcc/fortran/match.c
+++ b/gcc/fortran/match.c
@@ -6009,7 +6009,7 @@ copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
   else
     assoc_sym->as = NULL;
 
-  if (selector->ts.type == BT_CLASS)
+  if (selector->ts.type == BT_CLASS && gfc_expr_attr (selector).class_ok)
     {
       /* The correct class container has to be available.  */
       assoc_sym->ts.type = BT_CLASS;
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index ea0ce800743..67b625f3a51 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -8867,6 +8867,9 @@ resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
 
   if (code->expr2)
     {
+      if (!gfc_expr_attr (code->expr2).class_ok)
+       return;
+
       if (code->expr1->symtree->n.sym->attr.untyped)
        code->expr1->symtree->n.sym->ts = code->expr2->ts;
       selector_type = CLASS_DATA (code->expr2)->ts.u.derived;


Unfortunately it still fails on:

FAIL: gfortran.dg/allocate_with_source_15.f03   -Os  (internal compiler error)
FAIL: gfortran.dg/select_type_26.f03   -O0  (internal compiler error)
FAIL: gfortran.dg/select_type_27.f03   -O0  (internal compiler error)
Comment 5 Dominique d'Humieres 2020-12-12 19:05:31 UTC
The ICE is gone for GCC10.2.1 and 11.0.
Comment 6 Thomas Koenig 2020-12-13 08:35:35 UTC
(In reply to Dominique d'Humieres from comment #5)
> The ICE is gone for GCC10.2.1 and 11.0.

Then we should commit the test case and close.
Comment 7 GCC Commits 2021-10-26 20:23:00 UTC
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:0ec53a3df536f83ec72ef25b045768c06c363f86

commit r12-4723-g0ec53a3df536f83ec72ef25b045768c06c363f86
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Oct 26 22:22:36 2021 +0200

    Fortran: error recovery on invalid code with SELECT TYPE
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/86551
            * gfortran.dg/pr86551.f90: New test to verify that PR86551 remains
            fixed.
Comment 8 anlauf 2021-10-26 20:28:57 UTC
Testcase committed.  Closing.

Thanks for the report!