https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128858
Bug ID: 128858
Summary: Passing a derived type array subreference as actual
argument to a polymorphic dummy passes the wrong
values
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: mikael at gcc dot gnu.org
Target Milestone: ---
This is based on the first comment of the sashiko review at [1].
Testcase:
program prog
implicit none
integer, parameter :: n = 5
type :: t
integer :: c
end type
type :: u
type(t) :: c1, c2, c3
end type
type(u), allocatable :: a(:)
type(u) :: b(n)
integer :: i
a = [ (u(t(3*i*i+2), t(2*i+1), t(i*i)), i=1,n) ]
call check_all(a%c1, [5, 14, 29, 50, 77], 1)
call check_all(a%c2, [3, 5, 7, 9, 11], 2)
call check_all(a%c3, [1, 4, 9, 16, 25], 3)
b = [ (u(t(5*i+3), t(2*i*i+1), t(i)), i=1,n) ]
call check_all(b%c1, [8, 13, 18, 23, 28], 4)
call check_all(b%c2, [3, 9, 19, 33, 51], 5)
call check_all(b%c3, [1, 2, 3, 4, 5], 6)
contains
subroutine check_all(x, y, e)
class(t), target, intent(in) :: x(:)
integer, intent(in) :: y(:), e
integer :: i
call check_integer(size(x, dim=1), size(y, dim=1), e*10+7)
do i=1,n
call check_integer(x(i)%c, y(i), e*10+i)
end do
!if (any(x%c /= y)) error stop e*10+8
call check_logical(is_contiguous(x), .false., e*10+9)
end subroutine
subroutine check_integer(x, y, e)
integer, value :: x, y, e
if (x /= y) then
print *, e, "FAIL", x, y
!error stop e
end if
end subroutine
subroutine check_logical(x, y, e)
logical, value :: x, y
integer, value :: e
if (x .neqv. y) then
print *, e, "FAIL", x, y
!error stop e
end if
end subroutine
end program
When executed, the program outputs:
12 FAIL 3 14
13 FAIL 1 29
14 FAIL 14 50
15 FAIL 5 77
21 FAIL 5 3
22 FAIL 3 5
23 FAIL 1 7
24 FAIL 14 9
25 FAIL 5 11
31 FAIL 5 1
32 FAIL 3 4
33 FAIL 1 9
34 FAIL 14 16
35 FAIL 5 25
42 FAIL 3 13
43 FAIL 1 18
44 FAIL 13 23
45 FAIL 9 28
51 FAIL 8 3
52 FAIL 3 9
53 FAIL 1 19
54 FAIL 13 33
55 FAIL 9 51
61 FAIL 8 1
62 FAIL 3 2
63 FAIL 1 3
64 FAIL 13 4
65 FAIL 9 5
Here is the sashiko analysis from [1]:
> Enabling gfc_dummy_requires_direct_arg in gcc/fortran/symbol.cc for BT_CLASS
> dummies exposes a wrong-code bug when passing a derived-type subreference
> array (such as a%comp) to a polymorphic target dummy argument.
> In gfc_conv_procedure_call (gcc/fortran/trans-expr.cc), when the formal
> argument fsym has BT_CLASS and the actual argument e has BT_DERIVED,
> translation goes through gfc_conv_derived_to_class instead of the non-class
> path. On the non-class path, when copy-in/copy-out is declined because
> gfc_dummy_requires_direct_arg (fsym) is true, parmse.force_no_tmp = 1 is set
> when e->expr_type == EXPR_VARIABLE && is_subref_array (e) before calling
> gfc_conv_array_parameter. In gfc_conv_derived_to_class, however,
> parmse->force_no_tmp is never set before calling gfc_conv_array_parameter.
> As a result, in gfc_conv_expr_descriptor (gcc/fortran/trans-array.cc),
> subref_array_target = (is_subref_array (expr) && (se->direct_byref ||
> se->force_no_tmp || expr->ts.type == BT_CHARACTER)) evaluates to false,
> causing three silent wrong-code issues:
> 1. gfc_get_dataptr_offset is called with subref = false, so the if (subref)
> loop that walks trailing REF_COMPONENT and REF_ARRAY (AR_ELEMENT)
> references is skipped. The descriptor data pointer is left pointing to
> byte offset 0 of the outer element instead of the component offset. (In
> target_dummy_span_4.f90, call class_wrapper(a%t1, 4) only succeeds
> because parent component t1 is at byte offset 0; any subcomponent at a
> non-zero offset reads or writes the wrong memory.)
> 2. If the base array is allocatable, a pointer, or an assumed-shape dummy
> (!GFC_ARRAY_TYPE_P (TREE_TYPE (desc))), full remains 1 because
> subref_array_target is false, so gfc_conv_expr_descriptor reuses the
> outer array's descriptor directly without creating a subobject descriptor.
> 3. parmtype is set to the outer array element type instead of the subobject
> type, setting dtype.elem_len to the outer element size rather than the
> component size. Subsequent span == elem_len checks in
> gfc_conv_is_contiguous_expr and gfc_conv_span_is_elem_len then falsely
> treat the non-contiguous subobject array as contiguous.
> Setting parmse->force_no_tmp = 1 in gfc_conv_derived_to_class when passing a
> subreference array to a dummy that requires a direct argument (and handling
> copy-in/copy-out when copy_in_out_allowed is true) would avoid this.
The bullet 3. doesn't show any visible FAIL with the test, but it is because of
PR128857. With PR128857 fixed, additional FAILs should appear.
Despite the AI comment announcing the patch it's replying to as causing the
failures, the test pasted above doesn't work better with gfortran-16.
[1]:
https://gcc.sashiko.dev/#/patchset/20261026104353.58786-1-morin-mikael%40orange.fr