GNU bug report logs - #63586
29.x: dotimes (possible) problem

Previous Next

Package: emacs;

Reported by: balducci <at> units.it

Date: Fri, 19 May 2023 15:12:02 UTC

Severity: normal

Merged with 63588

Fixed in version 29.1

Done: Stefan Kangas <stefankangas <at> gmail.com>

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 63586 in the body.
You can then email your comments to 63586 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#63586; Package emacs. (Fri, 19 May 2023 15:12:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to balducci <at> units.it:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 19 May 2023 15:12:02 GMT) Full text and rfc822 format available.

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

From: balducci <at> units.it
To: bug-gnu-emacs <at> gnu.org
Subject: 29.x: dotimes (possible) problem
Date: Fri, 19 May 2023 17:10:44 +0200
hello

the dotimes macro behaves differently in 29.x with respect to previous
versions

Basically: changing the value of the loop variable in the body of
dotimes does not seem to have any effect, where for versions <29.x it
used to.

Here is a minimal stretch of dummy code clarifying the problem I'm
reporting.

emacs-29.0.91 (or 29.0.90)
==========================

(dotimes (ii 10)
(insert (format "%2d " ii))
(when (= ii 4)(setq ii 11))
)
==>  0  1  2  3  4  5  6  7  8  9 

emacs-28.2 (or any version <29.x)
=================================

(dotimes (ii 10)
(insert (format "%2d " ii))
(when (= ii 4)(setq ii 11))
)
==>  0  1  2  3  4 

The ability to jump out of the loop by pushing the loop variable over
the upper limit is something that I happen to use in my scripts, so
29.x breaks them somehow

Of course, there are very many other equivalent ways to
accomplish the same result, but I don't see why this one
shouldn't be supported (any longer)

Is the changed behavior intentional? Am I missing some blatant point here?

AFAICS, changing the value of the loop variable from inside the loop
body is supported by any other language which I know about

thank you very much in advance for any hint/feedback

ciao
-gabriele




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63586; Package emacs. (Fri, 19 May 2023 15:55:02 GMT) Full text and rfc822 format available.

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

From: Mattias Engdegård <mattiase <at> acm.org>
To: balducci <at> units.it
Cc: Eli Zaretskii <eliz <at> gnu.org>, Stefan Monnier <monnier <at> iro.umontreal.ca>,
 63586 <at> debbugs.gnu.org
Subject: bug#63586: 29.x: dotimes (possible) problem
Date: Fri, 19 May 2023 17:54:48 +0200
Hello Gabriele,

> changing the value of the loop variable in the body of dotimes does not seem to have any effect, where for versions <29.x it used to.

Prior to Emacs 29, the `dotimes` macro expanded to different code depending on whether lexical-binding was used or not, implicitly permitting your practice of altering the loop counter in code using dynamic binding.

However, the macro was probably never intended to be used that way, and your example does not work with lexical binding in any version as far as I can tell.

If you want to terminate your loop early, use catch/throw or a different loop construct altogether: while, named-let, or cl-loop, just to name a few.

> changing the value of the loop variable from inside the loop
> body is supported by any other language which I know about

Then you should learn more languages!

And you should use lexical-binding:t in all your elisp code. Always.






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63586; Package emacs. (Fri, 19 May 2023 16:01:02 GMT) Full text and rfc822 format available.

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

From: Eli Zaretskii <eliz <at> gnu.org>
To: balducci <at> units.it, Stefan Monnier <monnier <at> iro.umontreal.ca>,
 Mattias Engdegård <mattiase <at> acm.org>
Cc: 63586 <at> debbugs.gnu.org
Subject: Re: bug#63586: 29.x: dotimes (possible) problem
Date: Fri, 19 May 2023 19:00:13 +0300
> From: balducci <at> units.it
> Date: Fri, 19 May 2023 17:10:44 +0200
> 
> the dotimes macro behaves differently in 29.x with respect to previous
> versions
> 
> Basically: changing the value of the loop variable in the body of
> dotimes does not seem to have any effect, where for versions <29.x it
> used to.
> 
> Here is a minimal stretch of dummy code clarifying the problem I'm
> reporting.
> 
> emacs-29.0.91 (or 29.0.90)
> ==========================
> 
> (dotimes (ii 10)
> (insert (format "%2d " ii))
> (when (= ii 4)(setq ii 11))
> )
> ==>  0  1  2  3  4  5  6  7  8  9 
> 
> emacs-28.2 (or any version <29.x)
> =================================
> 
> (dotimes (ii 10)
> (insert (format "%2d " ii))
> (when (= ii 4)(setq ii 11))
> )
> ==>  0  1  2  3  4 

I can only reproduce the behavior you see in 28.2 in Emacs 26.3.  All
the later versions, starting from 27.1, behave like Emacs 29 does.

> The ability to jump out of the loop by pushing the loop variable over
> the upper limit is something that I happen to use in my scripts, so
> 29.x breaks them somehow
> 
> Of course, there are very many other equivalent ways to
> accomplish the same result, but I don't see why this one
> shouldn't be supported (any longer)
> 
> Is the changed behavior intentional? Am I missing some blatant point here?
> 
> AFAICS, changing the value of the loop variable from inside the loop
> body is supported by any other language which I know about

Adding Stefan and Mattias who made changes in dotimes recently.




