Subject: Re: bug#12369: 24.2.50; cl-loop: variable not left unused

From: Stefan Monnier <[email-masked]>
Date: Thu, 06 Sep 2012 11:43:04 -0400

>     ;; -*- lexical-binding: t -*-
>     (require 'cl-lib)
>     (cl-loop for (rms . emacs) in nil)
> Byte compile this piece of code.
>     Warning: Unused lexical variable `rms'

Yes, first bug: `emacs' is not listed as unused.

> Attempting to fix this warning by renaming rms to _rms results in
> another warning.
>     Warning: variable `_rms' not left unused

Yup, second bug.

This is all due to the code generated by cl-loop which was optimized to
use `set' in the loop instead of `let'.  This made sense for dynamically
scoped code where `let' is a bit slower, but for lexically-scoped code,
it's the exact opposite, so we should change the generated code from:

   (identity
    (catch '--cl-block-nil--
      (let* ((--cl-var-- nil)
             (emacs nil) (rms nil))
        (while (consp --cl-var--)
          (setq emacs (car --cl-var--) rms (pop emacs))
          (setq --cl-var-- (cdr --cl-var--)))
        nil)))
to
   (identity
    (catch '--cl-block-nil--
      (let* ((--cl-var-- nil))
        (while (consp --cl-var--)
          (let* ((tmp (car --cl-var--))
                 (emacs (car tmp))
                 (rms (cdr tmp)))
            (setq --cl-var-- (cdr --cl-var--))))
        nil)))

-- Stefan