Bug of nvfortran 22.2-0: array subscript triplet handled wrongly

This is a bug of nvfortran 22.2-0 (on Ubuntu 20.04), which does not handle array subscript triplet correctly in some cases — especially when such a triplet leads to an empty array section.

Here is a minimal example. The latest version of the code is available in my GitHub repo dedicated to testing Fortran compilers.


program test_empty

implicit none

integer, parameter :: m = 1
integer, parameter :: n = 3
integer, parameter :: i = n
integer :: j, A(m, n)

do j = 1, n
    A(:, j) = j
end do
A = reshape([A(:, 1:i - 1), A(:, i + 1:n), A(:, i)], shape(A))
write (*, *) A

end program test_empty

Here is what happens when it is compiled with nvfortran -C -O3:

$ nvfortran --version && nvfortran -C -O3 test_empty.f90 -o test_empty && ./test_empty
nvfortran 22.2-0 64-bit target on x86-64 Linux -tp haswell NVIDIA Compilers and Tools
0: Subscript out of range for array a (test_empty.f90: 15)
     subscript=4, lower bound=1, upper bound=3, dimension=2

The error comes from A(:, i+1:n) in the array constructor [A(:, 1:i - 1), A(:, i + 1:n), A(:, i)]. According to the Fortran 2003 standard, A(:, i+1:n) (i.e., A(:, 4:3)) should be an empty array, but nvfortran seems to be treating as a nonempty one.

N.B.: If we remove either the -C or -O3 compilation option, the error will not arise.

I look forward to a fix of this bug. Thank you very much!

Thanks zaikunzhang,

I’ve filed this issue as TPR #31488 and sent it to engineering for further review.


1 Like

Hi zaikunzhang,

TPR #31488 has been fixed in our 22.5 release.


1 Like

Thanks. It works on my side.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.