https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58904
--- Comment #3 from janus at gcc dot gnu.org --- 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)