https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87753
Bug ID: 87753 Summary: READ statement with nested implied do broken with optimization Product: gcc Version: 8.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: hvandam at bnl dot gov Target Milestone: --- Created attachment 44899 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44899&action=edit The test program demonstrating the problem I have code snippet that uses an implied do loop when reading some data from a file. Dependent on the level of optimization the code either reads the data correctly or does not seem to read any data at all. Compiling with the -Wall and -Wextra flags it seems that some of the variables in the implied do loop are mistaken for functions if the optimization is turned on. The code is (file "test.F"): program test implicit none integer, parameter :: mxj = 3 integer :: numj = mxj integer :: numi(mxj) = (/5,3,1/) integer :: anum(5,mxj) integer :: ii, jj anum=0 open(unit=10,file="input.dat",status="old") read(10,*)((anum(ii,jj),ii=1,numi(jj)),jj=1,numj) do ii=1,maxval(numi) write(*,*)(anum(ii,jj),jj=1,numj) enddo close(10) end The data file "input.dat" contains: 11 12 13 14 15 21 22 23 31 Compiling the code and running it gives: $ gfortran -Wall -Wextra test.F $ ./a.out 11 21 31 12 22 0 13 23 0 14 0 0 15 0 0 $ gfortran -Wall -Wextra -O1 test.F test.F:10:0: read(10,*)((anum(ii,jj),ii=1,numi(jj)),jj=1,numj) Warning: ‘jj’ is used uninitialized in this function [-Wuninitialized] $ ./a.out 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The warning makes no sense because jj is neither uninitialized nor used in a function. The resulting code is clearly broken. Higher optimization levels generate the same wrong results (-O2 and -O3). Turning optimization off (with -O0) produces the same correct result as specifying no optimization flags. GFortran version (I tried locating 8.2.1 and 9.0 but could not find those on any of the mirror sites): $ gfortran --version GNU Fortran (GCC) 8.2.0 Copyright (C) 2018 Free Software Foundation, Inc. Operating system: $ uname -a Linux host 2.6.32-220.el6.x86_64 #1 SMP Sat Dec 10 17:04:11 CST 2011 x86_64 x86_64 x86_64 GNU/Linux GCC configure line: $ ./configure --prefix=/home/username --disable-multilib