From: Philip Kaludercic <philipk@posteo.net>
To: Philippe Schnoebelen <phs@lmf.cnrs.fr>
Cc: 71120@debbugs.gnu.org, "Mattias Engdegård" <mattiase@acm.org>,
"Stefan Monnier" <monnier@iro.umontreal.ca>
Subject: bug#71120: 29.3; buglet in cl-loop
Date: Wed, 29 May 2024 20:47:56 +0000 [thread overview]
Message-ID: <87cyp4wdj7.fsf@posteo.net> (raw)
In-Reply-To: <23fd70f8-6e01-45d7-b9f9-d81d53c95ffd@lmf.cnrs.fr> (Philippe Schnoebelen's message of "Wed, 22 May 2024 10:43:36 +0200")
Philippe Schnoebelen <phs@lmf.cnrs.fr> writes:
> When I need a list of 100 random dice throws I write
>
> (cl-loop for i from 1 to 100 collect (random 6))
>
> It compiles just fine.
>
> If instead I use
>
> (cl-loop for _i from 1 to 100 collect (random 6))
>
> then I get a compilation warning:
>
> foo.el:1:18: Warning: variable ‘_i’ not left unused
>
> It should be the other way around.
The issue here is that the warning describes an issue in the output, in
the latter case
(let* ((_i 1) (--cl-var-- nil))
(while (<= _i 100)
(setq --cl-var-- (cons (random 6) --cl-var--))
(setq _i (+ _i 1)))
(nreverse --cl-var--))
As you see, _i is both evaluated in (+ _i 1) and updated.
Here you have a minimal working example of the warning:
(byte-compile (lambda () (let ((_x 3)) _x)))
;; Warning: variable ‘_x’ not left unused
My guess is that fixing this would require cl-loop to notice that the
counter is prefixed with a "_" and then use some other variable, but
that might lead to issues with existing code. Perhaps this
transformation might be safe in that case:
(let* ((<fresh-variable> 1) (--cl-var-- nil))
(while (<= <fresh-variable> 100)
(let ((_i <fresh-variable>))
(setq --cl-var-- (cons (random 6) --cl-var--))
(setq <fresh-variable> (+ <fresh-variable> 1))))
(nreverse --cl-var--))
I have added Mattias and Stefan to the CCs, as they'll probably have
more qualified comments to add.
>
> The variable 'i' is unused in the first form and that deserves a
> warning at compile time. Otherwise the compiler will not help me catch
> the typo in
>
> (cl-loop for i from 0 to 10 do
> (cl-loop for j from 0 to 10 do
> (foo j j))) ;; <<<=== typo, I meant (foo i j)
>
>
> --ph.schnoebelen, happy and thankful GNU Emacs user since 1983 (Thanks
> to all involved!!)
>
>
>
>
>
--
Philip Kaludercic on peregrine
next prev parent reply other threads:[~2024-05-29 20:47 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-22 8:43 bug#71120: 29.3; buglet in cl-loop Philippe Schnoebelen via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-29 20:47 ` Philip Kaludercic [this message]
2024-05-29 21:33 ` Andrea Corallo
2024-05-29 21:49 ` Andrea Corallo
2024-05-30 1:45 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-30 9:25 ` Mattias Engdegård
2024-05-30 13:14 ` Andrea Corallo
2024-05-30 14:51 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-30 15:41 ` Andrea Corallo
2024-05-30 15:45 ` Mattias Engdegård
2024-05-30 17:15 ` Andrea Corallo
2024-05-30 17:18 ` Mattias Engdegård
2024-05-30 17:42 ` Andrea Corallo
2024-05-31 4:30 ` Gerd Möllmann
2024-05-31 8:29 ` Mattias Engdegård
2024-06-03 15:24 ` Andrea Corallo
2024-06-03 15:32 ` Mattias Engdegård
2024-06-03 15:42 ` Andrea Corallo
2024-06-03 16:24 ` Mattias Engdegård
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=87cyp4wdj7.fsf@posteo.net \
--to=philipk@posteo.net \
--cc=71120@debbugs.gnu.org \
--cc=mattiase@acm.org \
--cc=monnier@iro.umontreal.ca \
--cc=phs@lmf.cnrs.fr \
/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.