unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#10146: error in aput in assoc.el, Emacs 24, 23, and 22 (at least)
@ 2011-11-27  5:53 Christopher Genovese
  2011-12-05 22:26 ` Stefan Monnier
  0 siblings, 1 reply; 2+ messages in thread
From: Christopher Genovese @ 2011-11-27  5:53 UTC (permalink / raw)
  To: 10146

[-- Attachment #1: Type: text/plain, Size: 3618 bytes --]

The function aput in assoc.el  (within lisp/emacs-lisp) has two errors.

1. In the third case of the cond (see original below), when the alist is not
    null, the key is already in the list (and thus at the head), and the
value is not null,
    the return value is incorrect.

    The original returns the result of the setcar -- that is, the new entry
-- rather than
    the modified alist as it should.

2. The documentation string indicates that nil is returned when the list is
nil,
    but this is incorrect. The modified list is returned, which will
include the
    new key-value pair.

I have included below, in order: 1) the original function for reference, 2)
a simple
case showing the problem, and 3) a fixed version of the function. Only two
minor changes are required.

(FWIW, I'm running on Mac OS X 10.5, Emacs 24 and 23 nextstep
and Emacs 22 carbon.)


;; Original version (from the current emacs24 trunk)

(defun aput (alist-symbol key &optional value)
  "Inserts a key-value pair into an alist.
The alist is referenced by ALIST-SYMBOL.  The key-value pair is made
from KEY and optionally, VALUE.  Returns the altered alist or nil if
ALIST is nil.

If the key-value pair referenced by KEY can be found in the alist, and
VALUE is supplied non-nil, then the value of KEY will be set to VALUE.
If VALUE is not supplied, or is nil, the key-value pair will not be
modified, but will be moved to the head of the alist.  If the key-value
pair cannot be found in the alist, it will be inserted into the head
of the alist (with value nil if VALUE is nil or not supplied)."
  (lexical-let ((elem (aelement key value))
        alist)
    (asort alist-symbol key)
    (setq alist (symbol-value alist-symbol))
    (cond ((null alist) (set alist-symbol elem))
      ((anot-head-p alist key) (set alist-symbol (nconc elem alist)))
      (value (setcar alist (car elem)))
      (t alist))))

;; Simple case illustrating the problem
(require 'assoc)
(progn
  (setq tmp-alist '((a . 1)
                    (b . 2)
                    (c . 3)
                    (d . 4)))
  (setq tmp-ret1
        (aput 'tmp-alist 'e 5))  ; no problem
  (prin1 tmp-ret1)

  (print "\n")

  (setq tmp-ret2
        (aput 'tmp-alist 'b 10))    ; oops
  (prin1 tmp-ret2))

;; Fixed version of the function
;;
;; Changes:
;;   + The `values' case of the cond needs to return the
;;     alist *not* the value returned by setcar
;;   + The docstring indicating the return value has been
;;     corrected. The original was inaccurate about the
;;     case where the alist is nil.
;;
;; Note this needs (require 'cl) as in assoc.el for lexical-let

(defun aput (alist-symbol key &optional value)
  "Inserts a key-value pair into an alist.
The alist is referenced by ALIST-SYMBOL.  The key-value pair is made
from KEY and optionally, VALUE.  Returns the altered alist.

If the key-value pair referenced by KEY can be found in the alist, and
VALUE is supplied non-nil, then the value of KEY will be set to VALUE.
If VALUE is not supplied, or is nil, the key-value pair will not be
modified, but will be moved to the head of the alist.  If the key-value
pair cannot be found in the alist, it will be inserted into the head
of the alist (with value nil if VALUE is nil or not supplied)."
  (lexical-let ((elem (aelement key value))
        alist)
    (asort alist-symbol key)
    (setq alist (symbol-value alist-symbol))
    (cond ((null alist)
           (set alist-symbol elem))
      ((anot-head-p alist key)
           (set alist-symbol (nconc elem alist)))
      (value
           (setcar alist (car elem))
           alist)
      (t alist))))

[-- Attachment #2: Type: text/html, Size: 4045 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* bug#10146: error in aput in assoc.el, Emacs 24, 23, and 22 (at least)
  2011-11-27  5:53 bug#10146: error in aput in assoc.el, Emacs 24, 23, and 22 (at least) Christopher Genovese
@ 2011-12-05 22:26 ` Stefan Monnier
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Monnier @ 2011-12-05 22:26 UTC (permalink / raw)
  To: Christopher Genovese; +Cc: 10146-done

> The function aput in assoc.el  (within lisp/emacs-lisp) has two errors.

Thanks, installed.
Note that assoc.el is very little used and I'd be happy to deprecate it,
among other things because it does not use a "assoc-" prefix and is
hence not clean w.r.t namespace.  So if you have good reasons to use it,
I'd be happy to hear them,


        Stefan





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-12-05 22:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-27  5:53 bug#10146: error in aput in assoc.el, Emacs 24, 23, and 22 (at least) Christopher Genovese
2011-12-05 22:26 ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).