From: Mikael Morin <mik...@gcc.gnu.org> PR 121185 exhibited wrong code examples where for assignements such as: class_obj%non_poly = class_obj%non_poly * x polymorphic references are (wrongly) used to access the left-hand-side array.
This is caused by: 1. In the scalarizer, we delay (since r16-2248) evaluation of the left hand side array descriptor to a variable. The reallocation code, which would contain the delayed evaluation to a variable, is not emitted because we can see that the right hand side has conformable shape. So the full reference to the array descriptor remains (this is the cause of the regression). 2. The r16-2371 code, that should factor array descriptor references to variables (it was originally written to catch almost the same bug), has a hole in it, so the full array descriptor passes through unmodified. 3. gfc_get_class_from_expr, which now has access to the full reference, returns 'class_obj' as the class container, as none was found for the child reference 'class_obj%non_poly'. This causes the array reference to be evaluated in a polymorphic way (this is the bug). The wrong code bug can be fixed by attacking either of the three causes. The three causes seem worth fixing, so the follow-up messages contain the fixes for the three causes (in reversed order). Regression-tested on x86_64-pc-linux-gnu. Jürgen also confirmed patches 1 and 3 resolve his original issues. OK for master? Mikael Morin (3): fortran: Bound class container lookup after array descriptor [PR121185] fortran: Trigger reference saving on pointer dereference [PR121185] fortran: Consistently use the same assignment reallocation condition [PR121185] gcc/fortran/trans-array.cc | 54 ++++++++++++++++--------- gcc/fortran/trans-expr.cc | 35 +++++++++++++--- gcc/testsuite/gfortran.dg/assign_13.f90 | 25 ++++++++++++ gcc/testsuite/gfortran.dg/assign_14.f90 | 24 +++++++++++ 4 files changed, 114 insertions(+), 24 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/assign_13.f90 create mode 100644 gcc/testsuite/gfortran.dg/assign_14.f90 -- 2.47.2