From 4d7b5c9a7234f68f2b3a6eff4ef338a104df0384 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Fri, 28 Feb 2025 08:31:56 +0100 Subject: compat-31: Copy {incf,decf} definitions from emacs.git As the new tests show, the previous implementations were not robust if PLACE causes side effects. --- compat-31.el | 6 ++++-- compat-tests.el | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/compat-31.el b/compat-31.el index bb87724..84ff7bf 100644 --- a/compat-31.el +++ b/compat-31.el @@ -52,7 +52,8 @@ The DELTA is first added to PLACE, and then stored in PLACE. Return the incremented value of PLACE. See also `decf'." - `(setf ,place (+ ,place (or ,delta 1)))) + (gv-letplace (getter setter) place + (funcall setter `(+ ,getter ,(or delta 1))))) (compat-defmacro decf (place &optional delta) ;; "Decrement PLACE by DELTA (default to 1). @@ -61,7 +62,8 @@ The DELTA is first subtracted from PLACE, and then stored in PLACE. Return the decremented value of PLACE. See also `incf'." - `(setf ,place (- ,place (or ,delta 1)))) + (gv-letplace (getter setter) place + (funcall setter `(- ,getter ,(or delta 1))))) ;;;; Defined in color.el diff --git a/compat-tests.el b/compat-tests.el index 802b2c8..7c67dc0 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -3357,7 +3357,10 @@ (let ((x 0)) (should-error (eval '(incf x 'symb) t)) (should-error (eval '(incf x [a b c]) t)) - (ignore x))) + (ignore x)) + (let ((vec (vector 1 2 3)) (i 0)) + (incf (aref vec (incf i))) + (should (equal [1 3 3] vec)))) (ert-deftest compat-decf () (let ((x 3)) @@ -3385,7 +3388,10 @@ (let ((x 0)) (should-error (eval '(decf x 'symb) t)) (should-error (eval '(decf x [a b c]) t)) - (ignore x))) + (ignore x)) + (let ((vec (vector 1 2 3)) (i 2)) + (decf (aref vec (decf i))) + (should (equal [1 1 3] vec)))) (ert-deftest compat-color-blend () ;; example from the docstring -- cgit v1.0