https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128079
Bug ID: 128079
Summary: Structure constructor of extended type loses dynamic
type of inherited CLASS allocatable component
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: ivan.pribec at gmail dot com
Target Milestone: ---
With gfortran 16.2 the program below (mwe.f90) segfaults:
program p
implicit none
type :: holder
class(*), allocatable :: inner
end type
type, extends(holder) :: child
end type
type(child) :: c
c = child(inner=42)
select type (x => c%inner)
type is (integer)
if (x /= 42) error stop "wrong value"
class default
error stop "c%inner lost its dynamic type"
end select
end program
$ gfortran -Og mwe.f90 && ./a.out
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x102fef323
#1 0x102fee063
#2 0x183189743
#3 0x102dc47b7
#4 0x102dc47b7
Segmentation fault: 11
It also segfaults on 15.2 and 14.3. If I add flags such as -O0, -O1, -O2, it
might also print:
ERROR STOP c%inner lost its dynamic type
With version 13.4 however, the compiler issues the error:
mwe.f90:9:18:
9 | c = child(inner=42)
| 1
Error: Cannot convert INTEGER(4) to CLASS(*) at (1)
Possibly related to PR 77412 and PR 68225.