Re: [PATCH] PR125534 ALLOCATE of parameterized derived-type array, initializes only first element

Jerry D <[email protected]> Sun, 7 Jun 2026 12:57:58 -0700
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.fortran
Message-ID <[email protected]>
The master branch has been updated by Jerry DeLisle <[email protected]>:

https://gcc.gnu.org/g:81a0bec12189136350dbb43969743fab8b87a212

commit r17-1409-g81a0bec12189136350dbb43969743fab8b87a212
Author: Jerry DeLisle <[email protected]>
Date:   Fri Jun 5 13:19:58 2026 -0700

On 6/7/26 5:59 AM, Paul Richard Thomas wrote:
> Hello Jerry,
> 
> I added this PR to the PDT meta-bug.
> 
> The patch does what it says on the box and passes regression testing.
> WIth 'deallocate(a)' at the end of the test, there are no memory
> leaks.
> 
> OK for mainline and for backporting.
> 
> Thanks for the patch.
> 
> Paul
> 
> On Sat, 6 Jun 2026 at 02:29, Jerry D <[email protected]> wrote:
>>
>> See attached patch.
>>
>> Regression tested on x86_64.
>>
>> OK for mainline and then backport to 16?
>>
>> Regards,
>>
>> Jerry
>>
>> ---
>>
>> fortran: ALLOCATE of parameterized derived-type array
>>    initializes only first element
>>
>> gfc_trans_allocate passed expr->rank to gfc_allocate_pdt_comp and
>> gfc_deallocate_pdt_comp for array allocations of parameterized derived
>> types.  For an allocate-shape-spec expression (e.g. "allocate(a(n))"),
>> expr->rank is 0 even though the symbol is an array; the rank is carried on
>> the symbol rather than on the allocate expression.  With rank 0 the loop
>> that initialises each element's allocatable components ran zero times, so
>> only element zero was initialised.  Subsequent accesses to elements 1..N-1
>> used garbage pointers and crashed.
>>
>> Fix: when se.expr is a GFC array descriptor, take the rank from the
>> descriptor via GFC_TYPE_ARRAY_RANK instead of from expr->rank.  Apply the
>> same correction to the CLASS path and to the matching deallocate calls in
>> gfc_trans_deallocate.
>>
>> PR fortran/125534
>>
>> Assisted by: Claude Sonnet 4.6
>>
>> gcc/fortran/ChangeLog:
>>
>>          PR fortran/125534
>>          * trans-stmt.cc (gfc_trans_allocate): Use GFC_TYPE_ARRAY_RANK from
>>          the GFC descriptor type when se.expr is a descriptor, rather than
>>          expr->rank, for the rank passed to gfc_allocate_pdt_comp.  Apply
>>          the same fix to the CLASS path.
>>          (gfc_trans_deallocate): Likewise for gfc_deallocate_pdt_comp.
>>
>> gcc/testsuite/ChangeLog:
>>
>>          PR fortran/125534
>>          * gfortran.dg/pdt_array_alloc_1.f90: New test.
>> ---