[Flang] Incorrect diagnosis on procedure pointer declaration using proc-interface being the procedure it is declared within.
Consider the following code:
FUNCTION foo() RESULT(ResFoo)
PROCEDURE(foo), POINTER :: ResFoo
END FUNCTION
Flang currently issues an error:
./t.f:1:14: error: Procedure 'foo' is recursively defined. Procedures in the cycle: 'foo', 'resfoo'
FUNCTION foo() RESULT(ResFoo)
^^^
./t.f:2:34: error: Procedure 'resfoo' is recursively defined. Procedures in the cycle: 'foo', 'resfoo'
PROCEDURE(foo), POINTER :: ResFoo
^^^^^^
I can't find anything in the standard that disallow this.
@llvm/issue-subscribers-bug
Author: Daniel Chen (DanielCChen)
Flang currently issues an error:
./t.f:1:14: error: Procedure 'foo' is recursively defined. Procedures in the cycle: 'foo', 'resfoo'
FUNCTION foo() RESULT(ResFoo)
^^^
./t.f:2:34: error: Procedure 'resfoo' is recursively defined. Procedures in the cycle: 'foo', 'resfoo'
PROCEDURE(foo), POINTER :: ResFoo
^^^^^^
I can't find anything in the standard that disallow this.
@llvm/issue-subscribers-flang-frontend
Author: Daniel Chen (DanielCChen)
Flang currently issues an error:
./t.f:1:14: error: Procedure 'foo' is recursively defined. Procedures in the cycle: 'foo', 'resfoo'
FUNCTION foo() RESULT(ResFoo)
^^^
./t.f:2:34: error: Procedure 'resfoo' is recursively defined. Procedures in the cycle: 'foo', 'resfoo'
PROCEDURE(foo), POINTER :: ResFoo
^^^^^^
I can't find anything in the standard that disallow this.
There probably isn't anything in the standard, but there's no good way to reason in Fortran about function interface(s) defined in terms of themselves. A C++ function that returns a pointer to itself is similarly impossible to declare.
gfortran and NAG compiler compile this code successfully. ifort gives a warning but let it pass. I have confirmed that the code itself is conforming. We have functional test cases like this that execute.
How do you know that the code conforms?
How do you know that the code conforms?
I checked with Malcolm Cohen at J3. I also couldn't find anything in the standard disallow it. We have functional test cases that are testing this.
The standard in several places requires that the characteristics of a procedure pointer target or actual procedure be the "same" as the characteristics of a procedure pointer or dummy procedure with an explicit interface. It does not define what "same" means, so I assume that it means that the representations of the characteristics can be compared and are identical. In the case of self-referential procedure pointer function results or dummy procedures or dummy procedure pointers, the characteristics are not finite, and they cannot be compared to see whether they are the "same" or not.
To demonstrate this in more detail, consider
subroutine selfRef(p)
procedure(selfRef) p
call p(p)
end
Is the call conforming? Subclause 15.5.2.10 paragraph 1 requires that "If the interface of a dummy procedure is explicit, its characteristics as a procedure (15.3.1) shall be the same as those of its effective argument, ...". But whether the characteristics of the actual and dummy procedures are the "same" or not is not defined in the standard. The naive interpretation of "same" -- calculating and comparing the characteristics of the explicit interfaces -- is not useful because those characteristics are infinite -- the characteristics of the procedures depend on the characteristics of their dummy procedures, which depend on the characteristics of the procedures, which depend on the characteristics of their dummy procedures, and so on forever.
If J3 were to define what "same" means in the context of self-referential procedure characteristics, I might revisit this bug. Until then, I am happy that these tests are "failing".
For whatever it's worth, that example above crashes gfortran, the Intel compilers, and nvfortran; elicits a fatal error from NAG; and produces a nonsensical error from XLF.
Yeah, the subroutine case you have is indeed troublesome. I agree that the standard should clarify about it especially when it passes itself as actual argument. I would also agree that it shouldn't be allowed.
However, I think the case I presented in this issue is different in the sense that the procedure pointer declaration using its defining function is to declare its result. The characteristics of its dummy arguments (if any) are the same (literally), and the function result is a procedure pointer to itself (so should be the same as well no matter what definition that would be).
I cooked up a very simple toy test case to show its functional usage.
procedure(foo), pointer :: pp, pp1
pp => foo()
pp1 => pp()
pp => bar()
pp1 => pp()
contains
FUNCTION foo() RESULT(ResFoo)
PROCEDURE(foo), POINTER :: ResFoo
print*, "in foo"
resfoo => bar
END FUNCTION
FUNCTION bar() RESULT(ResBar)
PROCEDURE(foo), POINTER :: ResBar
print*, "in bar"
resbar => foo
END FUNCTION
end
Currently, XLF prints
in foo
in bar
in bar
in foo