Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28613 Discussions

Bug with IMPORT statement in submodule

OP1
New Contributor III
362 Views

The following code triggers a spurious compiler error.

My understanding is that the variable J is "the name of one or more entities accessible in the host scoping unit" (per the documentation) and therefore its use in the IMPORT statement should be valid.

MODULE M
IMPLICIT NONE (TYPE, EXTERNAL)
INTEGER, PARAMETER :: J = 1
END MODULE M


MODULE N
USE M
IMPLICIT NONE (TYPE, EXTERNAL)
INTERFACE
    MODULE SUBROUTINE S
    END SUBROUTINE S
END INTERFACE
END MODULE N


SUBMODULE (N) S
IMPORT, ONLY : S, J   ! This line triggers a compiler error.
IMPLICIT NONE (TYPE, EXTERNAL)
CONTAINS
    MODULE SUBROUTINE S
    IMPLICIT NONE (TYPE, EXTERNAL)
    WRITE(*, *) J
    END SUBROUTINE S
END SUBMODULE S

 

0 Kudos
4 Replies
Ron_Green
Moderator
340 Views

I'll have to get our Standards members to look at this one.  First, ifx is in agreement with gfortran 13 and Nag that this IMPORT is illegal.  That is not to say that all 3 are right.  Perhaps all 3 compilers are getting this wrong.  Surely this deserves a more in-depth analysis with the Standard experts.  @OP1 your code is usually conformant so I'm inclined to side with you that this should be legal.  My reading would suggest the IMPORT has to go in the INTERFACE in MODULE N.  But with submodules my intuition may be flawed.

0 Kudos
andrew_4619
Honored Contributor III
303 Views

F2018 had significant changes in IMPORT and from my initial reading I think it may be valid.

0 Kudos
Ron_Green
Moderator
271 Views

@OP1 Jon Steidel agrees with you, it seems valid.  I will start a bug report for ifx.  His further analysis

 

Fortran 2018 expanded the use of the IMPORT statement to allow it in BLOCKs, internal procedures, and module procedures. There is a constraint

 

                C8100     An IMPORT statement shall not appear in the scoping unit of a main-program, external-subprogram, module, or block-data

 

which does not prohibit it in a submodule.  Then there is another constraint

 

                C8103      IMPORT, NONE shall not appear in the scoping unit of a submodule

 

So, it seems that this test case is valid.  I wrote a couple simple tests and it appears NAG and gfortran have not yet implemented this F2018 feature.  

0 Kudos
FortranFan
Honored Contributor III
260 Views

@Ron_Green ,

@andrew_4619 and Jon Steidel pointed you in the right direction.  And you are very likely correct in that both gfortran and NAG Fortran might yet have to catch up to this Fortran 2018 facility.  But here I thought I saw a blurb some place NAG Fortran is now conformant with Fortran 2018; if so, this is likely a bug in that compiler.

Anyways, you can pass this simpler variant to the Intel Fortran team which is processed as expected by IFX 2024.1.0 version (and that is really cool).  So the issue here is very likely when it comes to entities in a MODULE that are introduced via USE association rather than declared in the module itself.

 

MODULE N
IMPLICIT NONE (TYPE, EXTERNAL)
INTEGER, PARAMETER :: J = 1
INTERFACE
    MODULE SUBROUTINE S
    END SUBROUTINE S
END INTERFACE
END MODULE N


SUBMODULE (N) S
IMPORT, ONLY : S, J
IMPLICIT NONE (TYPE, EXTERNAL)
CONTAINS
    MODULE SUBROUTINE S
    IMPLICIT NONE (TYPE, EXTERNAL)
    WRITE(*, *) J
    END SUBROUTINE S
END SUBMODULE S

 

 

C:\temp>ifx /c /standard-semantics /free n.f
Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version 2024.1.0 Build 20240308
Copyright (C) 1985-2024 Intel Corporation. All rights reserved.


C:\temp>

 

 

0 Kudos
Reply