llvm / llvm/llvm-project

[Flang] Compilation error for procedure pointer of functions results when POINTER statements follow EXTERNAL statements

Open
#199,876 2 comments 0 reactions 0 assignees View on GitHub
confirmed flang:frontend
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Summary

Flang reports a semantic error for procedure pointer of functions results when POINTER statements follow EXTERNAL statements. I could not find a rationale for this behavior in the Fortran standard, as it does not seem to specify an order for these statements.

### Reproducer

* test.f90

```fortran
module mod
contains
function f() result(r)
external :: r
pointer :: r
r => null()
end function
end

use mod
external :: p
pointer :: p
p=>f()
if (associated(p)) print *,'error'
print *,'pass'
end
```

* commands

```console
$ flang --version
flang version 23.0.0git (https://github.com/llvm/llvm-project.git f95ccbae8bd70e56e31fc04d6307d317212b7fbc)
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:3:12: error: Procedure 'r' is referenced before being sufficiently defined in a context where it must be so
function f() result(r)
^
test.f90:4:17: error: EXTERNAL attribute not allowed on 'r'
external :: r
^
test.f90:3:23: Declaration of 'r'
function f() result(r)
^
test.f90:13:4: error: Procedure 'r' is referenced before being sufficiently defined in a context where it must be so
p=>f()
^^^
```

## Non-problematic Conditions

The issue does not occur under the following conditions:
* Putting the EXTERNAL statement after the POINTER statement:
```diff
@@ -1,8 +1,8 @@
module mod
contains
function f() result(r)
- external :: r
pointer :: r
+ external :: r
r => null()
end function
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
pass
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.