&& !INTEGER_CST_P (sym->ts.u.cl->backend_decl))
{
if (sym->ts.deferred && !sym->ts.u.cl->length && !sym->attr.dummy)
- gfc_add_modify (&init, sym->ts.u.cl->backend_decl,
- build_zero_cst (TREE_TYPE (sym->ts.u.cl->backend_decl)));
+ {
+ tree len_expr = sym->ts.u.cl->backend_decl;
+ tree init_val = build_zero_cst (TREE_TYPE (len_expr));
+ if (VAR_P (len_expr)
+ && sym->attr.save
+ && !DECL_INITIAL (len_expr))
+ DECL_INITIAL (len_expr) = init_val;
+ else
+ gfc_add_modify (&init, len_expr, init_val);
+ }
gfc_conv_string_length (sym->ts.u.cl, NULL, &init);
gfc_trans_vla_type_sizes (sym, &init);
--- /dev/null
+! { dg-do run }
+!
+! PR fortran/120713
+! Check that the length variable of SAVEd allocatable character arrays are
+! not initialized at function entry.
+
+program p
+ implicit none
+ call s(1)
+ call s(2)
+contains
+ subroutine s(i)
+ integer, intent(in) :: i
+ character(len=:), allocatable, save :: a(:)
+ integer :: j
+ if (i == 1) then
+ allocate(a, source= [ ('x' // achar(ichar('0') + j), j=1,7) ])
+ else
+ if (len(a) /= 2) error stop 1
+ if (any(a /= ['x1','x2','x3','x4','x5','x6','x7'])) error stop 2
+ end if
+ end subroutine s
+end program p