| Issue |
177601
|
| Summary |
[flang] function can be erroneously called as subroutine
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
ivan-pi
|
The following behavior looks surprising:
```
$ flang -pedantic mwe.f90 && ./a.out
calling get
3.
calling get_sub
hello
calling get_sub
$ flang --version
Homebrew flang version 21.1.8
Target: arm64-apple-darwin25.2.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/flang/21.1.8/libexec
Configuration file: /opt/homebrew/Cellar/flang/21.1.8/libexec/flang.cfg
Configuration file: /opt/homebrew/etc/clang/arm64-apple-darwin25.cfg
```
```
! mwe.f90
module mwe
implicit none
type :: action
contains
procedure :: get_fn
procedure :: get_sub
end type
contains
function get_fn(this) result(p)
class(action) :: this
procedure(), pointer :: p ! implicit subroutine interface ?
real :: p
print *, "calling get"
p => line
end function
function get_sub(this) result(p)
class(action) :: this
procedure(), pointer :: p
print *, "calling get_sub"
p => hello
end function
subroutine hello
print *, "hello"
end subroutine
real function line(x,c)
real :: x, c(2)
line = c(2)*x + c(1)
end function
end module
use mwe
implicit none
type(action) :: say_hello
procedure(), pointer :: p, s
real :: p
p => say_hello%get_fn()
if (associated(p)) print *, p(3.0,[0.0,1.0]) ! 3.0
s => say_hello%get_sub()
call s()
! call say_hello%get() ! Cannot call function 'get' like a subroutine
call say_hello%get_sub() ! Okay?
end
module mwe
implicit none
type :: action
contains
procedure :: get_fn
procedure :: get_sub
end type
contains
function get_fn(this) result(p)
class(action) :: this
procedure(), pointer :: p ! implicit subroutine interface ?
real :: p
print *, "calling get"
p => line
end function
function get_sub(this) result(p)
class(action) :: this
procedure(), pointer :: p
print *, "calling get_sub"
p => hello
end function
subroutine hello
print *, "hello"
end subroutine
real function line(x,c)
real :: x, c(2)
line = c(2)*x + c(1)
end function
end module
use mwe
implicit none
type(action) :: a
procedure(), pointer :: p, s
real :: p
p => a%get_fn()
if (associated(p)) print *, p(3.0,[0.0,1.0]) ! 3.0
s => a%get_sub()
call s()
!call a%get_fn() ! Cannot call function 'get' like a subroutine
call a%get_sub() ! Supposed to be illegal?
end
```
It is strange that calling `%get_fn()` like a subroutine is illegal, but calling `%get_sub()` works, although both of them being functions.
gfortran, ifort, ifx, nvfortran and Cray ftn all issue different errors on this example.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs