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

[Flang] Incorrect diagnose when pointer assign to intrinsic INDEX.

Open DanielCChen opened this issue 2 years ago • 10 comments

Consider the following code:

  MODULE M

  INTERFACE
   FUNCTION ExtINDEX(Arg1, Arg2, Arg3, Arg4)
      INTEGER :: ExtINDEX
      CHARACTER(*), INTENT(IN) :: Arg1, Arg2
      LOGICAL, OPTIONAL, INTENT(IN) :: Arg3
     INTEGER, OPTIONAL, INTENT(IN) :: Arg4
    END FUNCTION
  END INTERFACE

  END MODULE

  PROGRAM PtrAssignProcNameIntrin4
  USE M
  IMPLICIT NONE

  PROCEDURE(ExtINDEX),  POINTER :: PtrINDEX
  PtrINDEX => index

  END

Flang currently issues an error:

./t.f:19:3: error: Procedure pointer 'ptrindex' associated with incompatible procedure designator 'index': distinct numbers of dummy arguments
    PtrINDEX => index
    ^^^^^^^^^^^^^^^^^
./t.f:18:36: Declaration of 'ptrindex'
    PROCEDURE(ExtINDEX),  POINTER :: PtrINDEX
                                     ^^^^^^^^

The code seems conforming to me.

DanielCChen avatar Feb 20 '24 18:02 DanielCChen

@llvm/issue-subscribers-flang-frontend

Author: Daniel Chen (DanielCChen)

Consider the following code: ``` MODULE M

INTERFACE FUNCTION ExtINDEX(Arg1, Arg2, Arg3, Arg4) INTEGER :: ExtINDEX CHARACTER(*), INTENT(IN) :: Arg1, Arg2 LOGICAL, OPTIONAL, INTENT(IN) :: Arg3 INTEGER, OPTIONAL, INTENT(IN) :: Arg4 END FUNCTION END INTERFACE

END MODULE

PROGRAM PtrAssignProcNameIntrin4 USE M IMPLICIT NONE

PROCEDURE(ExtINDEX), POINTER :: PtrINDEX PtrINDEX => index

END


Flang currently issues an error:

./t.f:19:3: error: Procedure pointer 'ptrindex' associated with incompatible procedure designator 'index': distinct numbers of dummy arguments PtrINDEX => index ^^^^^^^^^^^^^^^^^ ./t.f:18:36: Declaration of 'ptrindex' PROCEDURE(ExtINDEX), POINTER :: PtrINDEX ^^^^^^^^


The code seems conforming to me. 

</details>

llvmbot avatar Feb 20 '24 18:02 llvmbot

@llvm/issue-subscribers-bug

Author: Daniel Chen (DanielCChen)

Consider the following code: ``` MODULE M

INTERFACE FUNCTION ExtINDEX(Arg1, Arg2, Arg3, Arg4) INTEGER :: ExtINDEX CHARACTER(*), INTENT(IN) :: Arg1, Arg2 LOGICAL, OPTIONAL, INTENT(IN) :: Arg3 INTEGER, OPTIONAL, INTENT(IN) :: Arg4 END FUNCTION END INTERFACE

END MODULE

PROGRAM PtrAssignProcNameIntrin4 USE M IMPLICIT NONE

PROCEDURE(ExtINDEX), POINTER :: PtrINDEX PtrINDEX => index

END


Flang currently issues an error:

./t.f:19:3: error: Procedure pointer 'ptrindex' associated with incompatible procedure designator 'index': distinct numbers of dummy arguments PtrINDEX => index ^^^^^^^^^^^^^^^^^ ./t.f:18:36: Declaration of 'ptrindex' PROCEDURE(ExtINDEX), POINTER :: PtrINDEX ^^^^^^^^


The code seems conforming to me. 

</details>

llvmbot avatar Feb 20 '24 18:02 llvmbot

That's basically the same error message that comes out for this test from both NAG and XLF. Using INDEX as a specific intrinsic procedure is somewhat fraught because the fourth optional argument (KIND=) can't determine the kind of the integer result type.

klausler avatar Feb 23 '24 22:02 klausler

I messed up the code. I updated it with only 3 arguments. XLF compiles the updated code successfully.

DanielCChen avatar Feb 24 '24 16:02 DanielCChen

I messed up the code. I updated it with only 3 arguments. XLF compiles the updated code successfully.

If there is still a bug to be fixed in f18, please post the updated test case; otherwise, please close this bug report.

klausler avatar Feb 26 '24 17:02 klausler

  MODULE M

  INTERFACE
   FUNCTION ExtINDEX(Arg1, Arg2, Arg3)!, Arg4)
      INTEGER :: ExtINDEX
      CHARACTER(*), INTENT(IN) :: Arg1, Arg2
      LOGICAL, OPTIONAL, INTENT(IN) :: Arg3
!     INTEGER, OPTIONAL, INTENT(IN) :: Arg4
    END FUNCTION
  END INTERFACE

  END MODULE

  PROGRAM PtrAssignProcNameIntrin4
  USE M
  IMPLICIT NONE

  PROCEDURE(ExtINDEX),  POINTER :: PtrINDEX
  PtrINDEX => index

  END

Both XLF and ifort accept this updated test case, but gfortran and Flang reject it.

DanielCChen avatar Feb 26 '24 18:02 DanielCChen

https://github.com/llvm/llvm-project/pull/83073

klausler avatar Feb 26 '24 22:02 klausler

I have verified PR #83073 has fixed the reproducer and the original test case. Thanks for the quick fix! @klausler

DanielCChen avatar Feb 27 '24 16:02 DanielCChen

Referencing the procptr segfaults as:

  MODULE M

  INTERFACE
   FUNCTION ExtINDEX(Arg1, Arg2, Arg3)
      INTEGER :: ExtINDEX
      CHARACTER(*), INTENT(IN) :: Arg1, Arg2
      LOGICAL, OPTIONAL, INTENT(IN) :: Arg3
    END FUNCTION
  END INTERFACE

  END MODULE

  PROGRAM PtrAssignProcNameIntrin4
  USE M
  IMPLICIT NONE

  integer :: res
  PROCEDURE(ExtINDEX),  POINTER :: PtrINDEX
  PtrINDEX => index
  res = PtrINDEX('FORTRAN', 'R')
  END

Not sure yet if it is a FE bug or codegen.

DanielCChen avatar Feb 27 '24 18:02 DanielCChen

Fortran '77 and Fortran '90 both document the unrestricted specific function INDEX with exactly two arguments. Later editions of the standards omit the argument information from the table of unrestricted specific intrinsic functions. Jean and I believe that the correct interpretation remains the one with two arguments, as we can't get a three-argument form with optional BACK= to work correctly with any other compiler, and that is what I have merged.

klausler avatar Mar 02 '24 00:03 klausler