[flang] prevent undefined behavior in character MAXLOC folding
authorJean Perier <jperier@nvidia.com>
Mon, 28 Mar 2022 17:05:36 +0000 (19:05 +0200)
committerJean Perier <jperier@nvidia.com>
Tue, 29 Mar 2022 07:34:09 +0000 (09:34 +0200)
commitec13942e71daad88267d73ff57b854b816c2a722
treef8f582f3dff0f1ccbd119062944426e38ad182d8
parent2c503dcb4f1d9206c2f789f7ff738fb1f0a6629c
[flang] prevent undefined behavior in character MAXLOC folding

When folding MAXLOC/MINLOC, the current element being compared was moved twice
in row in case it became the new extremum. With numeric and logical types, it
made no difference (std::move is a no-op for them), but for characters
where the string storage is actually moved, it caused the new extremum to
be set to the empty string, leading to wrong results.

Note: I could have left the first std::move relating to logical Findloc, but it
brings nothing and makes the code less auditable, so I also removed it.

Differential Revision: https://reviews.llvm.org/D122590
flang/lib/Evaluate/fold-integer.cpp
flang/test/Evaluate/fold-findloc.f90