https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128103
Bug ID: 128103
Summary: Wrong array descriptor with class container element
type
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: mikael at gcc dot gnu.org
Target Milestone: ---
Testcase:
program prog
implicit none
integer, parameter :: n = 5
type :: t1
integer :: c1
end type
type(t1), target :: a(n)
integer :: i
a = [ (t1(i*i+2), i=1,n) ]
call sub(a)
contains
subroutine sub(x)
class(t1), pointer, intent(in) :: x(:)
type(t1), pointer :: p(:)
p => a
if (.not. associated(p, x(:))) error stop 1
end subroutine
end program
This example fails at runtime:
ERROR STOP 1
Error termination. Backtrace:
(...)
The problem is in the generated code for the associated(...) check:
struct array01___class_prog_T1_1_0p parm.0;
(...)
parm.0.dtype = {.elem_len=72, .version=0, .rank=1, .type=7};
(...)
if (_gfortran_associated (&p, &parm.0) == 0 || p.dim[0].stride == 0)
The array descriptor generated for the x(:) array has the type of an array of
class containers, and the element length that is set for it isn't the length of
the t1 type.
The array descriptor is generated by gfc_conv_expr_descriptor, and in that
function the element type is chosen by the following snippet:
8586 /* Otherwise make a new one. The element type is that of the
8587 subobject for a subreference of the array. */
8588 if (expr->ts.type == BT_CHARACTER
8589 || (subref_array_target && !se->direct_byref))
8590 parmtype = gfc_typenode_for_spec (&expr->ts);
8591 else
8592 parmtype = gfc_get_element_type (TREE_TYPE (desc));
For the testcase, it is the true branch that is taken; the expression has a
polymorphic type, so that the resulting element type obtained in `parmtype' is
`__class_prog_T1_1_0p', the type of the class container, instead of `t1'.