Bug 58904 - ICE: accessing a component field of an unavailable type results in a seg fault
Summary: ICE: accessing a component field of an unavailable type results in a seg fault
Status: RESOLVED DUPLICATE of bug 78593
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.8.2
: P4 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2013-10-28 14:55 UTC by kimwooyoung
Modified: 2018-03-17 02:00 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-11-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description kimwooyoung 2013-10-28 14:55:06 UTC
The following code causes gfortran 4.8.2 to die with a seg fault.
(Ubuntu 12.10, intel64 (i.e., 64-bit x86) )

MODULE mymod
CONTAINS
  TYPE(mytype) FUNCTION create_sort_range(b) result(r)
    INTEGER b
    r%b = b
  END FUNCTION create_sort_range
END MODULE mymod

The compiler produces the correct error message if the statement 'r%b = b' is commented out.


$ gfortran -c  repro.F90
f951: internal compiler error: Segmentation fault
0x869cbf crash_signal
        ../../gcc-4.8.2/gcc/toplev.c:332
0x555765 gfc_match_varspec(gfc_expr*, int, bool, bool)
        ../../gcc-4.8.2/gcc/fortran/primary.c:1944
0x555d31 match_variable
        ../../gcc-4.8.2/gcc/fortran/primary.c:3304
0x537eb9 gfc_match(char const*, ...)
        ../../gcc-4.8.2/gcc/fortran/match.c:1115
0x53925c gfc_match_assignment()
        ../../gcc-4.8.2/gcc/fortran/match.c:1292
0x54cb69 match_word
        ../../gcc-4.8.2/gcc/fortran/parse.c:65
0x54db6b match_word
        ../../gcc-4.8.2/gcc/fortran/parse.c:327
0x54db6b decode_statement
        ../../gcc-4.8.2/gcc/fortran/parse.c:327
0x54f154 next_free
        ../../gcc-4.8.2/gcc/fortran/parse.c:783
0x54f154 next_statement
        ../../gcc-4.8.2/gcc/fortran/parse.c:976
0x54f8ed parse_spec
        ../../gcc-4.8.2/gcc/fortran/parse.c:2760
0x551738 parse_progunit
        ../../gcc-4.8.2/gcc/fortran/parse.c:4124
0x551ac0 parse_contained
        ../../gcc-4.8.2/gcc/fortran/parse.c:4063
0x552c7f parse_module
        ../../gcc-4.8.2/gcc/fortran/parse.c:4322
0x552c7f gfc_parse_file()
        ../../gcc-4.8.2/gcc/fortran/parse.c:4593
0x58e365 gfc_be_parse_file
        ../../gcc-4.8.2/gcc/fortran/f95-lang.c:189
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <http://gcc.gnu.org/bugs.html> for instructions.
Comment 1 janus 2013-11-01 14:19:21 UTC
This variant gives the correct error:

MODULE mymod
CONTAINS
  FUNCTION create_sort_range(b) result(r)
    TYPE(mytype) :: r
    INTEGER b
    r%b = b
  END FUNCTION create_sort_range
END MODULE mymod



c0.f90:4.16:

    TYPE(mytype) :: r
                1
Error: Derived type 'mytype' at (1) is being used before it is defined
c0.f90:6.6:

    r%b = b
      1
Error: Symbol 'r' at (1) has no IMPLICIT type



