fortran: Separate associate character lengths earlier [PR104570]
authorMikael Morin <mikael@gcc.gnu.org>
Sun, 13 Mar 2022 21:22:55 +0000 (22:22 +0100)
committerMikael Morin <mikael@gcc.gnu.org>
Sat, 19 Mar 2022 20:16:37 +0000 (21:16 +0100)
commit907811ddc35da6c1701ed22355ece63a8c3ed7fb
tree736d74938df30dbec55de8417f2fb677d833159a
parent638e630142b2cf691db890ab9eb6d77a65339c54
fortran: Separate associate character lengths earlier [PR104570]

This change workarounds an ICE in the evaluation of the character length
of an array expression referencing an associate variable; the code is
not prepared to see a non-scalar expression as it doesn’t initialize the
scalarizer.

Before this change, associate length symbols get a new gfc_charlen at
resolution stage to unshare them from the associate expression, so that
at translation stage it is a decl specific to the associate symbol that
is initialized, not the decl of some other symbol.  This
reinitialization of gfc_charlen happens after expressions referencing
the associate symbol have been parsed, so that those expressions retain
the original gfc_charlen they have copied from the symbol.
At translation stage, the gfc_charlen for the associate symbol is setup
with the decl holding the actual length value, but the expressions have
retained the original gfc_charlen without any decl.  So they need to
evaluate the character length, and this is where the ICE happens.

This change moves the reinitialization of gfc_charlen earlier at parsing
stage, so that at resolution stage the gfc_charlen can be retained as
it’s already not shared with any other symbol, and the expressions which
now share their gfc_charlen with the symbol are automatically updated
when the length decl is setup at translation stage.  There is no need
any more to evaluate the character length as it has all the required
information, and the ICE doesn’t happen.

The first resolve.cc hunk is necessary to avoid regressing on the
associate_35.f90 testcase.

PR fortran/104228
PR fortran/104570

gcc/fortran/ChangeLog:

* parse.cc (parse_associate): Use a new distinct gfc_charlen if the
copied type has one whose length is not known to be constant.
* resolve.cc (resolve_assoc_var): Reset charlen if it’s shared with
the associate target regardless of the expression type.
Don’t reinitialize charlen if it’s deferred.

gcc/testsuite/ChangeLog:

* gfortran.dg/associate_58.f90: New test.
gcc/fortran/parse.cc
gcc/fortran/resolve.cc
gcc/testsuite/gfortran.dg/associate_58.f90 [new file with mode: 0644]