https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123375

--- Comment #3 from Steve Kargl <kargl at gcc dot gnu.org> ---
(In reply to Steve Kargl from comment #2)
> (In reply to anlauf from comment #1)
> > I think the code is invalid.
> > 
> > NAG says:
> > 
> > % nagfor pr123375.f90
> > NAG Fortran Compiler Release 7.2(Shin-Urayasu) Build 7203
> > Error: pr123375.f90, line 7: C_INT has already been accessed by USE
> > association from module ISO_C_BINDING, so cannot be now be imported
> >        detected at IMPORT@C_INT
> > [NAG Fortran Compiler pass 1 error termination, 1 error]
> 
> Looks like NAG is invoking  Fortran 2023, 8.8:
> 
>    C8101 (R870) Each import-name shall be the name of an
>    entity in the host scoping unit.
> 
> In the code, the 'c_int' is USEd associated within the
> interface.  Looks like gfortran is trying to be lenient
> what it accepts.

Here's a slightly simplify version with four variations
of where the 'imported entity' is actually declared.  Note,
flang 21.1.5 compiles 'module bar4' while gfortran has 
an ICE.  I think NAG is right to reject the original code.
gfortran checks if an imported entity is available from
the host scope (which it is), but does not check if it is
also available from local scope.

   module foo
      implicit none
      integer aa
   end module foo
   !
   ! AA declared in bar1 and imported into the interface.
   ! This compiles with gfortran and flang21.
   !
   module bar1
      implicit none
      integer aa
      interface
         subroutine bah
            import aa
         end subroutine bah
      end interface
   end module bar1
   !
   ! AA made available by USE association in bar2, and imported into
   ! the interface.  This compiles with gfortran and flang21.
   !
   module bar2
      use foo
      implicit none
      interface
         subroutine bah
            import aa
         end subroutine bah
      end interface
   end module bar2
   !
   ! AA made available by USE association within bah, and then an
   ! import statement is invalid as is in the current scoping unit.
   !
   ! gfortran -c vv.f90
   ! vv.f90:40:17:
   !
   !   40 |          import aa
   !      |                 1
   ! Error: Cannot IMPORT 'aa' from host scoping unit at (1)...
   !
   ! flang21 -c vv.f90
   ! error: Semantic errors in vv.f90
   ! ./vv.f90:40:17: error: 'a' not found in host scope
   !           import aa
   !                  ^^
   module bar3
      implicit none
      interface
         subroutine bah
            use foo
            import aa
         end subroutine bah
      end interface
   end module bar3
   !
   ! AA made available by USE association within foo and bah.  The
   ! import statement should be invalid as AA is  the current scoping
   ! unit.
   !
   ! F2023:C8105 Within an interface body, an entity that is accessed
   ! by host association shall be accessible by host or use association
   ! within the host scoping unit, or explicitly declared prior to the
   ! interface body.
   !
   ! flang21 compiles this example.
   ! gfortran has an ICE.
   !
   module bar4
      use foo
      implicit none
      interface
         subroutine bah
            use foo      ! This brings aa into the interface, and
            import aa    ! should be an error.
         end subroutine bah
      end interface
   end module bar4

Reply via email to