Open pawosm-arm opened 6 months ago
@wanbinchen-hnc Would you be able to take a look at this issue?
IMHO, this difference is an undefined behavior. As mentioned in paragraph 1 of section 12.3.3 of the Fortran 2008 standard, the characteristics of the function results include the shape, but not the lower bound.
In addition, as mentioned in paragraph 3 of section 7.2.1.3 of the Fortran 2008 standard, an allocatable assignment will result in the unallocated LHS having the same shape and lower bounds as the RHS. For the following testcase,
program main
print *, lbound(foo()), ubound(foo()), shape(foo())
contains
function foo()
integer :: foo(0:10, 2:5)
end function
end program
gfortran outputs: 1 1 11 4 11 4 flang outputs: 0 2 10 5 11 4 It seems that gfortran normalizes the lower bound of the function result while flang makes no changes.
Eventually, we'd contacted the Fortran committee and the response was that there is no undefined behaviour here, quite contrary, the behavior is well-defined as per J3/10-007r1 (Fortran 2008). The subclause 7.2.1.3 paragraph 3 states the array is allocated with o if /expr/ is an array, the shape of the /expr/ with each lower bound equal to the corresponding element of LBOUND(/expr/). The subclause 13.7.90 (LBOUND) says that in this case, the lower bounds have the value 1.
@pawosm-arm Section 13.7.90 on LBOUND
states the following for its result value:
If ARRAY is a whole array and either ARRAY is an assumed-size array of rank DIM or dimension DIM of ARRAY has nonzero extent, LBOUND(ARRAY, DIM) has a value equal to the lower bound for subscript DIM of ARRAY. Otherwise the result value is 1.
Could you explain why, in the example programs above, arr_in1
(as defined in the main program and passed into evaluate
) or foo()
should not be considered whole arrays?
This issue was signaled on discourse as such: https://fortran-lang.discourse.group/t/an-interesting-difference-between-compilers/7131
There is also a reproducer initially written for AOCC, but since AOCC is based on classic flang, it can also be reproduced with it:
Note that the most recent changes to LBOUND/UBOUND in classic flang did not improve the reported behavior.