[Flang] Flang incorrectly accepts non-pure defined operations in pure procedures
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
This was discovered by @claude.
According to the Fortran 2023 standard, pure procedures must have the *prefix-spec* PURE, and non-pure procedures must not appear in pure procedures:
> A pure procedure is
> * defined by a pure subprogram,
> A pure subprogram is a subprogram that has the *prefix-spec* PURE or the *prefix-spec* SIMPLE, or that has the *prefix-spec* ELEMENTAL and does not have the *prefix-spec* IMPURE.
> C15105 Any procedure referenced in a pure subprogram, including one referenced via a defined operation, defined assignment, defined input/output, or finalization, shall be pure.
```fortran
MODULE pure_op_mod
IMPLICIT NONE
TYPE, PUBLIC :: pv_type
REAL :: x = 0.0, y = 0.0
END TYPE pv_type
INTERFACE OPERATOR(+)
MODULE FUNCTION pv_add(a, b) RESULT(r)
TYPE(pv_type), INTENT(IN) :: a, b
TYPE(pv_type) :: r
END FUNCTION pv_add
END INTERFACE OPERATOR(+)
INTERFACE OPERATOR(*)
MODULE FUNCTION pv_scale(s, v) RESULT(r)
REAL, INTENT(IN) :: s
TYPE(pv_type), INTENT(IN) :: v
TYPE(pv_type) :: r
END FUNCTION pv_scale
END INTERFACE OPERATOR(*)
END MODULE pure_op_mod
SUBMODULE (pure_op_mod) pure_op_smod
IMPLICIT NONE
CONTAINS
MODULE PROCEDURE pv_add
r%x = a%x + b%x; r%y = a%y + b%y
END PROCEDURE pv_add
MODULE PROCEDURE pv_scale
r%x = s * v%x; r%y = s * v%y
END PROCEDURE pv_scale
END SUBMODULE pure_op_smod
PROGRAM main
USE pure_op_mod
IMPLICIT NONE
TYPE(pv_type) :: a, b, m
a = pv_type(1.0, 2.0)
b = pv_type(3.0, 4.0)
m = midpoint(a, b)
IF (m%x == 2.0 .AND. m%y == 3.0) THEN
PRINT *, 'pass'
ELSE
PRINT *, 'FAIL: m=(', m%x, m%y, ')'
END IF
CONTAINS
PURE FUNCTION midpoint(p, q) RESULT(r)
TYPE(pv_type), INTENT(IN) :: p, q
TYPE(pv_type) :: r
! F2018 C1595
r = 0.5 * (p + q)
END FUNCTION midpoint
END PROGRAM main
```
Only Flang accepts this code: https://godbolt.org/z/GPbdKeWrd
Contributor guide
Research direction
Start by compiling the provided pure_op_mod and midpoint reproducer with Flang and compare its behavior with the other compilers referenced by the issue. Trace semantic checking for procedure references through defined operations in pure procedures; done means Flang rejects the reproducer because pv_add and pv_scale are non-pure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fortran
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100