flang-compiler / f18

F18 is a front-end for Fortran intended to replace the existing front-end in the Flang compiler
230 stars 48 forks source link

scalar argument may not be associated with a dummy argument #1074

Closed naromero77 closed 4 years ago

naromero77 commented 4 years ago

This code snippet is extracted from a much large DOE code. It compiles with gfortran, pgi, (old) flang, but f18 gives:

flang -c -Mfree os_reduced.f 
os_reduced.f:18:11: error: Whole scalar actual argument may not be associated with a dummy argument 'command=' array
      ret = system_c(command_c)
            ^^^^^^^^^^^^^^^^^^^
/scratch/naromero/opt/spack/linux-ubuntu18.04-westmere/gcc-7.4.0/f18-master-yxbvzyykz5e5e5mwvwact3dukjyyyqjm/bin/f18: semantic errors in os_reduced.f

I am copying and pasting the code below:

module os_m
  use, intrinsic :: iso_c_binding

contains                                                                                                                                                                                
  !> Call command `command` using call to POSIX `system` routine.                                                                                                                       
  subroutine system(command)
    character(len=*), intent(in) :: command
    character(len=len(command)+1) :: command_c
    integer(c_int) :: ret
    interface !int system(const char *command);                                                                                                                                         
      integer(c_int) function system_c(command) bind(c, name='system')
        use, intrinsic :: iso_c_binding, only: c_int, c_char
        character(len=1,kind=c_char), intent(in) :: command(*)
      end function system_c
    end interface
    command_c(1:len(command)) = command
    command_c(len(command)+1:len(command)+1) = c_null_char
    ret = system_c(command_c)
  end subroutine system
end module os_m
LKedward commented 4 years ago

I believe the type of command_c here is incorrect. It should be an array of c interoperable characters (see in your interface definition for system), as opposed to a Fortran string.

Change:

character(len=len(command)+1) :: command_c

to:

character(len=1,kind=c_char) :: command_c(len(command)+1)

You will then have to copy the strings a character at a time I think, see s2c for example.

I can confirm that your original code also compiled without warning in latest intel fortran: I'm not sure why this isn't picked-up in the other compilers, so I may be missing something here.

naromero77 commented 4 years ago

Thanks for looking at the reduced test case. I have verified that your modified code works with Intel, Gfortran, and f18 compilers. At the moment, I cannot compile the full DOE science app because of this unrelated issue: https://github.com/flang-compiler/f18/issues/1020

Let us leave the issue open for now until I can get the application developer to fix it their code. But I will close it as soon as I can.

naromero77 commented 4 years ago

Original code is fixed and test passing with multiple compilers.