llvm / llvm/llvm-project

[Flang] Compilation error for a generic function in nested submodules

Open
#208,360 1 comment 0 reactions 0 assignees View on GitHub
flang:frontend
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Summary

Flang rejects the following program. However, I believe this program is conforming to the Fortran standard.

* 15.5.5.2 Resolving procedure references to names established to be generic

> Otherwise, if the scoping unit has a host scoping unit, the name is established to be generic in that host scoping unit, and there is agreement between the scoping unit and the host scoping unit as to whether the name is a function name or a subroutine name, the name is resolved by applying the rules in this subclause to the host scoping unit as if the reference appeared there.

* 14.2.3 Submodules

> A module or submodule is an ancestor program unit of all of its descendants, which are its submodules and their descendants.

### Reproducer

* test.f90

```fortran
module m1
interface
module function s1()
end
module function s2(d)
end
module subroutine ss()
end
end interface
end

submodule (m1) smod
interface gen
procedure s1,s2
end interface
contains
module procedure s1
s1=1
end
module procedure s2
s2=d
end
end

submodule (m1:smod) smod2
contains
module subroutine ss()
c=gen()
if(c/=1) print *,901
c=gen(2.0)
if(c/=2) print *,902
end
end

use m1
call ss()
print *,'pass'
end
```

* commands

```console
$ flang --version
flang version 23.0.0git (https://github.com/llvm/llvm-project.git ecdd6403fd213f90243a3d354d4db3483b89471f)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/llvm/build/bin
Build config: +assertions
$ flang test.f90 && ./a.out
error: Semantic errors in test.f90
test.f90:28:5: error: No specific function of generic 'gen' matches the actual arguments
c=gen()
^^^^^
test.f90:30:5: error: No specific function of generic 'gen' matches the actual arguments
c=gen(2.0)
^^^^^^^^
```

## Non-problematic Conditions

The issue does not occur under the following conditions:
* Moving the generic interface to either the ancestor module (`m1`) or the innermost submodule (`smod2`).
* Converting functions to subroutines:
```diff
@@ -1,8 +1,8 @@
module m1
interface
- module function s1()
+ module subroutine s1()
end
- module function s2(d)
+ module subroutine s2(d)
end
module subroutine ss()
end
@@ -15,20 +15,16 @@
end interface
contains
module procedure s1
- s1=1
end
module procedure s2
- s2=d
end
end

submodule (m1:smod) smod2
contains
module subroutine ss()
- c=gen()
- if(c/=1) print *,901
- c=gen(2.0)
- if(c/=2) print *,902
+ call gen()
+ call gen(2.0)
end
end

```

## Other Compilers

* GFortran
```console
$ gfortran --version
GNU Fortran (GCC) 15.2.0
Copyright (C) 2025 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.

$ gfortran test.f90 && ./a.out
pass
```

* Intel Fortran
```console
$ ifx --version
ifx (IFX) 2025.3.2 20260112
Copyright (C) 1985-2026 Intel Corporation. All rights reserved.

$ ifx -O0 test.f90 && ./a.out
ld: /tmp/ifx1919201900Ee6pdS/ifxbe7XlC.o: in function `m1_mp_ss_':
test.f90:(.text+0x79): undefined reference to `m1.smod_mp_s1_'
ld: test.f90:(.text+0x100): undefined reference to `m1.smod_mp_s2_'
```

Contributor guide

Open the contributing guide

Research direction

Start by compiling the issue's test.f90 reproducer with flang and compare the failure at the two gen() calls against the Fortran standard sections cited in the report. Trace how Flang resolves the generic interface across m1, smod, and smod2. Done means the reproducer compiles and runs to print pass, while the existing subroutine and other-compiler behavior remain unaffected.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.