https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88710

            Bug ID: 88710
           Summary: [F08] Sourced allocation of array fails, yielding
                    wrong bounds and result
           Product: gcc
           Version: 8.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: c...@mnet-mail.de
  Target Milestone: ---

The following code shows that sourced allocation of an allocatable
array with gfortran 8.1.0 leads to wrong lower and upper bounds
that do not correspond to those of the source expression. 

Moreover, the initialized array therefore does not yield the correct
result expected from the value of the source expression.

$ cat test_alloc.F90 
program test_alloc

   implicit none

   integer(4) :: i, j, k
   real(8), dimension(:,:,:), allocatable :: a, b, c, t

   allocate( a(-1:2,-1:1,1:1) )
   allocate( b(-1:2,-1:1,1:1) )
   allocate( c(-1:2,-1:1,1:1) )

   a = 1.d0
   b = 2.d0
   c = 0.d0

   allocate(t, source = (a + (c - b)) )

   write(*,'(a,6(i5,1x))') 'lbound/ubound(a): ', lbound(a),  ubound(a)
   write(*,'(a,6(i5,1x))') 'lbound/ubound(b): ', lbound(b),  ubound(b)
   write(*,'(a,6(i5,1x))') 'lbound/ubound(c): ', lbound(c),  ubound(c)
   write(*,'(a,6(i5,1x))') 'lbound/ubound(t): ', lbound(t),  ubound(t)

   write(*,*) 'a, b, c, t: '
   do k = lbound(a,3), ubound(a,3)
      do j = lbound(a,2), ubound(a,2)
         do i = lbound(a,1), ubound(a,1)
            write(*,'(1p,4(e23.16,1x))') &
                 &   a(i,j,k), b(i,j,k), c(i,j,k), t(i,j,k)
         end do
      end do
   end do

end program test_alloc


Running this code with gfortran 8.1.0 gives the following output.
$ gfortran-8 test_alloc.F90 -o test.gfort; ./test.gfort 
lbound/ubound(a):    -1    -1     1     2     1     1
lbound/ubound(b):    -1    -1     1     2     1     1
lbound/ubound(c):    -1    -1     1     2     1     1
lbound/ubound(t):     0     0     0     3     2     0
 a, b, c, t: 
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00 
0.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00 
1.6304166312761136-322
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00 
1.0023829485142537E-95
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00 
3.4119363283543871-315
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00 
0.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00 
2.0716172530123468-320
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00 
9.6317959318370178-317


Both flang 6.0 and pgfortran 18.4-0 yield the following (correct) output 
(notice the different bounds for t, and its values printed in the last column):
$ flang test_alloc.F90 -o test.flang; ./test.flang
lbound/ubound(a):    -1    -1     1     2     1     1
lbound/ubound(b):    -1    -1     1     2     1     1
lbound/ubound(c):    -1    -1     1     2     1     1
lbound/ubound(t):    -1    -1     1     2     1     1
 a, b, c, t: 
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00
 1.0000000000000000E+00  2.0000000000000000E+00  0.0000000000000000E+00
-1.0000000000000000E+00

Gfortran version used is:
$ gfortran-8 -v
Using built-in specs.
COLLECT_GCC=gfortran-8
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
8.1.0-5ubuntu1~16.04' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr
--with-gcc-major-version-only --program-suffix=-8
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib
--with-target-system-zlib --enable-objc-gc=auto --enable-multiarch
--disable-werror --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 8.1.0 (Ubuntu 8.1.0-5ubuntu1~16.04)

Reply via email to