This is the current situation of the implementation. We suggest you remove the “IMPORT, ALL” from the “SUBMODULE (N) S”.
Without IMPORT, ALL, the submodule can still access module N’s entities (including the declaration of S) through implicit host association.
Host association means that everything declared in the parent module (N) is automatically accessible in the submodule - you don’t need IMPORT for basic access.
Here’s the key difference:
SUBMODULE (N) S
USE :: M, ONLY : T
! No IMPORT statement
IMPLICIT NONE
CONTAINS
MODULE SUBROUTINE S
print *, “in submodule S module subroutine S”
END SUBROUTINE S
END SUBMODULE S
In this case:
- The submodule is implementing the module subroutine S that was declared in N
- It doesn’t need to explicitly “import” the declaration - it knows about it through host association
- The implementation just provides the body for the interface declared in module N
When do you need IMPORT?
IMPORT is mainly needed in interface bodies or when you want to make host association explicit (to resolve potential ambiguities or for clarity). For example:
INTERFACE
MODULE SUBROUTINE some_proc(x)
IMPORT :: some_type ! Need IMPORT here to use host’s some_type
TYPE(some_type) :: x
END SUBROUTINE
END INTERFACE
The conflict with IMPORT, ALL:
When you add IMPORT, ALL, it makes the host association explicit, and the compiler then checks for conflicts with USE-associated names. That’s why the error occurs - without it, the implicit host association doesn’t trigger the same conflict check.
You may try out the following example:
$ cat runme.sh
#!/bin/bash
## Please also check the Stack Overflow post, the import statement can only be in interface scope in Fortran 2008, but is extended to be allowed in any scoping unit that has a host scoping unit, such as module procedures within a submodule in Fortran 2018
## https://stackoverflow.com/questions/67514373/import-statement-within-module-procedures
module purge
module load intel/oneapi/2026.0
ifx -c test.f90
rm test.o
ifx test.f90 submod_m.f90 driver2.f90 -o driver2
./driver2
rm ./driver2
gfortran --version
gfortran test.f90 submod_m.f90 driver2.f90 -o driver2
./driver2
$ cat test.f90
MODULE M
IMPLICIT NONE
TYPE :: T
CONTAINS
PROCEDURE, NOPASS :: S => T_S
! PROCEDURE, NOPASS :: SOME_OTHER_BINDING_NAME => T_S
END TYPE T
INTERFACE
MODULE SUBROUTINE T_S
END SUBROUTINE T_S
END INTERFACE
END MODULE M
MODULE N
IMPLICIT NONE
INTERFACE
MODULE SUBROUTINE S
END SUBROUTINE S
END INTERFACE
END MODULE N
SUBMODULE (N) S
USE :: M, ONLY : T
!IMPORT, ALL !! implicit host association in submodule to bring in parent module entities, no need to “IMPORT”. The IMPORT makes the host association explicit and triggers checks for conflicts with USE-associated names
IMPLICIT NONE
CONTAINS
MODULE SUBROUTINE S
IMPORT, ALL
print *, “in submodule S module subroutine S”
END SUBROUTINE S
END SUBMODULE S
$ cat submod_m.f90
SUBMODULE (M) M_impl
IMPLICIT NONE
CONTAINS
MODULE SUBROUTINE T_S
print *, “in module M’s T_S subroutine”
END SUBROUTINE T_S
END SUBMODULE M_impl
shiquans@ortce-skl21:/scratch/users/shiquans/home_backup-20260505/osc_06873441$ cat driver2.f90
PROGRAM driver2
USE :: M
USE :: N
IMPLICIT NONE
TYPE(T) :: my_t
print *, “=== Testing M’s S (type-bound procedure) ===”
CALL my_t%S()
print *, “”
print *, “=== Testing N’s S (module subroutine from submodule) ===”
CALL S()
print *, “”
print *, “Driver2 program completed successfully”
END PROGRAM driver2
$ . ./runme.sh
=== Testing M’s S (type-bound procedure) ===
in module M’s T_S subroutine
=== Testing N’s S (module subroutine from submodule) ===
in submodule S module subroutine S
Driver2 program completed successfully
GNU Fortran (Ubuntu 13.2.0-23ubuntu4) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
test.f90:30:25:
30 | MODULE SUBROUTINE S
| 1
Error: Name ‘s’ at (1) is an ambiguous reference to ‘n.s’ from current program unit
test.f90:31:21:
31 | IMPORT, ALL
| 1
Error: IMPORT statement at (1) only permitted in an INTERFACE body
test.f90:32:59:
32 | print *, “in submodule S module subroutine S”
| 1
Error: Unexpected WRITE statement in CONTAINS section at (1)
test.f90:33:9:
33 | END SUBROUTINE S
| 1
Error: Expecting END SUBMODULE statement at (1)
-bash: ./driver2: No such file or directory