llvm / llvm/llvm-project

[Flang] Flang allows array to be specified in both the allocate-list and the SOURCE= specifier

Open
#222,875 1 comment 0 reactions 0 assignees View on GitHub
flang:frontend
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This was discovered by @claude and might be related to #209709.

According to the Fortran 2023 standard, the *source-expr* and *allocate-object* are prohibited from being the same:

> *source-expr* shall not be allocated within the ALLOCATE statement in which it appears; nor shall it depend on the value, bounds, deferred type parameters, allocation status, or association status of any *allocate-object* in that statement.

* test1.f90

```fortran
program main
implicit none
integer, allocatable :: ptr(:)

! ILLEGAL: ptr appears as both the allocatee and the source= expression.
allocate(ptr, source=ptr) ! must not compile

print *, "pass"
end program main
```

* test2.f90

```fortran
program main
implicit none
integer, allocatable :: ptr1(:), ptr2(:)

! ILLEGAL: ptr2 is both an allocatee and an argument in the source= expression.
allocate(ptr1, ptr2, source=identity(ptr2)) ! must not compile

print *, "pass"
contains
function identity(arr) result(res)
integer, allocatable, intent(in) :: arr(:)
integer, allocatable :: res(:)
res = arr
end function identity
end program main
```

I found that GFortran emits a (runtime) error only for test2.f90 and Intel ifx emits an error only for test1.f90. Flang, however, accepts both.

Contributor guide

Open the contributing guide

Research direction

Start by running Flang against the two provided reproducers, test1.f90 and test2.f90, and confirm that both are accepted. Trace the semantic checking for ALLOCATE statements, especially the allocate-list and SOURCE= expression. Done means Flang rejects both invalid programs according to the Fortran 2023 rule, with suitable diagnostic coverage.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.