Issue 242897
Summary [flang] Indirect module self-reference through a module file is reported as a missing name inside that module file
Labels flang:frontend
Assignees
Reporter eugeneepshteyn
    When a module USEs itself indirectly through a separately compiled module, flang reports a missing name located in the other module's `.mod` file instead of the self-reference. F2023 14.2.2 p1 says "A module shall not reference itself, either directly or indirectly."

Three files, compiled one after another in the same directory:

```fortran
! s1.f90
module m1
  integer :: a
end module
```

```fortran
! s2.f90
module m2
  use m1
  integer :: b
end module
```

```fortran
! s3.f90 -- a new version of m1 that now uses m2
module m1
  use m2
  integer :: c
end module
```

```console
$ flang -fsyntax-only s1.f90
$ flang -fsyntax-only s2.f90
$ flang -fsyntax-only s3.f90
error: Semantic errors in s3.f90
m2.mod:4:13: error: 'a' not found in module 'm1'
  use m1,only:a
              ^
```

(flang built from main at fe077b6e4150.) The error points into `m2.mod`, names an entity the user never referenced in `s3.f90`, and says nothing about `m1` depending on itself.

The same dependence inside one compilation unit is already diagnosed well:

```console
$ cat cycle.f90
module m2
  use m1
  integer :: b
end module
module m1
  use m2
  integer :: c
end module
$ flang -fsyntax-only cycle.f90
error: Semantic errors in cycle.f90
cycle.f90:5:8: error: Some modules in this compilation unit form one or more cycles of dependence
  module m1
         ^^
cycle.f90:1:8: Module in a cycle
  module m2
         ^^
```

gfortran 13.3 reports the three-file case at the USE statement:

```console
s3.f90:2:7:

    2 |   use m2
      |       1
Error: ‘m1’ of module ‘m2’, imported at (1), is also the name of the current program unit
```

What seems to happen: reading `m2.mod` processes its `use m1,only:a`. `ModFileReader::Read` looks `m1` up in the global scope and finds the module currently being compiled. That module has no module file hash, so the `!need$` hash that `m2.mod` records for `m1` doesn't reject it. The `only:a` is then resolved against the partially built `m1`, which has no `a` yet. The self-USE check in `ModuleVisitor::FindModule` doesn't fire, because at that point the current scope is `m2`'s, which `m1` does not contain.

Expected: an error at `use m2` in `s3.f90` that says `m1` references itself through `m2`, similar to the existing "Module 'm1' cannot USE itself".

Related: issue 227340 and PR 228489 (direct self-USE in the specification part), issue 227265 (a USE cycle within one compilation unit crashes with `-fopenmp`).

(This issue was filed with the help of AI)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to