GNU bug report logs -
#71120
29.3; buglet in cl-loop
Previous Next
Reported by: Philippe Schnoebelen <phs <at> lmf.cnrs.fr>
Date: Wed, 22 May 2024 14:49:02 UTC
Severity: normal
Found in version 29.3
Done: Stefan Kangas <stefankangas <at> gmail.com>
Bug is archived. No further changes may be made.
Full log
Message #14 received at 71120 <at> debbugs.gnu.org (full text, mbox):
Andrea Corallo <acorallo <at> gnu.org> writes:
> Philip Kaludercic <philipk <at> posteo.net> writes:
>
>> Philippe Schnoebelen <phs <at> 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.
>
> This is the same transformation that came to my mind reading the orginal
> report, I think it should be safe.
As a datapoint, SBCL does expands this with inside a
(declare (ignore i))
Unfortuantelly we don't support it :/ and our 'ignore' doesn't work here
as goes in the opposite direction :(
Andrea
This bug report was last modified 78 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.