Fix later-bindings-win logic in with-fluids.
authorMark H Weaver <mhw@netris.org>
Thu, 28 Feb 2013 23:43:09 +0000 (18:43 -0500)
committerMark H Weaver <mhw@netris.org>
Thu, 28 Feb 2013 23:43:09 +0000 (18:43 -0500)
Based on a patch by David Kastrup <dak@gnu.org>.
Fixes <http://bugs.gnu.org/13843>.

* libguile/fluids.c (scm_i_make_with_fluids): Reverse direction of inner
  loop that checks for duplicates, to properly handle more than two
  bindings to the same fluid.

libguile/fluids.c
test-suite/tests/fluids.test

index 277246e..327d12f 100644 (file)
@@ -319,10 +319,10 @@ scm_i_make_with_fluids (size_t n, SCM *fluids, SCM *vals)
   /* Ensure that there are no duplicates in the fluids set -- an N^2 operation,
      but N will usually be small, so perhaps that's OK. */
   {
-    size_t i, j = n;
+    size_t i, j;
 
-    while (j--)
-      for (i = 0; i < j; i++)
+    for (j = n; j--;)
+      for (i = j; i--;)
         if (scm_is_eq (fluids[i], fluids[j]))
           {
             vals[i] = vals[j]; /* later bindings win */
index 5552fd9..9ad9e81 100644 (file)
 
   (pass-if "last value wins"
     (compile '(with-fluids ((a 1)
-                            (a 2))
-                (eqv? (fluid-ref a) 2))
+                            (a 2)
+                            (a 3))
+                (eqv? (fluid-ref a) 3))
              #:env (current-module)))
   
   (pass-if "remove the duplicate, not the last binding"
     (compile '(with-fluids ((a 1)
                             (a 2)
-                            (b 3))
-                (eqv? (fluid-ref b) 3))
+                            (a 3)
+                            (b 4))
+                (eqv? (fluid-ref b) 4))
              #:env (current-module)))
 
   (pass-if "original value restored"