Hello Jerry, This does what is advertised on the package and passes regression testing.
OK for mainline. Thanks for the patch. Paul On Sat, 21 Nov 2026 at 18:06, Jerry DeLisle <[email protected]> wrote: > > See attached patch. A VALUE dummy must be associated with a copy of > the actual argument, but gfortran gave a derived type with allocatable > components a shallow copy, so a callee that writes to a component > changes the actual argument. A scalar CLASS dummy with VALUE received > the actual's own container, or, for a non-polymorphic or intrinsic > actual, the address of a container where the callee expected the > container itself. > > A derived type with allocatable components is now passed by reference > to a deep copy made by the caller, as is already done for arrays and > non-constant-length characters; the caller owns the copy, so it sees a > DEALLOCATE done by the callee. A scalar CLASS dummy gets a container > whose data is copied through the vtable, using conv_class_value_copy > from the PR128478 patch, which must go in first. Function results and > constructors are not copied, since they are already anonymous. > > Start with gfc_value_dummy_passed_byref in trans-types.cc, which > decides the new calling convention; gfc_sym_type and > gfc_maybe_dereference_var both use it and must agree. Then > conv_dummy_value in trans-expr.cc for the derived-type copy, with > value_actual_shares_storage deciding when a copy is needed. Then > conv_class_value_arg and its call in gfc_conv_procedure_call for the > CLASS case. The rest is the four new tests. > > Regression tested on x86_64. > > OK for mainline? > > Regards, > > Jerry > > --- > > fortran: [PR51284] Copy derived-type and CLASS actuals for VALUE dummies > > A VALUE dummy is associated with a copy of the actual argument (F2023 > 15.5.2.4). gfortran passed a derived type with allocatable components > by value, sharing its component data with the actual argument, and gave > a scalar CLASS dummy the actual's own container or, for a > non-polymorphic or intrinsic actual, the address of a container where > the callee expected the container. > > Pass a derived type with allocatable components by reference, like an > array or a CHARACTER of non-constant length, because the caller frees > the components of the copy and must see a DEALLOCATE done by the > callee. This changes the calling convention for such dummies, which > gfc_value_dummy_passed_byref decides for both gfc_sym_type and > gfc_maybe_dereference_var. Pass a scalar CLASS dummy a container whose > data is copied through the vtable. Copy only an actual argument that > can share storage with a variable; a function result or a constructor > is already an anonymous object, and the cleanup after the call frees > it. > > This patch depends on the PR128478 patch, which adds > conv_class_value_copy. > > Assisted-by: Claude Opus 5.5 > > PR fortran/51284 > > gcc/fortran/ChangeLog: > > * trans-expr.cc (gfc_maybe_dereference_var): Dereference a VALUE > dummy that gfc_value_dummy_passed_byref passes by reference. > (value_actual_shares_storage): New function. > (conv_class_value_arg): New function. > (conv_dummy_value): Pass a derived type with allocatable components > by reference to a copy of a variable, or to the temporary. Pass the > copy of a PDT by reference when the dummy is. Pass a null pointer > for an absent argument to a dummy passed by reference. > (gfc_conv_procedure_call): Use conv_class_value_arg for a scalar > CLASS dummy with VALUE, repackaging an element of a class array as a > class container. Copy an intrinsic actual argument for a CLASS(*) > dummy with VALUE. > * trans-types.cc (gfc_value_dummy_passed_byref): New function. > (gfc_sym_type): Use it. > * trans-types.h (gfc_value_dummy_passed_byref): Declare. > > gcc/testsuite/ChangeLog: > > * gfortran.dg/asan/value_dummy_copy_1.f90: New test. > * gfortran.dg/value_19.f90: New test. > * gfortran.dg/value_21.f90: New test. > * gfortran.dg/value_22.f90: New test.
