When is a fortran namelist public

I am confused, but I may not be reading the standard correctly. The following code

module m
real,private:: a
contains
subroutine sub
integer :: b
namelist /n/ a,b
end subroutine sub
end module m

results in an error when compiled with “-Mstandard”:

$ nvfortran -Mstandard -c namelist_test.f90
NVFORTRAN-S-1220-PUBLIC namelist /n/ has a PRIVATE namelist object (a) (namelist_test.f90: 6)
0 inform, 0 warnings, 1 severes, 0 fatal for sub

I this correct? I do not understand is why /n/ is public; it is inside the body of a subroutine.

This problem is the synthesis from a large code where some variables in the namelist (e.g. “verbose”) are private module variables used throughout the internal module routines, and set in a namelist statement in the “init” procedure.

I could of course move “b” and the namelist to the module body and make them “private”, but this pollutes the module namespace, and some of the modules have +10.000 lines and many routines. Is there a workaround to declare a namelist “private” in a subroutine?

Gfortran and ifort do not identify this as a problem.

best

Troels

Hi Troels,

Which version of the Fortran standard are you looking at?

I asked our experts here who noted that this restriction was just removed in the F2023 standard. F2018 and earlier stated:

C8105 (R868) A namelist-group-object shall not have the PRIVATE attribute if the namelist-group-name has the PUBLIC attribute.

nvfortran supports F2003 with some F2018 and F2023. Our new language feature development is being put into our joint project with the LLVM community’s F18 project, aka “new flang”. At some point once it is feature complete and stable, we’ll replace nvfortran with the new flang.

However, this may be an easy constraint to remove so I’ve added an RFE, TPR#35766, and asked engineering to take a look.

-Mat

I read the same after posting. The question still remains:

  • Why is a namelist in a subroutine public?

Alternatively:

  • how can a namelist in a subroutine be made private?

Best

Troels

I don’t believe you can. The constraint is Fortran 2018 and earlier and it is obviously flawed because a namelist cannot be declared public/private in a subprogram. That is probably why the constraint went away in Fortran 2023. We will try to fix this so it meets the Fortran 2023 Standard or behave like other compilers in this case. In the meantime, consider not using -Mstandard for this program or move the namelist to the module specification section. The namelist can be private in the module specification section when there is a private statement for the whole module.