llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang] Incorrect diagnosis on procedure pointer declaration using proc-interface being the procedure it is declared within.

Open DanielCChen opened this issue 2 years ago • 9 comments

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.

DanielCChen avatar Feb 21 '24 19:02 DanielCChen

@llvm/issue-subscribers-bug

Author: Daniel Chen (DanielCChen)

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.

llvmbot avatar Feb 21 '24 19:02 llvmbot

@llvm/issue-subscribers-flang-frontend

Author: Daniel Chen (DanielCChen)

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.

llvmbot avatar Feb 21 '24 19:02 llvmbot

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.

klausler avatar Feb 23 '24 22:02 klausler

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.

DanielCChen avatar Feb 26 '24 22:02 DanielCChen

How do you know that the code conforms?

klausler avatar Feb 26 '24 22:02 klausler

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.

DanielCChen avatar Feb 26 '24 23:02 DanielCChen

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".

klausler avatar Feb 26 '24 23:02 klausler

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.

klausler avatar Feb 26 '24 23:02 klausler

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

DanielCChen avatar Feb 27 '24 01:02 DanielCChen