From: Okamsn via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: 70524@debbugs.gnu.org
Subject: bug#70524: [PATCH] Fix `map-elt` with `setf` for subplaces
Date: Tue, 23 Apr 2024 02:10:42 +0000 [thread overview]
Message-ID: <cec20603-30c3-4db6-a96b-9d8b60e80f1c@protonmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1451 bytes --]
Hello,
Currently, the use
(let ((arr (vector 0 1 2 3 4 5 6)))
(setf (map-elt (cl-subseq arr 3) 0)
27)
arr)
expands to
(let ((arr (vector 0 1 2 3 4 5 6)))
(let* ((v arr))
(condition-case nil
(with-no-warnings
(map-put! (cl-subseq v 3) 0 27 nil))
(map-not-inplace
(let* ((new (map-insert (cl-subseq v 3) 0 27)))
(progn
(cl-replace v new :start1 3 :end1 nil)
new))
27)))
arr)
which does not modify the original variable `arr` due to how `map-put!`
is being used. With the attached patch, it would expand to
(let ((arr (vector 0 1 2 3 4 5 6)))
(let* ((v arr))
(condition-case nil
(with-no-warnings
(let* ((m (cl-subseq v 3)))
(progn
(map-put! m 0 27 nil)
(let* ((new m))
(progn
(cl-replace v new :start1 3 :end1 nil)
new))
27)))
(map-not-inplace
(let* ((new (map-insert (cl-subseq v 3) 0 27)))
(progn
(cl-replace v new :start1 3 :end1 nil)
new))
27)))
arr)
which correctly sets the value using `cl-replace` as the setter for
`cl-subseq`.
Thank you.
[-- Attachment #2: 0001-Make-setf-with-map-elt-work-with-subplaces.patch --]
[-- Type: text/x-patch, Size: 2490 bytes --]
From 1ac65858dc2f241975df8620ac79c973fcc6dad4 Mon Sep 17 00:00:00 2001
From: Earl Hyatt <okamsn@protonmail.com>
Date: Mon, 22 Apr 2024 21:45:03 -0400
Subject: [PATCH] Make setf with map-elt work with subplaces.
* lisp/emacs-lisp/map.el (map-elt): Using the setting function defined
by 'gv-letplace' to make sure that we capture the modification, even
when using 'map-put!'. Otherwise, the sub-place might be understood as
a normal function call that will create the value to modify instead.
* test/lisp/emacs-lisp/map-tests.el (test-setf-map-with-function): Test
that sub-places work with 'map-elt' as expected. For example, the use
of 'substring' should be understood as wanting to modify an existing
string, not wanting to modify the new string that would be created by a
call to 'substring'.
---
lisp/emacs-lisp/map.el | 6 +++++-
test/lisp/emacs-lisp/map-tests.el | 5 +++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index d3d71a36ee4..facfdd8de7b 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -167,7 +167,11 @@ map-elt
`(condition-case nil
;; Silence warnings about the hidden 4th arg.
(with-no-warnings
- (map-put! ,mgetter ,key ,v ,testfn))
+ ,(macroexp-let2 nil m mgetter
+ `(progn
+ (map-put! ,m ,key ,v ,testfn)
+ ,(funcall msetter m)
+ ,v)))
(map-not-inplace
,(funcall msetter
`(map-insert ,mgetter ,key ,v))
diff --git a/test/lisp/emacs-lisp/map-tests.el b/test/lisp/emacs-lisp/map-tests.el
index dc8121b0582..c79bac54cb3 100644
--- a/test/lisp/emacs-lisp/map-tests.el
+++ b/test/lisp/emacs-lisp/map-tests.el
@@ -723,6 +723,11 @@ test-setf-map-with-function
;; Check that the function is only called once.
(should (= num 1))))
+(ert-deftest test-setf-map-with-subplace ()
+ (let ((arr (string ?0 ?1 ?2 ?3 ?4 ?5 ?6)))
+ (setf (map-elt (substring arr 3) 0) ?x)
+ (should (equal arr (string ?0 ?1 ?2 ?x ?4 ?5 ?6)))))
+
(ert-deftest test-map-plist-member ()
"Test `map--plist-member' and `map--plist-member-1'."
(dolist (mem '(map--plist-member map--plist-member-1))
--
2.34.1
next reply other threads:[~2024-04-23 2:10 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-23 2:10 Okamsn via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-04-24 6:06 ` bug#70524: [PATCH] Fix `map-elt` with `setf` for subplaces Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-24 20:14 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-25 1:59 ` okamsn--- via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-25 12:49 ` Augusto Stoffel
2024-04-26 12:19 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-29 1:08 ` okamsn--- via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-29 1:54 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-30 16:17 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-25 12:03 ` Augusto Stoffel
2024-04-25 12:42 ` Augusto Stoffel
2024-05-06 14:02 ` Michael Heerdegen via Bug reports for GNU Emacs, the Swiss army knife of text editors
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cec20603-30c3-4db6-a96b-9d8b60e80f1c@protonmail.com \
--to=bug-gnu-emacs@gnu.org \
--cc=70524@debbugs.gnu.org \
--cc=okamsn@protonmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.