GNU bug report logs - #42837
26.3; `cl-incf' returns wrong value for (alist-get 'X ALIST 0) when ALIST is nil

Previous Next

Package: emacs;

Reported by: Drew Adams <drew.adams <at> oracle.com>

Date: Wed, 12 Aug 2020 21:59:02 UTC

Severity: normal

Found in version 26.3

Done: Stefan Kangas <stefan <at> marxist.se>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 42837 in the body.
You can then email your comments to 42837 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#42837; Package emacs. (Wed, 12 Aug 2020 21:59:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Drew Adams <drew.adams <at> oracle.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 12 Aug 2020 21:59:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Drew Adams <drew.adams <at> oracle.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 26.3; `cl-incf' returns wrong value for (alist-get 'X ALIST 0) when
 ALIST is nil
Date: Wed, 12 Aug 2020 21:58:30 +0000 (UTC)
`cl-incf' should always return the new value of PLACE, after it is
incremented.  It doesn't, if PLACE is an `alist-get' that returns the
default value.

(setq ali ())

(cl-incf (alist-get 'a ali 0))

returns ((a . 1)), but it should return just 1, the incremented value 
associated with key `a'.

This is the definition of `cl-incf':

(defmacro cl-incf (place &optional x)
  "Increment PLACE by X (1 by default).
PLACE may be a symbol, or any generalized variable allowed by `setf'.
The return value is the incremented value of PLACE."
  (declare (debug (place &optional form)))
  (if (symbolp place)
      (list 'setq place (if x (list '+ place x) (list '1+ place)))
    (list 'cl-callf '+ place (or x 1))))

The definition should be something like this (not generally tested;
might need some fixes, more hygiene, or optimization):

(defmacro cl-incf (place &optional x)
  "Increment PLACE by X (1 by default).
PLACE may be a symbol, or any generalized variable allowed by `setf'.
The return value is the incremented value of PLACE."
  (declare (debug (place &optional form)))
  (if (symbolp place)
      (list 'setq place (if x (list '+ place x) (list '1+ place)))
    `(progn
       (cl-callf + ,place (or ,x 1))
       ,place)))

IOW, after updating the alist entry, `cl-incf' needs to return the new
value for the given key.  `cl-callf' currently returns the entire alist.

See this question from user `grepcake', who uncovered the problem:

https://emacs.stackexchange.com/q/60097/105


In GNU Emacs 26.3 (build 1, x86_64-w64-mingw32)
 of 2019-08-29
Repository revision: 96dd0196c28bc36779584e47fffcca433c9309cd
Windowing system distributor `Microsoft Corp.', version 10.0.18362
Configured using:
 `configure --without-dbus --host=x86_64-w64-mingw32
 --without-compress-install 'CFLAGS=-O2 -static -g3''




Reply sent to Stefan Kangas <stefan <at> marxist.se>:
You have taken responsibility. (Thu, 13 Aug 2020 15:56:02 GMT) Full text and rfc822 format available.

Notification sent to Drew Adams <drew.adams <at> oracle.com>:
bug acknowledged by developer. (Thu, 13 Aug 2020 15:56:03 GMT) Full text and rfc822 format available.

Message #10 received at 42837-done <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefan <at> marxist.se>
To: Drew Adams <drew.adams <at> oracle.com>
Cc: 42837-done <at> debbugs.gnu.org
Subject: Re: bug#42837: 26.3; `cl-incf' returns wrong value for (alist-get 'X
 ALIST 0) when ALIST is nil
Date: Thu, 13 Aug 2020 08:55:25 -0700
Drew Adams <drew.adams <at> oracle.com> writes:

> `cl-incf' should always return the new value of PLACE, after it is
> incremented.  It doesn't, if PLACE is an `alist-get' that returns the
> default value.
>
> (setq ali ())
>
> (cl-incf (alist-get 'a ali 0))
>
> returns ((a . 1)), but it should return just 1, the incremented value
> associated with key `a'.

I can reproduce this on Emacs 26.3 (under emacs -Q).  But it seems to
have been fixed on emacs-27 and current master, where I get:

(progn
   (require 'cl-lib)
   (setq ali ())
   (cl-incf (alist-get 'a ali 0)))

=> 1

I'm therefore closing this bug report.

Best regards,
Stefan Kangas




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 11 Sep 2020 11:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 4 years and 283 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.