Shape analysis was building expressions using non constant explicit bounds.
This caused issues because shape analysis is actively used in front-end
expression rewrites, regardless of the context. When rewriting expressions
inside execution parts it is not guaranteed that non constant specification
expressions will evaluate to the same value they would inside the specification
parts.
Example:
fortran subroutine foo(n) real :: x(n) n = n + 100 print *, size(x, 1) end subroutine
Was unparsed with f18 -funparse to the following program that is not
semantically equivalent:
fortran SUBROUTINE foo (n) REAL x(n) n=n+100_4 PRINT *, int(int(n,kind=8)-1_8+1_8,kind=4) END SUBROUTINE
Currently, this impacted at least all rewrites of LBOUND, UBOUND, SIZE, SHAPE,
and SIZEOF.
This patch prevents usage of non constant explicit bounds in shape analysis
results, except when requested by the shape analysis user (in contexts where it
is known to be safe to use these). Otherwise, evaluate::DescriptorInquiry are
generated. The only place where keeping non constant explicit bounds in shape
analysis appeared to be needed so far was runtime type info generation (for
derived types).
I considered fixing it at the folding level (by discarding non constant results from
shape analysis), but it seemed less solid (more places to secure, hard to enforce all
usages of shape analysis do not end-up used somewhere else now or in the future). Also,
Being able to rewrite size(x+y) where y is an array to size(y) is safe and still an
improvement, and naively discarding non constant shape analysis results in folding was
preventing that from happening.
Typo, should be "contexts" rather than "context".