[flang] Limit shape inquiries rewrite to associate construct entity
authorJean Perier <jperier@nvidia.com>
Wed, 21 Sep 2022 08:23:45 +0000 (10:23 +0200)
committerJean Perier <jperier@nvidia.com>
Wed, 21 Sep 2022 08:25:06 +0000 (10:25 +0200)
commitc4a73957f6c59f4f88d186c1a98327a826fbfb16
tree78762dc4b761237e4b11abc8ff0577f98e43872b
parent053df6ccafbb00d242fbc36ef9d4776dfc3552f3
[flang] Limit shape inquiries rewrite to associate construct entity

The previous code was rewriting all shape inquires on associate
construct entities to inquires on the associated expression or variable.

This is is incorrect because at the point of inquiry, some statement
between the association and the inquiry may have modified the expression
operands or variable in a way that changes its shapes or bounds.

For instance, in the example below, expression rewrites was previously
replacing `size(x, 1)` by `size(p, 1)` which is invalid if p is a
pointer.

```
associate(x => p + 1)
 call call_that_may_modify_p_shape()
 print *, size(x, 1)
end associate
```

This change restricts rewrites of shape inquiries on associate construct entity
to use the associated expression shape and bounds if and only if the
shape/bounds are compile time constant. Otherwise, this may be invalid.

Differential Revision: https://reviews.llvm.org/D133857
flang/include/flang/Evaluate/check-expression.h
flang/lib/Evaluate/check-expression.cpp
flang/lib/Evaluate/shape.cpp
flang/test/Evaluate/rewrite01.f90