https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128857
Bug ID: 128857
Summary: Element length incorrectly set when passing a
polymorphic value as actual argument
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, c2
end type
type, extends(t1) :: t2
integer :: c3
end type
type(t2), target :: a(n)
integer :: i
a = [ (t2(1, 2, 3), i=1,n) ]
call sub(a)
contains
subroutine sub(x)
class(t1), target, intent(in) :: x(:)
if (.not. is_contiguous(x)) error stop 1
end subroutine
end program
This gives at runtime:
ERROR STOP 1
Error termination. Backtrace:
(...)
The element length is correctly set for the call to SUB:
parm.16.dtype = {.elem_len=12, .version=0, .rank=1, .type=5};
(...)
class.15._data.dtype = parm.16.dtype;
(...)
sub (&class.15);
but not for the call to IS_CONTIGUOUS:
parm.5.dtype = {.elem_len=8, .version=0, .rank=1, .type=5};
(...)
D.4851 = parm.5;
if ((logical(kind=4)) (D.4851.span != (integer(kind=8))
D.4851.dtype.elem_len || D.4851.dim[0].stride != 1))
{
_gfortran_error_stop_numeric (1, 0);
}
The value 8 is correct for a value of type T1, which is the declared type of X.
But X is polymorphic, with an dynamic type of T2, so the elem_len should be set
to 12.
The dtype is set in gfc_conv_expr_descriptor:
8719 /* Set the dtype. */
8720 if (se->unlimited_polymorphic)
8721 dtype = gfc_get_dtype (TREE_TYPE (desc), &loop.dimen);
8722 else if (expr->ts.type == BT_ASSUMED)
8723 {
8724 tree tmp2 = desc;
8725 if (DECL_LANG_SPECIFIC (tmp2) && GFC_DECL_SAVED_DESCRIPTOR
(tmp2))
8726 tmp2 = GFC_DECL_SAVED_DESCRIPTOR (tmp2);
8727 if (POINTER_TYPE_P (TREE_TYPE (tmp2)))
8728 tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
8729 dtype = gfc_conv_descriptor_dtype_get (tmp2);
8730 }
8731 else
8732 dtype = gfc_get_dtype (parmtype);
8733 gfc_conv_descriptor_dtype_set (&loop.pre, parm, dtype);
and the initialization never uses the dynamic type in the polymorphic case.