The segfault happens at least with 4.8 and trunk (haven't tried other versions).
Comment 2 Gerhard Steinmetz 2016-11-29 18:46:50 UTC
Update, gives an ICE from 7 down to at least 4.8 :


$ gfortran-7-20161127 -c pr58904.f90
f951: internal compiler error: Segmentation fault
0xc4532f crash_signal
        ../../gcc/toplev.c:333
0x6e97dc gfc_match_varspec(gfc_expr*, int, bool, bool)
        ../../gcc/fortran/primary.c:2053
0x6e9f04 match_variable
        ../../gcc/fortran/primary.c:3659
0x6bb401 gfc_match(char const*, ...)
        ../../gcc/fortran/match.c:1174
0x6bcb9c gfc_match_assignment()
        ../../gcc/fortran/match.c:1351
0x6db919 match_word_omp_simd
        ../../gcc/fortran/parse.c:93
0x6deccb match_word
        ../../gcc/fortran/parse.c:357
0x6deccb decode_statement
        ../../gcc/fortran/parse.c:362
0x6e0cd4 next_free
        ../../gcc/fortran/parse.c:1180
0x6e0cd4 next_statement
        ../../gcc/fortran/parse.c:1413
0x6e23e8 parse_spec
        ../../gcc/fortran/parse.c:3828
0x6e4a53 parse_progunit
        ../../gcc/fortran/parse.c:5615
0x6e4ee0 parse_contained
        ../../gcc/fortran/parse.c:5518
0x6e5bd9 parse_module
        ../../gcc/fortran/parse.c:5857
0x6e65a9 gfc_parse_file()
        ../../gcc/fortran/parse.c:6160
0x729ee2 gfc_be_parse_file
        ../../gcc/fortran/f95-lang.c:202
Comment 3 janus 2016-11-29 21:09:31 UTC
Draft patch:


Index: gcc/fortran/decl.c
===================================================================
--- gcc/fortran/decl.c	(revision 242960)
+++ gcc/fortran/decl.c	(working copy)
@@ -5962,6 +5962,19 @@ gfc_match_function_decl (void)
       return m;
     }
 
