2019-08-30 Jakub Jelinek Backported from mainline 2019-01-19 Jakub Jelinek PR fortran/88902 * trans-decl.c (gfc_get_symbol_decl): Don't add length to function or parent function if it has been added there already. * gfortran.dg/pr88902.f90: New test. --- gcc/fortran/trans-decl.c (revision 268622) +++ gcc/fortran/trans-decl.c (revision 268623) @@ -1602,13 +1602,17 @@ gfc_get_symbol_decl (gfc_symbol * sym) if (VAR_P (length) && DECL_FILE_SCOPE_P (length)) { /* Add the string length to the same context as the symbol. */ - if (DECL_CONTEXT (sym->backend_decl) == current_function_decl) - gfc_add_decl_to_function (length); - else - gfc_add_decl_to_parent_function (length); + if (DECL_CONTEXT (length) == NULL_TREE) + { + if (DECL_CONTEXT (sym->backend_decl) + == current_function_decl) + gfc_add_decl_to_function (length); + else + gfc_add_decl_to_parent_function (length); + } - gcc_assert (DECL_CONTEXT (sym->backend_decl) == - DECL_CONTEXT (length)); + gcc_assert (DECL_CONTEXT (sym->backend_decl) + == DECL_CONTEXT (length)); gfc_defer_symbol_init (sym); } --- gcc/testsuite/gfortran.dg/pr88902.f90 (nonexistent) +++ gcc/testsuite/gfortran.dg/pr88902.f90 (revision 268623) @@ -0,0 +1,6 @@ +! PR fortran/88902 +! { dg-do compile } +! { dg-require-effective-target lto } +! { dg-options "-flto --param ggc-min-heapsize=0" } + +include 'pr50069_2.f90'