https://gcc.gnu.org/g:d6e5d79340fc1e95b01a2b59a6e8060685c9d494
commit r17-4419-gd6e5d79340fc1e95b01a2b59a6e8060685c9d494 Author: Paul Thomas <[email protected]> Date: Fri Sep 18 16:12:40 2026 +0100 Fortran: Fix a number of issues with parameterized strings [PR127146] 2026-09-18 Paul Thomas <[email protected]> gcc/fortran PR fortran/127146 * primary.cc (gfc_match_rvalue): At two points: if the entry in the parameter list has a name, use that in preference to the component name. gcc/testsuite PR fortran/127146 * gfortran.dg/pdt_96.f03: New test. Diff: --- gcc/fortran/primary.cc | 6 ++++-- gcc/testsuite/gfortran.dg/pdt_96.f03 | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/gcc/fortran/primary.cc b/gcc/fortran/primary.cc index d167036b8086..f3708f125d70 100644 --- a/gcc/fortran/primary.cc +++ b/gcc/fortran/primary.cc @@ -4225,7 +4225,8 @@ gfc_match_rvalue (gfc_expr **result) { if (c->attr.pdt_kind || c->attr.pdt_len) continue; - tmp->name = c->name; + if (!tmp->name) + tmp->name = c->name; tmp = tmp->next; } @@ -4251,7 +4252,8 @@ gfc_match_rvalue (gfc_expr **result) /* Can now add all the component names. */ for (c = pdt_sym->components; c && tmp; c = c->next) { - tmp->name = c->name; + if (!tmp->name) + tmp->name = c->name; tmp = tmp->next; } } diff --git a/gcc/testsuite/gfortran.dg/pdt_96.f03 b/gcc/testsuite/gfortran.dg/pdt_96.f03 new file mode 100644 index 000000000000..6aa9b6e5a50c --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pdt_96.f03 @@ -0,0 +1,32 @@ +! { dg-do run } +! Test the fix for PR127146, where the structure constructors caused errors as +! shown below. +! +! Contributed by Amir Shamoradi <[email protected]> +! +module mod_pdt + implicit none + integer, parameter :: IK = kind(1) + integer, parameter :: IK8 = kind(1_8) + type :: pdt_parent(IKP) + integer, kind :: IKP + integer(IKP) :: ivalp + end type + type, extends(pdt_parent) :: pdt_child(IKC) + integer, kind :: IKC + integer(IKC) :: ivalc + end type +end module mod_pdt + + use mod_pdt + implicit none + type(pdt_child(IKP = IK, IKC = IK)) :: child1 = pdt_child(IKP = IK, IKC = IK)(ivalp = 1, ivalc = 2) + !! double free or corruption (fasttop) + + type(pdt_child(IKP = IK, IKC = IK8)) :: child2 + + child2 = pdt_child(IKP = IK, IKC = IK8)(ivalp = 3, ivalc = 4) + !! Error: Component 'ivalc' is initialized twice in the structure constructor at (1) + if ((child1%ivalp /= 1) .or. (child1%ivalc /= 2)) stop 1 + if ((child2%ivalp /= 3) .or. (child2%ivalc /= 4) .or. (kind (child2%ivalc) /= kind (1_8))) stop 2 +end
