简体   繁体   中英

Fortran derived-type parameter with allocatables

I have a derived-type with pointer (or allocatable) component

type  Tmy
  real, dimension(:), pointer :: vals
  integer :: nVals 
end type Tmy

And I want to create a parameter, eg to pass it to a subroutine as intent(in):

type (Tmy), parameter, public :: my_missing = Tmy(null(), 0)

Currently a similar code compiles fine to me by GNU Fortran (Spack GCC) 9.4.0, but introduction of the parameter triggers numerous strange errors in another module that uses the current one. The using module has variables of the type, but no reference to the parameter. The first error is triggered in a derived-type declaration with default initialization:

      550 |     logical, dimension(:,:), pointer :: ifValid => null()   
          |                                                         1
 Error: Cannot change attributes of USE-associated symbol null at (1)

and follows by numerous similar errors and complains about missing members in various structures. If I comment out the parameter declaration the code compiles and runs just fine.

So the questions are:

  1. How introducing a parameter in one module can trigger compilation errors in another module, given that the name my_missing is unique within the project? Or is it just a bug in the compiler?
  2. Is introducing such a parameter a valid practice in Fortran?
  3. If it is valid, what is the effect of assigning this parameter to a structure that has vals already allocated (in case if vals is declared as allocatable)?

PS Yes, I know that allocating pointers is a bad idea. I try to turn them to allocatables bit by bit..

UPD: The issue is reproducible with two different versions of gfortran, but only at one specific computer. Also I did not manage to compile any observable MWE so far..

UPD2: Thank you all, who commented so far. It seems clear that there is no breaking of Fortran standards in my examples here. MWE is of little use since the issue is reproducible only at one computer. I'll then ask for help from our HPC staff and come back once something interesting found.

TL;DR

Bug in gfortran-9 (9.4.0 from Spack, 9.5.0-1ubuntu1~22.04). Other gfortran compilers available for Ubuntu 22.04 (10.4.0-4ubuntu1~22.04,11.3.0-1ubuntu1~22.04, and 12.1.0-2ubuntu1~22.04) work just fine.

Details

I have managed to reproduce the issue in Ubuntu 20.04, by changing to gcc-9. Here is the way to reproduce it:

  1. Make sure that gcc-9 is called for gfortran
  2. Checkout and compile https://github.com/fmidev/silam-model/ See README there for more details
  3. Edit source/namelist.silam.mod.f90 with adding after the definition of Tsilam_namelist (around l. 257):
! Enabling these lines causes a mysterious error at  GNU Fortran 9.4.0 and 9.5.0
  type(Tsilam_namelist), parameter, private  ::  silam_namelist_missing = Tsilam_namelist( &
                                  & null(), int_missing, int_missing, int_missing, &
                                  & silja_false, "Missing_namelist" &
                                  & )  
  1. make clean
  2. make full

The problematic lines were added in our development branch quite some time ago. The only way to pinpoint the line that triggers the bug was a bisect on many revisions. Luckily, the revision that introduced the trigger was small.

The specific bug in gcc-9 responsible for the problem is yet unknown, but it is more of an academic interest...

Thank everyone once again for valuable comments!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM