Allocated array bounds with MOLD and SOURCE on expressions

Gfortran, Intel Fortran, and flang are giving different results for the lower bounds of an array when it is allocated with source or mold. I think gfortran is correct, since parenthesizing an array or multiplying it by 1 creates an expression, and expressions have lower bounds of 1. For the code

program alloc
implicit none
integer, parameter :: n = 2
real :: x(2:2+n-1)
real, allocatable :: y(:)
character (len=*), parameter :: nl = new_line("")
call random_number(x)
print*,"x =",x
print*,"lbound(x),lbound((x)),lbound(1*x)=",lbound(x),lbound((x)),lbound(1*x)
allocate (y(n))
print*,nl,"allocate(y(n)), y =",y ; deallocate (y)
allocate (y,mold=x)
print*,nl,"allocate(y,mold=x) y =",y
print*,"lbound(y)=",lbound(y)
deallocate (y) ; allocate (y,mold=(x))
print*,nl,"allocate(y,mold=(x)) y =",y
print*,"lbound(y)=",lbound(y)
deallocate (y) ; allocate (y,mold=1*x)
print*,nl,"allocate(y,mold=1*x) y =",y
print*,"lbound(y)=",lbound(y)
deallocate (y) ; allocate (y,source=x)
print*,nl,"allocate (y,source=x) y =",y
print*,"lbound(y)=",lbound(y)
deallocate (y) ; allocate (y, source = (x))
print*,nl,"allocate (y,source=(x)) y =",y
print*,"lbound(y)=",lbound(y)
deallocate (y) ; allocate (y, source = 1*x)
print*,nl,"allocate (y,source=1*x) y =",y
print*,"lbound(y)=",lbound(y)
y = [x,1.0]
! allocation on assignment allowed on allocated target
print*,nl,"y = [x,1.0] =",y 
end program alloc

gfortran gives

 x =  0.507174253      0.208649397    
 lbound(x),lbound((x)),lbound(1*x)=           2           1           1
 
allocate(y(n)), y =  -3.74480805E-06   7.41286888E-43
 
allocate(y,mold=x) y =  -3.74481533E-06   7.41286888E-43
 lbound(y)=           2
 
allocate(y,mold=(x)) y =  -3.74481533E-06   7.41286888E-43
 lbound(y)=           1
 
allocate(y,mold=1*x) y =  -3.74481533E-06   7.41286888E-43
 lbound(y)=           1
 
allocate (y,source=x) y =  0.507174253      0.208649397    
 lbound(y)=           2
 
allocate (y,source=(x)) y =  0.507174253      0.208649397    
 lbound(y)=           1
 
allocate (y,source=1*x) y =  0.507174253      0.208649397    
 lbound(y)=           1
 
y = [x,1.0] =  0.507174253      0.208649397       1.00000000

ifort on Windows Version 2021.5.0 Build 20211109_000000 gives

 x =  3.9208680E-07  2.5480442E-02
 lbound(x),lbound((x)),lbound(1*x)=           2           1           1
 
allocate(y(n)), y =  6.3367669E-39  7.0714481E-39
 
allocate(y,mold=x) y =  1.0660544E+27  1.6651776E+25
 lbound(y)=           2
 
allocate(y,mold=(x)) y =  4.0461097E-14  1.8053905E+28
 lbound(y)=           2
 
allocate(y,mold=1*x) y =  3.0097508E+32  7.9316175E+34
 lbound(y)=           1
 
allocate (y,source=x) y =  3.9208680E-07  2.5480442E-02
 lbound(y)=           2
 
allocate (y,source=(x)) y =  3.9208680E-07  2.5480442E-02
 lbound(y)=           2
 
allocate (y,source=1*x) y =  3.9208680E-07  2.5480442E-02
 lbound(y)=           1
 
y = [x,1.0] =  3.9208680E-07  2.5480442E-02   1.000000

flang clang version 7.0.1 gives

     x =   0.9079230       0.1906921
 lbound(x),lbound((x)),lbound(1*x)=            2            2            1

allocate(y(n)), y =    0.000000        0.000000

allocate(y,mold=x) y =    0.000000        0.000000
 lbound(y)=            2

allocate(y,mold=(x)) y =    0.000000        0.000000
 lbound(y)=            2

allocate(y,mold=1*x) y =    0.000000        0.000000
 lbound(y)=            2

allocate (y,source=x) y =   0.9079230       0.1906921
 lbound(y)=            2

allocate (y,source=(x)) y =   0.9079230       0.1906921
 lbound(y)=            2

allocate (y,source=1*x) y =   0.9079230       0.1906921
 lbound(y)=            2

y = [x,1.0] =   0.9079230       0.1906921        1.000000
1 Like

I agree that gfortran has the right answer here. Please report the bug to Intel (in their forum if you don’t have paid support, or at their online service center if you do.