Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: For loop bug



Philippe Troin wrote:
> Tried on zsh 4.0.4 and 4.0.6:
> 
>   % for i in 1 2 3; do echo $i || break; done && echo X
>   1
>   X
>   2
>   X
>   3
>   X
>   X
>   % 
> 
> That looks somewhat incorrect...

Oh, *yuk*.

> Or did I miss something?

Non, hélas.

The wordcode is pretty horrific to debug: it's uncommented and as an
unstructured array of integers completely opaque to all debugging tools.
Tentative patch below --- passes all tests, including the new one (which
Philippe will recognise).  It's actually against 4.1, but I doubt if
much has changed here.

I may leave this till Monday for people to scratch their heads over.

Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.45
diff -u -r1.45 exec.c
--- Src/exec.c	5 Aug 2002 12:35:59 -0000	1.45
+++ Src/exec.c	13 Sep 2002 23:18:57 -0000
@@ -877,8 +877,15 @@
 			 * for this sublist.                                   */
 			donetrap = 1;
 			goto sublist_done;
-		    } else if (WC_SUBLIST_TYPE(code) == WC_SUBLIST_END)
+		    } else if (WC_SUBLIST_TYPE(code) == WC_SUBLIST_END) {
+			/*
+			 * Treat this in the same way as if we reached
+			 * the end of the sublist normally.
+			 */
 			donetrap = 1;
+			state->pc = next;
+			goto sublist_done;
+		    }
 		}
 		cmdpush(CS_CMDOR);
 		break;
Index: Test/A01grammar.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/A01grammar.ztst,v
retrieving revision 1.5
diff -u -r1.5 A01grammar.ztst
--- Test/A01grammar.ztst	22 Aug 2001 15:59:27 -0000	1.5
+++ Test/A01grammar.ztst	13 Sep 2002 23:18:58 -0000
@@ -319,3 +319,14 @@
   done < /dev/null | { read name; print done }
 0:Bug regression: `while' loop with redirection and pipeline
 >done
+
+# This used to be buggy and print X at the end of each loop.
+  for f in 1 2 3 4; do
+    print $f || break
+  done && print X
+0:Handling of ||'s and &&'s with a for loop in between
+>1
+>2
+>3
+>4
+>X

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxxxxxxxxx>
Work: pws@xxxxxxx
Web: http://www.pwstephenson.fsnet.co.uk



Messages sorted by: Reverse Date, Date, Thread, Author