[flang] Flang allows array to be allocated twice without deallocation in the middle
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
The following code will compile and run with flang (tested with flang based on llvm 22)
```fortran
program allocate_twice_example
implicit none
integer, allocatable :: arr(:)
! First ALLOCATE statement
allocate(arr(5))
arr = [10, 20, 30, 40, 50]
print *, "First array:"
print *, arr
! Second ALLOCATE statement (invalid: arr is already allocated)
allocate(arr(8))
arr = [100, 200, 300, 400, 500, 600, 700, 800]
print *, "Second array:"
print *, arr
end program allocate_twice_example
```
With most compilers it will compile, but will give a runtime error. For example with lfortran
```
First array:
10 20 30 40 50
runtime error: Attempting to allocate already allocated variable 'arr'
--> double_allocation.f90:15:14
|
15 | allocate(arr(8))
| ^^^^^^ Cannot allocate 'arr' because it is already allocated
```
With gfortran you get
```
First array:
10 20 30 40 50
At line 15 of file double_allocation.f90
Fortran runtime error: Attempting to allocate already allocated variable 'arr'
Error termination. Backtrace:
#0 0x102a7dc47
#1 0x102a7e66f
#2 0x102a7e9c7
#3 0x1027e4843
#4 0x1027e4a57
```
While Flang is happy to output both arrays
```
First array:
10 20 30 40 50
Second array:
100 200 300 400 500 600 700 800
```
Contributor guide
Research direction
Start with the double_allocation.f90 reproducer, especially the second ALLOCATE statement at line 15, and compare Flang's behavior with the runtime errors shown from lfortran and gfortran. Trace the Flang path handling allocation of an already allocated variable and add a regression test for this case. Done means Flang reports the invalid allocation instead of printing the second array.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fortran
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100