[Flang] Missing diagnose that a type bound procedure cannot be passed as an actual argument.
Test case:
type dt
contains
procedure, nopass :: foo => real_foo
end type
interface
subroutine real_foo()
end
end interface
type(dt) :: t1
call sub1(t1%foo) !!! ERROR: Is it illegal to pass t1%foo as actual argument
end
Based on R1524 (F2018)
R1524 actual-arg is expr
or variable
or procedure-name
or proc-component-ref
or alt-return-spec
the binding name t1%foo is not in the list.
For reference, if I had
module m
type dt
contains
procedure :: foo => real_foo
end type
contains
subroutine real_foo(this)
class(dt) :: this
end
end
program main
use m
interface
subroutine sub1(arg)
import
class(dt) :: arg
end subroutine
end interface
type(dt) :: t1
call sub1(t1%foo)
end
It produces:
error: Semantic errors in t1.f
./t1.f:21:13: error: Actual argument 't1%foo' associated with dummy argument 'arg=' is not a variable or typed expression
call sub1(t1%foo)
^^^^^^
@llvm/issue-subscribers-flang-frontend
This is an intentional extension to the language.
I believe this is documented as:
A type-bound procedure binding can be passed as an actual argument corresponding to a dummy procedure and can be used as the target of a procedure pointer assignment statement.
in https://github.com/llvm/llvm-project/blob/d8ba9e505ac3a57211fade270532519adde374c2/flang/docs/Extensions.md
Also in that document, the general philosophy:
As a general principle, this compiler will accept by default and without complaint many legacy features, extensions to the standard language, and features that have been deleted from the standard, so long as the recognition of those features would not cause a standard-conforming program to be rejected or misinterpreted.
Just curious. Is there an option that triggers a warning that the usage is not standard conforming? I think it is a good usability feature to help users write portable code.
I don't know whether this case has one, but there are a bunch of portability warnings that can be enabled in parsing and semantics via the driver; it might be by way of -pedantic in the clang-based driver.
Ok. I see. Thanks @klausler @sscalpone for the pointer. There was some discussion on this topic within the Fortran standard committee. The reasoning for disallowing a type-bound procedure being passed as an actual argument or the target of a procedure pointer statement is that a type-bound procedure is invoked through an object. Without an object, the dynamic dispatch cannot be done. Consider the following code,
interface
subroutine real_foo1()
end
subroutine real_foo2()
end
end interface
type dt1
contains
procedure, nopass :: foo => real_foo1
end type
type, extends(dt1) :: dt2
contains
procedure, nopass :: foo => real_foo2
end type
class(dt1), allocatable :: t1
allocate(dt1:: t1)
call sub1(t1%foo)
deallocate(t1)
allocate(dt2:: t1)
call sub1(t1%foo)
deallocate(t1)
end
subroutine real_foo1()
print*, "real_foo1"
end
subroutine real_foo2()
print*, "real_foo2"
end
The t1%foo can be resolved to different procedures depending on the dynamic type of t1. In order to make it work, the compiler would need to insert a SELECT TYPE to check all the possible dynamic type of t1 and pass the address of the real_foo*. If it is something like t1%t2%t3...%foo, it would be quite expensive to enumerate every part-ref.
The TBPs can be resolved by loading the procedure address from the type table of the dynamic type of the object. It is exactly the same cheap process that is used to find the target of a call to the TBP, except that procedure is simply resolved, not called.
Thanks for the explanation. Just to clarify, to determine the dynamic type of t1 in this case, would it be a runtime process as t1 can have different dynamic type through out the compilation?
By "runtime process", I meant, the compiler would need to generate the code to determine its dynamic type and load the address of the correct TBP at the runtime.
No the dynamic type is stored in the addendum so it is just loaded from the descriptor.
Just as CALL t1%foo() needs resolution on any call via a simply offset load from a vtable, so does CALL sub1(t1%foo) or ptr => t1%foo. Same problem, same solution. The discussion on the J3 mailing list was generally misleading.
Thanks! I see how it works now from the caller side. The next question is how the dummy procedure is used on the callee side? It seems it cannot have an explicit interface as at the compile time, we don't know which real procedure is passed down.
As with any use of a dummy procedure or procedure pointer, any actual procedure that is associated will have been checked by semantics to ensure that it matches the requirements of the interface. Where the actual procedure comes from (subprogram names, incoming dummy procedure, procedure pointer) is not relevant to the use of the dummy procedure in the callee.
I am sorry if I missed something trivial. still quite new to Flang :(. Still trying to understand how this is a compile time decision. I cooked up a sample code based on the previous one to illustrate my confusion.
interface
subroutine real_foo1(arg1)
integer :: arg1
end
subroutine real_foo2(arg2, arg3)
integer :: arg2, arg3
end
end interface
type dt1
contains
procedure, nopass :: foo => real_foo1
end type
type, extends(dt1) :: dt2
contains
procedure, nopass :: foo => real_foo2
end type
class(dt1), allocatable :: t1
interface
subroutine sub1(proc_arg)
..... !!! How can I declare the interface of proc_arg so that it can be checked ?
end subroutine
end interface
if (condition) then
allocate(dt1:: t1)
call sub1(t1%foo)
deallocate(t1)
else
allocate(dt2:: t1)
call sub1(t1%foo)
deallocate(t1)
endif
end
In you example, you can't declare an interface for proc_arg that matches both real_foo1 and real_foo2, but that's not a problem for this extension, since this is not a valid program in any case. The declaration of the overriding TBP in the extension type dt2 is not valid since the interface is not compatible with the TBP that it overrides from dt1.
./t3.f90:15:26: error: A type-bound procedure and its override must have compatible interfaces
procedure, nopass :: foo => real_foo2
^^^
./t3.f90:2:16: Procedure 'foo' of type 'dt1' is bound to 'real_foo1'
subroutine real_foo1(arg1)
^^^^^^^^^
Sigh. You are right. I missed that.
Thanks @klausler for the detailed explanation! I had just one mor case to clarify to see if it is part of the extension, which is with the PASS attribute.
module m
type dt1
contains
procedure, pass :: foo => real_foo1
end type
type, extends(dt1) :: dt2
contains
procedure, pass :: foo => real_foo2
end type
interface
subroutine real_foo1(this, arg1)
import
class(dt1) :: this
integer :: arg1
end
subroutine real_foo2(this, arg1)
import
class(dt2) :: this
integer :: arg1
end
end interface
class(dt1), allocatable :: t1
end module
program main
use m
logical :: condition
interface
subroutine sub1(proc_arg, arg1)
import
interface
subroutine proc_arg(this, arg1)
!!!...... how to declare *this" argument?
integer :: arg1
end subroutine
end interface
end subroutine
end interface
if (condition) then
allocate(dt1:: t1)
call sub1(t1%foo, 1)
deallocate(t1)
else
allocate(dt2:: t1)
call sub1(t1%foo, 1)
deallocate(t1)
endif
end
module m
type dt1
contains
procedure, pass :: foo => real_foo1
end type
type, extends(dt1) :: dt2
contains
procedure, pass :: foo => real_foo2
end type
interface
subroutine real_foo1(this, arg1)
import
class(dt1) :: this
integer :: arg1
end
subroutine real_foo2(this, arg1)
import
class(dt2) :: this
integer :: arg1
end
end interface
class(dt1), allocatable :: t1
end module
program main
use m
logical :: condition
interface
subroutine sub1(proc_arg, arg1)
import
interface
subroutine proc_arg(this, arg1)
import dt1
class(dt1) this
integer :: arg1
end subroutine
end interface
integer arg1
end subroutine
end interface
if (condition) then
allocate(dt1:: t1)
call sub1(t1%foo, 1)
deallocate(t1)
else
allocate(dt2:: t1)
call sub1(t1%foo, 1)
deallocate(t1)
endif
end
Thanks again for the explanation! All the cases that I can think of are covered. I will close this issue.