23.9 regression: lbound bind(C)

Running test code below worked until NVHPC 23.9.

Example output with different compilers:

GCC version 13.2.0
Lneg1: lbound(A) = 1 ubound(A) = 4
Lneg1_C: lbound(A) = 1 ubound(A) = 4
Pneg1: lbound(A) = -1 ubound(A) = 2

nvfortran 23.7-0
Lneg1: lbound(A) = 1 ubound(A) = 4
Lneg1_C: lbound(A) = 1 ubound(A) = 4
Pneg1: lbound(A) = -1 ubound(A) = 2

nvfortran 23.9-0
Lneg1: lbound(A) = 1 ubound(A) = 4
Lneg1_C: lbound(A) = -1 ubound(A) = 2
Lneg1_C: unexpected lbound(A,1) -1
ERROR STOP

Test code

program pointer_bounds
!! explore lbound, ubound vis-a-vis pointer
!! Fortran allows arbitrary lbound, ubound.
!! In C array index starts at 0, but pointers can start in middle of array and then validly use negative indices.
use, intrinsic :: iso_c_binding, only : C_INT
use, intrinsic :: iso_fortran_env

implicit none

integer(C_INT), dimension(-1:2), target :: Ln1
integer(C_INT), pointer :: Pn1(:)


print '(a)', compiler_version()

call Lneg1(Ln1)
call Lneg1_C(Ln1)
!! bind(C) has no effect on lbound, ubound

Pn1 => Ln1
call Pneg1(Pn1)


contains

subroutine Lneg1(A)
integer(C_INT), dimension(:) :: A
print '(a,i0,a,i0)', "Lneg1: lbound(A) = ", lbound(A,1), " ubound(A) = ", ubound(A,1)
if (lbound(A,1) /= 1) error stop "Lneg1: unexpected lbound(A,1)"
if (ubound(A,1) /= 4) error stop "Lneg1: unexpected ubound(A,1)"
end subroutine Lneg1

subroutine Pneg1(A)
integer(C_INT), dimension(:), pointer :: A
print '(a,i0,a,i0)', "Pneg1: lbound(A) = ", lbound(A,1), " ubound(A) = ", ubound(A,1)
if (lbound(A,1) /= -1) error stop "Pneg1: unexpected lbound(A,1)"
if (ubound(A,1) /= 2) error stop "Pneg1: unexpected ubound(A,1)"
end subroutine Pneg1

subroutine Lneg1_C(A) bind(C)
integer(C_INT), dimension(:) :: A
print '(a,i0,a,i0)', "Lneg1_C: lbound(A) = ", lbound(A,1), " ubound(A) = ", ubound(A,1)
if (lbound(A,1) /= 1) then
  write(error_unit,'(a,i0)') "Lneg1_C: unexpected lbound(A,1) ", lbound(A,1)
  error stop
endif
if (ubound(A,1) /= 4) then
  write(error_unit, '(a,i0)') "Lneg1_C: unexpected ubound(A,1) ", ubound(A,1)
  error stop
endif
end subroutine lneg1_C
end program

Thanks Scivision,

I’ve filed a problem report, TPR #34430, and sent it to engineering for review.

We’ve just added initial support for F2018’s C to Fortran Interface (CFI) so my guess is that this regression occurred due to that work. Though, I’ll let engineering dig into it.

-Mat

FYI, TPR #34430 has been fixed in our 23.11 release.

% nvfortran -V23.11 -fast test.F90 ; a.out
nvfortran 23.11-0
Lneg1: lbound(A) = 1 ubound(A) = 4
Lneg1_C: lbound(A) = 1 ubound(A) = 4
Pneg1: lbound(A) = -1 ubound(A) = 2

Thanks again for the report,
Mat