Subject: bug#33890: 27.0.50; Incorrect behaviour of cl-loop macro

From: Quek Yu Han <[email-masked]>
Date: Thu, 27 Dec 2018 04:28:59 -0500

It appears that a change to the `cl-loop` macro in Emacs 27 introduced a
bug where certain clauses would be evaluated more than once in a loop
with a "when" clause.

See the following minimal example which finds the index of an element in
a list:

(cl-loop
 for x in '(a b c d e)
 for index = 0 then (1+ index)
 when (eq x 'e)
 return index)

In Emacs 26.1 this returns `4` as expected, but in Emacs 27.0.50 it
returns `7`, due to the index being incremented twice per loop. The same
occurs after a `emacs -Q` restart.

For reference here is the macroexpanded form of the above expression,
with the difference between 26 and 27 outlined (everything else is
identical):

(cl-block nil
  (let* ((--cl-var-- '(a b c d e))
         (x nil)
         (index nil)
         (--cl-var-- t)
         (--cl-var-- t)
         --cl-var--)
    (while (and (consp --cl-var--)
                (progn
                  (setq x (car --cl-var--))
                  (setq index
                        (if --cl-var-- 0 (1+ index)))
                  (if (eq x 'e)
                      (setq --cl-var-- index
                            --cl-var-- nil)
                    t)))
      (setq --cl-var-- (cdr --cl-var--))

;;;============== ADDED IN EMACS 27 =================
      (and (and (consp --cl-var--)
                (progn
                  (setq x (car --cl-var--))
                  (setq index
                        (if --cl-var-- 0 (1+ index)))
                  (if (eq x 'e)
                      (setq --cl-var-- index
                            --cl-var-- nil)
                    t))))
;;;==================================================

      (setq --cl-var-- nil))
    --cl-var--))

Thanks!

In GNU Emacs 27.0.50 (build 1, x86_64-apple-darwin17.6.0, NS
appkit-1561.40 Version 10.13.5 (Build 17F77))
 of 2018-07-08 built on QYH.local
Repository revision: 98463bed07ecc9057094928ffdc2321eaaf3ab52
Windowing system distributor 'Apple', version 10.3.1671
System Description:  Mac OS X 10.14.2

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.

Configured using:
 'configure --disable-dependency-tracking --disable-silent-rules
 --enable-locallisppath=/usr/local/share/emacs/site-lisp
 --infodir=/usr/local/Cellar/emacs-plus/HEAD-98463be/share/info/emacs
 --prefix=/usr/local/Cellar/emacs-plus/HEAD-98463be --with-xml2
 --without-dbus --with-gnutls --with-imagemagick --with-modules
 --with-rsvg --with-xwidgets --with-ns --disable-ns-self-contained'

Configured features:
RSVG IMAGEMAGICK NOTIFY ACL GNUTLS LIBXML2 ZLIB TOOLKIT_SCROLL_BARS NS
MODULES THREADS XWIDGETS LCMS2

Important settings:
  value of $LC_ALL: en_US.UTF-8
  value of $LC_CTYPE: UTF-8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny seq byte-opt gv
bytecomp byte-compile cconv dired dired-loaddefs format-spec rfc822 mml
easymenu mml-sec password-cache epa derived epg epg-config gnus-util
rmail rmail-loaddefs mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader cl-loaddefs cl-lib sendmail rfc2047
rfc2045 ietf-drums mm-util mail-prsvr mail-utils elec-pair time-date
tooltip eldoc electric uniquify ediff-hook vc-hooks lisp-float-type
mwheel term/ns-win ns-win ucs-normalize mule-util term/common-win
tool-bar dnd fontset image regexp-opt fringe tabulated-list replace
newcomment text-mode elisp-mode lisp-mode prog-mode register page
menu-bar rfn-eshadow isearch timer select scroll-bar mouse jit-lock
font-lock syntax facemenu font-core term/tty-colors frame cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray minibuffer
cl-preloaded nadvice loaddefs button faces cus-face macroexp files
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote xwidget-internal kqueue
cocoa ns lcms2 multi-tty make-network-process emacs)

Memory information:
((conses 16 203880 9574)
 (symbols 48 20001 1)
 (miscs 40 33 114)
 (strings 32 28869 1418)
 (string-bytes 1 768776)
 (vectors 16 35252)
 (vector-slots 8 725502 18860)
 (floats 8 47 69)
 (intervals 56 230 0)
 (buffers 992 11))