ecmwf-ifs / loki

Freely programmable source-to-source translation for Fortran
https://sites.ecmwf.int/docs/loki/
Apache License 2.0
22 stars 11 forks source link

Bug: import of private symbols affects the type inferece #309

Closed quepas closed 2 months ago

quepas commented 2 months ago

While importing symbols from modules, the access specificators (private, public) are not taken into account resulting in invalid type inference.

Here is the simplest example. Two modules with the same variable var, but with different access specification:

module mod_private
    private
    integer :: var
end module mod_private

and:

module mod_public
    public
    integer :: var
end module mod_public

Main module importing both modules and using the var variable:

module mod_main
    use mod_public
    ! Private module overshadows the `var` variable from the public module
    use mod_private
contains

    subroutine test_routine()
        integer :: result
        ! `var` comes from mod_private, but it should from mod_public
        result = var
    end subroutine test_routine

end module mod_main

And a test script:

from loki import Sourcefile, FP, OFP, OMNI

frontend = FP # Also OFP, OMNI
mod_private = Sourcefile.from_file("mod_private.f90", frontend=frontend).modules[0]
mod_public = Sourcefile.from_file("mod_public.f90", frontend=frontend).modules[0]

s = Sourcefile.from_file("mod_main.f90", frontend=frontend, definitions=[mod_private, mod_public])

var = s.modules[0].subroutines[0].body.body[1].rhs

print(f"Variable `var` type: {var.type}")

In the current version of loki, the type of var is: <SymbolAttributes BasicType.INTEGER, imported, module=Module:: mod_private>, but it should be: <SymbolAttributes BasicType.INTEGER, imported, module=Module:: mod_public>.

In the pull request #308 I have prepared a simple solution for FP, OFP, or OMNI.