+  if (current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
+    {
+      sym = gfc_use_derived (current_ts.u.derived);
+
+      if (sym == NULL)
+	{
+	  m = MATCH_ERROR;
+	  goto cleanup;
+	}
+
+      current_ts.u.derived = sym;
+    }
+
   if (gfc_match ("function% %n", name) != MATCH_YES)
     {
       gfc_current_locus = old_loc;


This generates the right error, thus removing the ICE, but also produces several follow-up errors, because the function statement is rejected:


c0.f90:3:14:

   TYPE(mytype) FUNCTION create_sort_range(b) result(r)
              1
Error: Derived type ‘mytype’ at (1) is being used before it is defined
c0.f90:4:13:

     INTEGER b
             1
Error: Unexpected data declaration statement in CONTAINS section at (1)
c0.f90:5:6:

     r%b = b
      1
Error: Symbol ‘r’ at (1) has no IMPLICIT type
c0.f90:6:5:

   END FUNCTION create_sort_range
     1
Error: Expecting END MODULE statement at (1)
Comment 4 janus 2016-11-29 21:32:25 UTC
(In reply to janus from comment #3)
> Draft patch:

Unfortunately this causes several regressions in the testsuite:

FAIL: gfortran.dg/asynchronous_1.f90   -O   (test for errors, line 35)
FAIL: gfortran.dg/asynchronous_1.f90   -O   (test for errors, line 36)
FAIL: gfortran.dg/asynchronous_1.f90   -O   (test for errors, line 40)
FAIL: gfortran.dg/asynchronous_1.f90   -O   (test for errors, line 41)
FAIL: gfortran.dg/asynchronous_1.f90   -O  (test for excess errors)
FAIL: gfortran.dg/class_result_4.f90   -O   (test for errors, line 3)
FAIL: gfortran.dg/class_result_4.f90   -O   (test for errors, line 4)
FAIL: gfortran.dg/class_result_4.f90   -O  (test for excess errors)
FAIL: gfortran.dg/derived_external_function_1.f90   -O0  (test for excess errors)
FAIL: gfortran.dg/derived_external_function_1.f90   -O1  (test for excess errors)
FAIL: gfortran.dg/derived_external_function_1.f90   -O2  (test for excess errors)
FAIL: gfortran.dg/derived_external_function_1.f90   -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions  (test for excess errors)
FAIL: gfortran.dg/derived_external_function_1.f90   -O3 -g  (test for excess errors)
FAIL: gfortran.dg/derived_external_function_1.f90   -Os  (test for excess errors)
FAIL: gfortran.dg/derived_function_interface_1.f90   -O   (test for errors, line 41)
FAIL: gfortran.dg/derived_function_interface_1.f90   -O   (test for errors, line 42)
FAIL: gfortran.dg/derived_function_interface_1.f90   -O  (test for excess errors)
FAIL: gfortran.dg/func_result_5.f90   -O  (test for excess errors)
FAIL: gfortran.dg/function_types_1.f90   -O   (test for errors, line 9)
FAIL: gfortran.dg/function_types_1.f90   -O  (test for excess errors)
FAIL: gfortran.dg/function_types_2.f90   -O  (test for excess errors)
FAIL: gfortran.dg/function_types_3.f90   -O   (test for errors, line 8)
FAIL: gfortran.dg/function_types_3.f90   -O   (test for errors, line 17)
FAIL: gfortran.dg/function_types_3.f90   -O   (test for errors, line 18)
FAIL: gfortran.dg/function_types_3.f90   -O  (test for excess errors)
FAIL: gfortran.dg/func_derived_5.f90   -O  (test for excess errors)
FAIL: gfortran.dg/function_kinds_1.f90   -O0  (test for excess errors)
FAIL: gfortran.dg/function_kinds_1.f90   -O1  (test for excess errors)
FAIL: gfortran.dg/function_kinds_1.f90   -O2  (test for excess errors)
FAIL: gfortran.dg/function_kinds_1.f90   -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions  (test for excess errors)
FAIL: gfortran.dg/function_kinds_1.f90   -O3 -g  (test for excess errors)
FAIL: gfortran.dg/function_kinds_1.f90   -Os  (test for excess errors)
FAIL: gfortran.dg/function_kinds_2.f90   -O   (test for errors, line 16)
FAIL: gfortran.dg/function_kinds_2.f90   -O  (test for excess errors)
Comment 5 kargls 2018-03-16 22:50:33 UTC
The original testcase on FreeBSD gives

troutmask:sgk[209] gfcx -c a.f90
a.f90:4:4:

     r%b = b
    1
Error: Unclassifiable statement at (1)
a.f90:2:2:

   TYPE(mytype) FUNCTION create_sort_range(b) result(r)
  1
Error: The type for function 'create_sort_range' at (1) is not accessible
a.f90:6:3:

 END MODULE mymod
   1
Error: Expecting END PROGRAM statement at (1)
f951: Error: Unexpected end of file in 'a.f90'

without an ICE.
Comment 6 Dominique d'Humieres 2018-03-16 23:03:30 UTC
I get

pr58904.f90:5:4:

     r%b = b
    1
Error: Unclassifiable statement at (1)
pr58904.f90:3:2:

   TYPE(mytype) FUNCTION create_sort_range(b) result(r)
  1
Error: The type for function 'create_sort_range' at (1) is not accessible

with 6.4, 7.3, and trunk (8.0) and an ICE with 5.5.
Comment 7 Dominique d'Humieres 2018-03-16 23:14:54 UTC
The change occurred between revisions r242984 (2016-11-29, ICE) and r243219 (2016-12-03, errors), likely fixed by r243020 (pr78593) for gcc7 and r243485 for gcc6.
Comment 8 Steve Kargl 2018-03-16 23:49:31 UTC
On Fri, Mar 16, 2018 at 11:03:30PM +0000, dominiq at lps dot ens.fr wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58904
> 
> --- Comment #6 from Dominique d'Humieres <dominiq at lps dot ens.fr> ---
> I get
> 
> pr58904.f90:5:4:
> 
>      r%b = b
>     1
> Error: Unclassifiable statement at (1)
> pr58904.f90:3:2:
> 
>    TYPE(mytype) FUNCTION create_sort_range(b) result(r)
>   1
> Error: The type for function 'create_sort_range' at (1) is not accessible
> 
> with 6.4, 7.3, and trunk (8.0) and an ICE with 5.5.
> 

So, it can be closed?
Comment 9 Dominique d'Humieres 2018-03-17 02:00:19 UTC
>So, it can be closed?

Marked as duplicate of pr78593.

*** This bug has been marked as a duplicate of bug 78593 ***