Merged 63586 63588. Request was from Eli Zaretskii <eliz <at> gnu.org> to control <at> debbugs.gnu.org. (Fri, 19 May 2023 16:08:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63586; Package emacs. (Fri, 19 May 2023 17:30:02 GMT) Full text and rfc822 format available.

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

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: Mattias Engdegård <mattiase <at> acm.org>,
 balducci <at> units.it, 63586 <at> debbugs.gnu.org
Subject: Re: bug#63586: 29.x: dotimes (possible) problem
Date: Fri, 19 May 2023 13:29:14 -0400
>> Basically: changing the value of the loop variable in the body of
>> dotimes does not seem to have any effect,

It does, but only within the current iteration, because the variable is
local to the iteration (each iteration gets a fresh new variable).

>> where for versions <29.x it used to.

Indeed it was changed last year for dynamically scoped ELisp to align it
with the semantics used in the lexically scoped ELisp dialect (and thus
simplify the macro).

>> Here is a minimal stretch of dummy code clarifying the problem I'm
>> reporting.
>>
>> emacs-29.0.91 (or 29.0.90)
>> ==========================
>>
>> (dotimes (ii 10)
>> (insert (format "%2d " ii))
>> (when (= ii 4)(setq ii 11))
>> )
>> ==>  0  1  2  3  4  5  6  7  8  9 
>>
>> emacs-28.2 (or any version <29.x)
>> =================================
>>
>> (dotimes (ii 10)
>> (insert (format "%2d " ii))
>> (when (= ii 4)(setq ii 11))
>> )
>> ==>  0  1  2  3  4 
>
> I can only reproduce the behavior you see in 28.2 in Emacs 26.3.  All
> the later versions, starting from 27.1, behave like Emacs 29 does.

AFAIK what the OP describes is new in Emacs-29 (commit
c6c9dfc8670f5698634a8d5853853056ff928974).
But the change only affects code that has not activated
`lexical-binding`: for `lexical-binding`, the behavior has been with us
"forever" (i.e. since Emacs-24, when `lexical-binding` was introduced).

>> AFAICS, changing the value of the loop variable from inside the loop
>> body is supported by any other language which I know about

It's definitely not supported by Common Lisp, which explicitly allows
both our old implementation and our new implementation (i.e. code like
yours may or may not work depending on the CL implementation).
Several other languages (starting with Pascal :-) either disallow or
discourage modifying the iteration variable(s) inside the loop body.

But in any case, the question is not whether it's a good idea or not: it
used to happen to work and now it doesn't work any more.

Maybe we should mention the change in etc/NEWS?


        Stefan





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63586; Package emacs. (Fri, 19 May 2023 17:34:02 GMT) Full text and rfc822 format available.

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

From: Drew Adams <drew.adams <at> oracle.com>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>, Eli Zaretskii <eliz <at> gnu.org>
Cc: Mattias Engdegård <mattiase <at> acm.org>,
 "balducci <at> units.it" <balducci <at> units.it>,
 "63586 <at> debbugs.gnu.org" <63586 <at> debbugs.gnu.org>
Subject: RE: [External] : bug#63586: 29.x: dotimes (possible) problem
Date: Fri, 19 May 2023 17:33:18 +0000
> But in any case, the question is not whether it's a good idea or not: it
> used to happen to work and now it doesn't work any more.
> 
> Maybe we should mention the change in etc/NEWS?

+1




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63586; Package emacs. (Fri, 19 May 2023 17:48:02 GMT) Full text and rfc822 format available.

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

From: Eli Zaretskii <eliz <at> gnu.org>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: mattiase <at> acm.org, balducci <at> units.it, 63586 <at> debbugs.gnu.org
Subject: Re: bug#63586: 29.x: dotimes (possible) problem
Date: Fri, 19 May 2023 20:47:50 +0300
> From: Stefan Monnier <monnier <at> iro.umontreal.ca>
> Cc: balducci <at> units.it,  Mattias Engdegård
>  <mattiase <at> acm.org>,
>   63586 <at> debbugs.gnu.org
> Date: Fri, 19 May 2023 13:29:14 -0400
> 
> Maybe we should mention the change in etc/NEWS?

Feel free to suggest a NEWS entry, and thanks.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63586; Package emacs. (Sat, 20 May 2023 08:57:01 GMT) Full text and rfc822 format available.

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

From: Mattias Engdegård <mattiase <at> acm.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: balducci <at> units.it, Stefan Monnier <monnier <at> iro.umontreal.ca>,
 63586 <at> debbugs.gnu.org
Subject: Re: bug#63586: 29.x: dotimes (possible) problem
Date: Sat, 20 May 2023 10:56:01 +0200
19 maj 2023 kl. 19.47 skrev Eli Zaretskii <eliz <at> gnu.org>:

> Feel free to suggest a NEWS entry, and thanks.

There is now a NEWS entry in emacs-29. Do modify it as you like.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#63586; Package emacs. (Wed, 06 Sep 2023 20:11:02 GMT) Full text and rfc822 format available.

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

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Mattias Engdegård <mattiase <at> acm.org>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 63588-done <at> debbugs.gnu.org,
 63586 <at> debbugs.gnu.org, balducci <at> units.it,
 Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: Re: bug#63588: 29.x: dotimes (possible) problem
Date: Wed, 6 Sep 2023 13:10:40 -0700
Version: 29.1

Mattias Engdegård <mattiase <at> acm.org> writes:

> 19 maj 2023 kl. 19.47 skrev Eli Zaretskii <eliz <at> gnu.org>:
>
>> Feel free to suggest a NEWS entry, and thanks.
>
> There is now a NEWS entry in emacs-29. Do modify it as you like.

It seems like that was all that needed doing here, so I'm closing this.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 05 Oct 2023 11:24:13 GMT) Full text and rfc822 format available.

This bug report was last modified 203 days ago.

Previous Next


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