https://gcc.gnu.org/g:04d78e410bb86a4f14924a4b1829d7898128b6d6
commit r17-6575-g04d78e410bb86a4f14924a4b1829d7898128b6d6 Author: Jerry DeLisle <[email protected]> Date: Fri Nov 6 19:05:40 2026 -0800 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. Diff: --- gcc/fortran/trans-expr.cc | 103 ++++++++++++++++- gcc/fortran/trans-types.cc | 33 +++++- gcc/fortran/trans-types.h | 1 + .../gfortran.dg/asan/value_dummy_copy_1.f90 | 116 +++++++++++++++++++ gcc/testsuite/gfortran.dg/value_19.f90 | 127 +++++++++++++++++++++ gcc/testsuite/gfortran.dg/value_21.f90 | 80 +++++++++++++ gcc/testsuite/gfortran.dg/value_22.f90 | 28 +++++ 7 files changed, 480 insertions(+), 8 deletions(-) diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index 766956d9b8cb..a8c8943801d1 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -3081,7 +3081,7 @@ gfc_maybe_dereference_var (gfc_symbol *sym, tree var, bool descriptor_only_p, || sym->attr.result)) var = build_fold_indirect_ref_loc (input_location, var); } - else if (!sym->attr.value) + else if (!sym->attr.value || gfc_value_dummy_passed_byref (sym)) { /* Dereference temporaries for class array dummy arguments. */ if (sym->attr.dummy && is_classarray @@ -6818,6 +6818,32 @@ conv_seq_assoc_value_arg (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym, } +/* Whether actual argument E can share storage with a variable, so that a + VALUE dummy needs a copy; any other value is freed after the call. */ + +static bool +value_actual_shares_storage (gfc_expr *e) +{ + return (expr_may_alias_variables (e, true) + || (e->ts.type == BT_DERIVED && parenthesized_variable (e))); +} + + +/* Pass the class container in PARMSE->EXPR, or its address, to a scalar + CLASS dummy with VALUE: a copy if actual argument E can share storage + with a variable, otherwise the container itself. */ + +static void +conv_class_value_arg (gfc_se *parmse, gfc_expr *e) +{ + if (value_actual_shares_storage (e)) + conv_class_value_copy (parmse); + else if (POINTER_TYPE_P (TREE_TYPE (parmse->expr))) + parmse->expr = build_fold_indirect_ref_loc (input_location, + parmse->expr); +} + + /* Helper function for the handling of (currently) scalar dummy variables with the VALUE attribute. Argument parmse should already be set up. */ static void @@ -6836,7 +6862,8 @@ conv_dummy_value (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym, gfc_copy_alloc_comp (e->ts.u.derived, parmse->expr, tmp, e->rank, 0)); - parmse->expr = tmp; + parmse->expr = gfc_value_dummy_passed_byref (fsym) + ? gfc_build_addr_expr (NULL_TREE, tmp) : tmp; tmp = gfc_deallocate_pdt_comp (e->ts.u.derived, tmp, e->rank); gfc_add_expr_to_block (&parmse->post, tmp); return; @@ -6854,6 +6881,8 @@ conv_dummy_value (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym, parmse->expr = null_pointer_node; parmse->string_length = build_int_cst (gfc_charlen_type_node, 0); } + else if (gfc_value_dummy_passed_byref (fsym)) + parmse->expr = null_pointer_node; else if (gfc_bt_struct (fsym->ts.type) && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING)) { @@ -6873,6 +6902,55 @@ conv_dummy_value (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym, return; } + /* A derived type with allocatable components is passed by reference to + a copy of a variable, or to a temporary as it is. */ + if (fsym->ts.type == BT_DERIVED && gfc_value_dummy_passed_byref (fsym)) + { + bool shared = value_actual_shares_storage (e); + tree addr, copy, cond; + stmtblock_t block; + + if (GFC_CLASS_TYPE_P (TREE_TYPE (parmse->expr))) + { + /* Copy the declared type of a CLASS actual. */ + parmse->expr = build_fold_indirect_ref_loc (input_location, + gfc_class_data_get + (parmse->expr)); + shared = true; + } + else if (!shared) + parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre); + + addr = gfc_evaluate_now (gfc_build_addr_expr (NULL_TREE, parmse->expr), + &parmse->pre); + parmse->expr = addr; + if (shared) + { + /* An absent optional actual has nothing to copy or free. */ + cond = fsym->attr.optional + ? fold_build2_loc (input_location, NE_EXPR, logical_type_node, + addr, fold_convert (TREE_TYPE (addr), + null_pointer_node)) + : logical_true_node; + copy = gfc_create_var (TREE_TYPE (TREE_TYPE (addr)), "val_copy"); + tmp = build_fold_indirect_ref_loc (input_location, addr); + gfc_init_block (&block); + gfc_add_modify (&block, copy, tmp); + gfc_add_expr_to_block (&block, + gfc_copy_alloc_comp (fsym->ts.u.derived, tmp, + copy, 0, 0)); + tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, + cond, gfc_finish_block (&block), + build_empty_stmt (input_location)); + gfc_add_expr_to_block (&parmse->pre, tmp); + tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived, copy, 0); + tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, + cond, tmp, build_empty_stmt (input_location)); + gfc_add_expr_to_block (&parmse->post, tmp); + parmse->expr = gfc_build_addr_expr (NULL_TREE, copy); + } + } + /* Assumed-length or non-constant-length CHARACTER VALUE dummy: copy the actual argument and pass the copy. */ if (fsym->ts.type == BT_CHARACTER @@ -7522,6 +7600,9 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, CLASS_DATA (fsym)->attr.class_pointer || CLASS_DATA (fsym)->attr.allocatable, sym->name, &derived_array); + + if (fsym->attr.value && e->rank == 0) + conv_class_value_arg (&parmse, e); } else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS && e->ts.type != BT_PROCEDURE @@ -7533,7 +7614,8 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, gfc_find_vtab (&e->ts); gfc_init_se (&parmse, se); gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts); - + if (fsym->attr.value) + conv_class_value_copy (&parmse); } else if (se->ss && se->ss->info->useflags) { @@ -7691,6 +7773,21 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, if (parmse.expr == NULL) gfc_conv_expr (&parmse, e); } + else if (fsym->ts.type == BT_CLASS) + { + if (e->ts.type == BT_CLASS + && CLASS_DATA (e)->attr.dimension) + { + /* An element of a class array is not a class + container; repackage it as one. */ + gfc_conv_expr_reference (&parmse, e); + gfc_conv_class_to_class (&parmse, e, fsym->ts, false, + false, false, false); + } + else + gfc_conv_expr (&parmse, e); + conv_class_value_arg (&parmse, e); + } else { gfc_conv_expr (&parmse, e); diff --git a/gcc/fortran/trans-types.cc b/gcc/fortran/trans-types.cc index 83d32a392962..d7ccdfee314e 100644 --- a/gcc/fortran/trans-types.cc +++ b/gcc/fortran/trans-types.cc @@ -2470,6 +2470,33 @@ gfc_nonrestricted_type (tree t) } +/* Whether VALUE dummy SYM is passed by reference to a copy made by the + caller; gfc_maybe_dereference_var dereferences it accordingly. */ + +bool +gfc_value_dummy_passed_byref (gfc_symbol *sym) +{ + if (!sym->attr.value) + return false; + + if (sym->attr.dimension) + return true; + + /* The caller frees the components, so it must see a DEALLOCATE done by + the callee. */ + if (sym->ts.type == BT_DERIVED && sym->ts.u.derived + && sym->ts.u.derived->attr.alloc_comp) + return true; + + if (sym->ts.type == BT_CHARACTER + && (!sym->ts.u.cl || !sym->ts.u.cl->length + || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)) + return true; + + return false; +} + + /* Return the type for a symbol. Special handling is required for character types to get the correct level of indirection. For functions return the return type. @@ -2528,11 +2555,7 @@ gfc_sym_type (gfc_symbol * sym, bool is_bind_c) type = gfc_typenode_for_spec (&sym->ts, sym->attr.codimension); if (sym->attr.dummy && !sym->attr.function - && (!sym->attr.value - || sym->attr.dimension - || (sym->ts.type == BT_CHARACTER - && (!sym->ts.u.cl || !sym->ts.u.cl->length - || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT))) + && (!sym->attr.value || gfc_value_dummy_passed_byref (sym)) && !sym->pass_as_value) byref = 1; else diff --git a/gcc/fortran/trans-types.h b/gcc/fortran/trans-types.h index 0d75ecf72524..03f698ca1dfb 100644 --- a/gcc/fortran/trans-types.h +++ b/gcc/fortran/trans-types.h @@ -90,6 +90,7 @@ tree gfc_get_character_type_len (int, tree); tree gfc_get_character_type_len_for_eltype (tree, tree); tree gfc_sym_type (gfc_symbol *, bool is_bind_c_arg = false); +bool gfc_value_dummy_passed_byref (gfc_symbol *); tree gfc_get_cfi_type (int dimen, bool restricted); tree gfc_typenode_for_spec (gfc_typespec *, int c = 0); bool gfc_copy_dt_decls_ifequal (gfc_symbol *, gfc_symbol *, bool); diff --git a/gcc/testsuite/gfortran.dg/asan/value_dummy_copy_1.f90 b/gcc/testsuite/gfortran.dg/asan/value_dummy_copy_1.f90 new file mode 100644 index 000000000000..d7ae2ca16389 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/asan/value_dummy_copy_1.f90 @@ -0,0 +1,116 @@ +! { dg-do run } +! +! PR 51284 +! The copy of the actual argument for a VALUE dummy of derived type with +! allocatable components, or of CLASS type. An absent optional actual has +! no copy, a function result or constructor is evaluated and freed once, +! and a CLASS actual for a TYPE dummy and an intrinsic actual for a +! CLASS(*) dummy are copied. Checked under -fsanitize=address. + +module m + implicit none + type :: t + integer, allocatable :: v(:) + end type + integer :: ncall = 0 +contains + subroutine st (x) + type(t), value :: x + if (x%v(1) /= 1) stop 1 + x%v(1) = 99 + end subroutine + subroutine st_free (x) + type(t), value :: x + if (x%v(1) /= 1) stop 2 + deallocate (x%v) + end subroutine + subroutine st_opt (x) + type(t), value, optional :: x + if (present (x)) then + if (x%v(1) /= 1) stop 3 + x%v(1) = 99 + end if + end subroutine + subroutine pass_opt (y) + type(t), optional :: y + call st_opt (y) + end subroutine + subroutine pass_opt_value (y) + type(t), value, optional :: y + call st_opt (y) + end subroutine + subroutine sc (x) + class(t), value :: x + if (x%v(1) /= 1) stop 4 + x%v(1) = 99 + end subroutine + subroutine su (x) + class(*), value :: x + select type (x) + type is (integer) + if (x /= 1) stop 6 + x = 99 + type is (character(*)) + if (x /= 'abc') stop 7 + x = 'xyz' + class default + stop 8 + end select + end subroutine + function f () result (r) + type(t) :: r + ncall = ncall + 1 + r%v = [1] + end function + function g () result (r) + class(t), allocatable :: r + ncall = ncall + 1 + allocate (r, source = t ([1])) + end function + subroutine work () + type(t) :: a + type(t), allocatable :: b + class(t), allocatable :: c, ca(:) + integer :: i + character(3) :: ch + + a = t ([1]) + call pass_opt (a) + if (a%v(1) /= 1) stop 11 + call pass_opt () + call pass_opt_value (a) + if (a%v(1) /= 1) stop 18 + call pass_opt_value () + call st_opt (b) + + ncall = 0 + call st (f ()) + call st_free (f ()) + call sc (f ()) + call sc (g ()) + if (ncall /= 4) stop 12 + call st (t ([1])) + + allocate (c, source = t ([1])) + call st (c) + if (c%v(1) /= 1) stop 13 + allocate (ca(2), source = t ([1])) + call st (ca(2)) + if (ca(2)%v(1) /= 1) stop 14 + + i = 1 + call su (i) + if (i /= 1) stop 16 + call su (1) + ch = 'abc' + call su (ch) + if (ch /= 'abc') stop 17 + call su ('abc') + end subroutine +end module + +program p + use m + implicit none + call work () +end program diff --git a/gcc/testsuite/gfortran.dg/value_19.f90 b/gcc/testsuite/gfortran.dg/value_19.f90 new file mode 100644 index 000000000000..320c39daff7b --- /dev/null +++ b/gcc/testsuite/gfortran.dg/value_19.f90 @@ -0,0 +1,127 @@ +! { dg-do run } +! PR 51284 +! A scalar dummy with the VALUE attribute is associated with an anonymous +! copy of the actual argument (F2023, 15.5.2.4), so the callee must not be +! able to reach the actual argument's data. Three things went wrong: +! +! - a non-polymorphic derived type with allocatable components was copied +! shallowly, so the callee wrote through to the actual argument; +! - a CLASS dummy fed a non-polymorphic actual argument was declared to +! take the class container by value but was handed its address, which +! gave the callee a garbage _data pointer; +! - a CLASS dummy fed a polymorphic actual argument shared the actual +! argument's data outright. + +module m + implicit none + + type :: base + integer :: i = 0 + integer, allocatable :: a(:) + end type base + + type, extends(base) :: child + character(:), allocatable :: c + end type child + +contains + + subroutine take_derived (x) + type(base), value :: x + if (.not. allocated (x%a)) stop 1 + if (any (x%a /= [1, 2, 3])) stop 2 + x%i = -1 + x%a = -1 + deallocate (x%a) + if (allocated (x%a)) stop 3 + end subroutine take_derived + + subroutine take_class (x) + class(base), value :: x + if (.not. allocated (x%a)) stop 4 + if (any (x%a /= [1, 2, 3])) stop 5 + x%i = -1 + x%a = -1 + select type (x) + type is (child) + if (x%c /= 'orig') stop 6 + x%c = 'CLOBBERED, and longer than the original' + end select + end subroutine take_class + +end module m + +program test + use m + implicit none + + type(base) :: b + type(child) :: c + class(base), allocatable :: p + + ! Non-polymorphic dummy, non-polymorphic actual. + b%i = 1 + b%a = [1, 2, 3] + call take_derived (b) + call check_base (b, 7) + + ! Polymorphic dummy, non-polymorphic actual of the declared type. + call take_class (b) + call check_base (b, 8) + + ! Polymorphic dummy, non-polymorphic actual of an extended type. + c%i = 1 + c%a = [1, 2, 3] + c%c = 'orig' + call take_class (c) + call check_child (c, 9) + + ! Polymorphic dummy, polymorphic actual of the declared type. + allocate (p, source = b) + call take_class (p) + call check_class (p, 10) + deallocate (p) + + ! Polymorphic dummy, polymorphic actual of an extended type. + allocate (p, source = c) + call take_class (p) + call check_class (p, 11) + select type (p) + type is (child) + if (p%c /= 'orig') stop 12 + class default + stop 13 + end select + deallocate (p) + +contains + + subroutine check_base (x, code) + type(base), intent(in) :: x + integer, intent(in) :: code + if (x%i /= 1) stop 20 + if (.not. allocated (x%a)) stop 21 + if (any (x%a /= [1, 2, 3])) stop 22 + if (code < 0) stop 23 + end subroutine check_base + + subroutine check_child (x, code) + type(child), intent(in) :: x + integer, intent(in) :: code + if (x%i /= 1) stop 24 + if (.not. allocated (x%a)) stop 25 + if (any (x%a /= [1, 2, 3])) stop 26 + if (x%c /= 'orig') stop 27 + if (code < 0) stop 28 + end subroutine check_child + + subroutine check_class (x, code) + class(base), intent(in) :: x + integer, intent(in) :: code + if (x%i /= 1) stop 29 + if (.not. allocated (x%a)) stop 30 + if (any (x%a /= [1, 2, 3])) stop 31 + if (code < 0) stop 32 + end subroutine check_class + +end program test diff --git a/gcc/testsuite/gfortran.dg/value_21.f90 b/gcc/testsuite/gfortran.dg/value_21.f90 new file mode 100644 index 000000000000..b3542ffc1090 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/value_21.f90 @@ -0,0 +1,80 @@ +! { dg-do run } +! PR 51284 +! An element of a polymorphic array passed to a scalar CLASS dummy with the +! VALUE attribute is not a class container, so it has to be repackaged as +! one before the copy is made. + +program prog + implicit none + type :: t1 + integer :: c1, c2 + end type + type, extends(t1) :: t2 + integer :: c3 + end type + type(t2) :: x(5) + class(t1), allocatable :: y(:) + integer :: i + x = [ (t2(i*i*i, i*i, i), i=1,5) ] + y = x + call s(x(3), 1) + if (x(3)%c1 /= 27) error stop 141 + if (x(3)%c2 /= 9) error stop 142 + if (x(3)%c3 /= 3) error stop 143 + call s(y(3), 2) + if (y(3)%c1 /= 27) error stop 241 + if (y(3)%c2 /= 9) error stop 242 + select type(y) + type is (t2) + if (y(3)%c3 /= 3) error stop 243 + class default + error stop 249 + end select +contains + subroutine s(a, e) + class(t1), value :: a + integer, intent(in) :: e + if (a%c1 /= 27) error stop e * 100 + 1 + if (a%c2 /= 9) error stop e * 100 + 2 + select type (a) + type is (t2) + if (a%c3 /= 3) error stop e * 100 + 3 + class default + error stop 9 + end select + call ar(a, e) + if (a%c1 /= 12) error stop e * 100 + 11 + if (a%c2 /= 8) error stop e * 100 + 12 + select type (a) + type is (t2) + if (a%c3 /= 4) error stop e * 100 + 13 + class default + error stop e * 100 + 19 + end select + end subroutine + subroutine ar(a, e) + class(t1) :: a(..) + integer, intent(in) :: e + select rank (a) + rank(0) + call check(a, e) + rank default + error stop e * 100 + 21 + end select + end subroutine + subroutine check(a, e) + class(t1) :: a + integer, intent(in) :: e + if (a%c1 /= 27) error stop e * 100 + 31 + a%c1 = 12 + if (a%c2 /= 9) error stop e * 100 + 32 + a%c2 = 8 + select type (a) + type is (t2) + if (a%c3 /= 3) error stop e * 100 + 33 + a%c3 = 4 + class default + error stop e * 100 + 39 + end select + end subroutine +end program diff --git a/gcc/testsuite/gfortran.dg/value_22.f90 b/gcc/testsuite/gfortran.dg/value_22.f90 new file mode 100644 index 000000000000..c97770a5d21f --- /dev/null +++ b/gcc/testsuite/gfortran.dg/value_22.f90 @@ -0,0 +1,28 @@ +! { dg-do run } +! +! PR 51284 +! A VALUE dummy of a PDT with an allocatable component is passed by +! reference to a copy, like that of any type with allocatable components. + +module m + implicit none + type :: pt(k) + integer, kind :: k + integer(k), allocatable :: v(:) + end type +contains + subroutine sp (x) + type(pt(4)), value :: x + if (any (x%v /= [1, 2])) stop 1 + x%v = 99 + end subroutine +end module + +program p + use m + implicit none + type(pt(4)) :: z + z%v = [1, 2] + call sp (z) + if (any (z%v /= [1, 2])) stop 2 +end program
