Here is a simple example program test do 10 i=1,3 if(i == 2) goto 10 write(*,*) ' loop with i =',i 10 enddo end where the loop stops at the second pass (i=2). Replacing the "goto" by "cycle" cures the problem (but this breaks compatibility with old g77 codes) program test do 10 i=1,3 if(i == 2) cycle write(*,*) ' loop with i =',i 10 enddo end I've used the gfortran version as of today (28 sep), on gcc 3.2.2
Confirmed.
t02.original looks like this: MAIN__ () { int4 i; { int4 count.0; count.0 = 3; i = 1; while (1) { if (count.0 <= 0) { goto L.2; } else { (void) 0; } _gfortran_filename = "pr17708.f90"; _gfortran_line = 4; _gfortran_ioparm.unit = 6; _gfortran_ioparm.list_format = 1; _gfortran_st_write (); _gfortran_transfer_character (" loop with i =", 14); _gfortran_transfer_integer (&i, 4); _gfortran_st_write_done (); i = i + 1; count.0 = count.0 - 1; } L.2:; } __label_000010:; <<<<<<<<<<<<<<<<<<<< this is the wrong place }
The bug is actually in the frontend. From the -fdump-parse-tree output: DO i=1 3 1 IF (= i 2) GOTO 10 ENDIF WRITE UNIT=6 FMT=-1 TRANSFER ' loop with i =' TRANSFER i DT_END END DO 10 NOP The last two lines should clearly be interchanged.
Patch here: http://gcc.gnu.org/ml/fortran/2004-09/msg00277.html
Subject: Bug 17708 CVSROOT: /cvs/gcc Module name: gcc Changes by: tobi@gcc.gnu.org 2004-10-04 21:05:08 Modified files: gcc/fortran : ChangeLog parse.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/gfortran.dg: pr17708.f90 Log message: fortran/ PR fortran/17708 * parse.c (accept_statement): Don't treat END DO like END IF and END SELECT. (parse_do_block): Generate possible END DO label inside END DO block. also, added ChangeLog entry for previous commit. testsuite/ PR fortran/17708 * gfortran.dg/pr17708.f90: New test. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/ChangeLog.diff?cvsroot=gcc&r1=1.224&r2=1.225 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/parse.c.diff?cvsroot=gcc&r1=1.19&r2=1.20 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4384&r2=1.4385 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/pr17708.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1
Fixed.