Nvfortran 23.9: optional dummy with NULL pointer actual argument is sometimes marked PRESENT

Since F08, nonpointer optional arguments may be associated with unallocated allocatables or unassociated pointers, rendering the optional argument not present (F08, 12.5.2.12).

There seems to be a bug in nvfortran up to at least version 23.9. If I compile this piece of code

PROGRAM optional_null
  
  IMPLICIT NONE
  REAL, POINTER :: p(:)

  p => NULL()
  CALL f(p)

CONTAINS
  SUBROUTINE f(optarg)
    REAL, OPTIONAL, INTENT(IN) :: optarg(:)

    IF (PRESENT(optarg)) THEN
      WRITE (*,*) 'optarg is present'
    ELSE
      WRITE (*,*) 'optarg is not present'
    END IF
  END SUBROUTINE
END PROGRAM

the expected outcome is that ‘optarg is not present’ is printed out every time. However, the actual execution yields

$ nvfortran optional_null.f90
$ for i in {1..10}; do ./a.out; done
 optarg is not present
 optarg is not present
 optarg is not present
 optarg is present
 optarg is present
 optarg is not present
 optarg is present
 optarg is present
 optarg is not present
 optarg is not present

Thanks Roland. I filed a problem report, TPR #35549, and sent it to engineering for investigation.

-Mat

1 Like