Affects versions down to at least r5 (type t not defined) : $ cat z1.f90 class(t) function f() allocatable :: f end $ cat z2.f90 class(t) function f() pointer :: f end $ gfortran-12-20210905 -c z1.f90 f951: internal compiler error: Segmentation fault 0xeace0f crash_signal ../../gcc/toplev.c:328 0x7882b4 attr_decl1 ../../gcc/fortran/decl.c:8691 0x7882b4 attr_decl ../../gcc/fortran/decl.c:8777 0x7f7db1 match_word ../../gcc/fortran/parse.c:65 0x7fd189 decode_statement ../../gcc/fortran/parse.c:441 0x7fda8a next_free ../../gcc/fortran/parse.c:1384 0x7fda8a next_statement ../../gcc/fortran/parse.c:1616 0x7ff2ad parse_spec ../../gcc/fortran/parse.c:4164 0x801f9c parse_progunit ../../gcc/fortran/parse.c:6114 0x803a37 gfc_parse_file() ../../gcc/fortran/parse.c:6669 0x8510ff gfc_be_parse_file ../../gcc/fortran/f95-lang.c:216
Detected with "type" instead of "class" : $ cat za1.f90 type(t) function f() allocatable :: f end $ cat za2.f90 type(t) function f() pointer :: f end $ gfortran-12-20210905 -c za1.f90 za1.f90:1:0: 1 | type(t) function f() | Error: The type for function 'f' at (1) is not accessible
The following patch fixes the problem. It has not been regression tested. diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c index 05081c40f1e..85c95cea17f 100644 --- a/gcc/fortran/decl.c +++ b/gcc/fortran/decl.c @@ -8695,7 +8695,9 @@ attr_decl1 (void) /* Update symbol table. DIMENSION attribute is set in gfc_set_array_spec(). For CLASS variables, this must be applied to the first component, or '_data' field. */ - if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class) + if (sym->ts.type == BT_CLASS + && sym->ts.u.derived + && sym->ts.u.derived->attr.is_class) { /* gfc_set_array_spec sets sym->attr not CLASS_DATA(sym)->attr. Check for duplicate attribute here. */ diff --git a/gcc/fortran/parse.c b/gcc/fortran/parse.c index 6d7845e8517..8d6d4f4e98f 100644 --- a/gcc/fortran/parse.c +++ b/gcc/fortran/parse.c @@ -3813,7 +3813,7 @@ match_deferred_characteristics (gfc_typespec * ts) m = gfc_match_prefix (ts); gfc_buffer_error (false); - if (ts->type == BT_DERIVED) + if (ts->type == BT_DERIVED || ts->type == BT_CLASS) { ts->kind = 0; @@ -4094,7 +4094,7 @@ declSt: if (bad_characteristic) { ts = &gfc_current_block ()->result->ts; - if (ts->type != BT_DERIVED) + if (ts->type != BT_DERIVED && ts->type != BT_CLASS) gfc_error ("Bad kind expression for function %qs at %L", gfc_current_block ()->name, &gfc_current_block ()->declared_at);
(In reply to kargl from comment #2) > The following patch fixes the problem. It has not been regression tested. This restores the error, but for CLASS I now get: pr102331.f90:2:3: 2 | allocatable :: f | 1 Error: Unclassifiable statement at (1) pr102331.f90:1:0: 1 | class(t) function f() | Error: The type for function 'f' at (1) is not accessible while the error message for line 2 does not show up for TYPE. Maybe there are a few more places that need fixing. (Not only the obvious one after the second hunk for parse.c).
On Wed, Sep 15, 2021 at 08:57:45PM +0000, anlauf at gcc dot gnu.org wrote: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102331 > > --- Comment #3 from anlauf at gcc dot gnu.org --- > (In reply to kargl from comment #2) > > The following patch fixes the problem. It has not been regression tested. > > This restores the error, but for CLASS I now get: > > pr102331.f90:2:3: > > 2 | allocatable :: f > | 1 > Error: Unclassifiable statement at (1) > pr102331.f90:1:0: > > 1 | class(t) function f() > | > Error: The type for function 'f' at (1) is not accessible > > while the error message for line 2 does not show up for TYPE. > Maybe there are a few more places that need fixing. > (Not only the obvious one after the second hunk for parse.c). > Yes, likely, 'ts.type == BT_CLASS' needs to be sprinkled in other places. Unfortunately, the 'Unclassifiable statement' error is a last resort catch-all error. I know little about the CLASS implementation. I suspect BT_CLASS takes a much different path through the compiler.
After exploring a while and looking for other places I have concluded that for invalid code Steve's patch is adequate. I will regression test it next.
Regression testing looks good. The patch wiggles on the error messages given for: pr85779.f90 class_result_4.f90 In both cases they are reasonable. I don't think we need any new test cases since we are obviously exercising the patch. If anyone insists I add the z1 and z2 cases to the testsuite, let me know and i will do so.
I biffed the PR number on this commit. It should have been here. I will have to look into amending the ChangeLog correctly. The master branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>: https://gcc.gnu.org/g:cdc6bf44eec295805ae29a8aaddafd111de01c8e commit r13-4934-gcdc6bf44eec295805ae29a8aaddafd111de01c8e Author: Steve Kargl <kargl@gcc.gnu.org> Date: Mon Dec 26 14:07:04 2022 -0800 Modify checks to avoid referencing NULL pointer. Update test cases with error messages that changed as a result. gcc/fortran/ChangeLog: PR fortran/102595 * decl.cc (attr_decl1): Guard against NULL pointer. * parse.cc (match_deferred_characteristics): Include BT_CLASS in check for derived being undefined. gcc/testsuite/ChangeLog: PR fortran/102595 * gfortran.dg/class_result_4.f90: Update error message check. * gfortran.dg/pr85779_3.f90: Update error message check.
Fixed ChangeLogs PR number referenced. commit 04d7cc165387d19e1433a4b2157d2bde7b95305b (HEAD -> master, origin/master, origin/HEAD) Author: Jerry DeLisle <jvdelisle@gcc.gnu.org> Date: Tue Jan 17 17:30:49 2023 -0800 Fix bug number reference in Changelogs For: gcc/fortran/ChangeLog-2022: gcc/testsuite/ChangeLog-2022:
The releases/gcc-12 branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>: https://gcc.gnu.org/g:82ec75a726b3f8f874dacb0cb342c9bbd1233cc0 commit r12-9320-g82ec75a726b3f8f874dacb0cb342c9bbd1233cc0 Author: Jerry DeLisle <jvdelisle@gcc.gnu.org> Date: Sun Mar 26 18:44:35 2023 -0700 Fortran: Modify checks to avoid referencing NULL pointer. Backport from mainline. gcc/fortran/ChangeLog: PR fortran/102331 * decl.cc (attr_decl1): Guard against NULL pointer. * parse.cc (match_deferred_characteristics): Include BT_CLASS in check for derived being undefined. gcc/testsuite/ChangeLog: PR fortran/102331 * gfortran.dg/class_result_4.f90: Update error message check. * gfortran.dg/pr85779_3.f90: Update error message check.
fixed on 12, 13, and master.