GNU bug report logs - #40692
28.0.50; Constant list modified inside function

Previous Next

Package: emacs;

Reported by: Ivan Andrus <darthandrus <at> gmail.com>

Date: Fri, 17 Apr 2020 23:46:02 UTC

Severity: normal

Found in version 28.0.50

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

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 40692 in the body.
You can then email your comments to 40692 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#40692; Package emacs. (Fri, 17 Apr 2020 23:46:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ivan Andrus <darthandrus <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 17 Apr 2020 23:46:02 GMT) Full text and rfc822 format available.

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

From: Ivan Andrus <darthandrus <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 28.0.50; Constant list modified inside  function
Date: Fri, 17 Apr 2020 17:45:12 -0600
Starting from Emacs -Q (I am building off of trunk, but my brother verified the same behavior in 26.3), if I evaluate the following code I get an error the second time I call withdraw.

   (defmacro show (var)
     `(message  ,(format "%S %%S" var) ,var))

   (defun my-test-fun (amount params)
     (when (memq 'tricked-ya params)
       (error "What happened here?"))
     (show amount)
     (show params)
     (setcdr (cdr params) (list 'tricked-ya))
     (show params))

   (defun fun-withdraw (amount)
     (my-test-fun amount
                  `((amount . , amount)
                    (const . some-constant))))

   (fun-withdraw 12)

   (fun-withdraw 12) ;; The second time it's called it will error because the "constant" list was modified.


I believe this is the root cause of a bug in magit/forge https://github.com/magit/forge/issues/267 in which all subsequent pull requests created have the same name.  The maintainer of magit/forge (tarsius) was unable to reproduce that bug, so I tried my hand at creating a minimal test case, and I was able to get it down to this.

Now, I understand reference semantics of lists in general, but it seems like this should be different.  If this behavior is intentional, what's the best way to for creation of a new list every time so that functions using the alist don't have to worry about not changing the list?

-Ivan Andrus


In GNU Emacs 28.0.50 (build 18, x86_64-apple-darwin18.7.0, NS appkit-1671.60 Version 10.14.6 (Build 18G4032))
of 2020-04-10 built on iandrus-macOS
Repository revision: 965390ca5f206c6358014574fe5140ba40850391 -- some local changes on top of e18c24b35a7cf9bb1b91288b706fa448ed28a7c2
Repository branch: master
Windowing system distributor 'Apple', version 10.3.1671
System Description:  Mac OS X 10.14.6

Configured using:
'configure
PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig:/usr/local/opt/libxml2/lib/pkgconfig
--with-sound=yes --with-ns --with-modules --with-file-notification=yes
--enable-gcc-warnings=warn-only --with-xpm --with-jpeg --with-tiff
--with-gif --with-png --with-rsvg --with-xml2 --with-imagemagick
--with-json --with-xft --with-libotf --with-gnutls=no --with-makeinfo
--with-libgmp'

Configured features:
RSVG IMAGEMAGICK GLIB NOTIFY KQUEUE ACL LIBXML2 ZLIB TOOLKIT_SCROLL_BARS
XIM NS MODULES THREADS JSON PDUMPER GMP

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




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#40692; Package emacs. (Sat, 18 Apr 2020 03:06:01 GMT) Full text and rfc822 format available.

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

From: Ivan Andrus <darthandrus <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: Re: 28.0.50; Constant list modified inside  function
Date: Fri, 17 Apr 2020 21:05:31 -0600
On Apr 17, 2020, at 5:45 PM, Ivan Andrus <darthandrus <at> gmail.com> wrote:
> 
> I believe this is the root cause of a bug in magit/forge https://github.com/magit/forge/issues/267 in which all subsequent pull requests created have the same name.  The maintainer of magit/forge (tarsius) was unable to reproduce that bug, so I tried my hand at creating a minimal test case, and I was able to get it down to this.
> 
> Now, I understand reference semantics of lists in general, but it seems like this should be different.  If this behavior is intentional, what's the best way to for creation of a new list every time so that functions using the alist don't have to worry about not changing the list?

I tracked down the function doing the actual changing: json-encode-alist.  I filed https://debbugs.gnu.org/cgi/bugreport.cgi?bug=40693   

I still find the behavior here confusing, but I expect it will be considered expected behavior.

-Ivan



Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#40692; Package emacs. (Sat, 18 Apr 2020 09:19:02 GMT) Full text and rfc822 format available.

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

From: Štěpán Němec <stepnem <at> gmail.com>
To: Ivan Andrus <darthandrus <at> gmail.com>
Cc: 40692 <at> debbugs.gnu.org
Subject: Re: bug#40692: 28.0.50; Constant list modified inside  function
Date: Sat, 18 Apr 2020 11:18:46 +0200
On Fri, 17 Apr 2020 17:45:12 -0600
Ivan Andrus wrote:

> Starting from Emacs -Q (I am building off of trunk, but my brother verified the same behavior in 26.3), if I evaluate the following code I get an error the second time I call withdraw.
>
>    (defmacro show (var)
>      `(message  ,(format "%S %%S" var) ,var))
>
>    (defun my-test-fun (amount params)
>      (when (memq 'tricked-ya params)
>        (error "What happened here?"))
>      (show amount)
>      (show params)
>      (setcdr (cdr params) (list 'tricked-ya))
>      (show params))
>
>    (defun fun-withdraw (amount)
>      (my-test-fun amount
>                   `((amount . , amount)
>                     (const . some-constant))))
>
>    (fun-withdraw 12)
>
>    (fun-withdraw 12) ;; The second time it's called it will error because the "constant" list was modified.
>
>
> I believe this is the root cause of a bug in magit/forge https://github.com/magit/forge/issues/267 in which all subsequent pull requests created have the same name.  The maintainer of magit/forge (tarsius) was unable to reproduce that bug, so I tried my hand at creating a minimal test case, and I was able to get it down to this.
>
> Now, I understand reference semantics of lists in general, but it
> seems like this should be different. If this behavior is intentional,

I think it is, although I admit I was confused by it, too, as I've
somehow come to believe that e.g. `(list) macroexpands to (list 'list),
but that's not the case: it expands to '(list).

> what's the best way to for creation of a new list every time so that
> functions using the alist don't have to worry about not changing the
> list?

If you change the backquoted form in `fun-withdraw' to 

 (list `(amount . ,amount)
       '(const . some-constant))

it works as desired.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#40692; Package emacs. (Sat, 18 Apr 2020 22:50:01 GMT) Full text and rfc822 format available.

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

From: Michael Heerdegen <michael_heerdegen <at> web.de>
To: Štěpán Němec <stepnem <at> gmail.com>
Cc: Ivan Andrus <darthandrus <at> gmail.com>, 40692 <at> debbugs.gnu.org
Subject: Re: bug#40692: 28.0.50; Constant list modified inside  function
Date: Sun, 19 Apr 2020 00:49:02 +0200
Štěpán Němec <stepnem <at> gmail.com> writes:

> > Now, I understand reference semantics of lists in general, but it
> > seems like this should be different. If this behavior is intentional,
>
> I think it is, although I admit I was confused by it, too, as I've
> somehow come to believe that e.g. `(list) macroexpands to (list 'list),
> but that's not the case: it expands to '(list).

Yes, I had been bitten by this as well some time ago.  I don't find the
discussion anymore, I had asked somewhere and the answer was that it's
an intended feature of backquote to produce an expansion like that.

Michael.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#40692; Package emacs. (Sun, 19 Apr 2020 07:09:01 GMT) Full text and rfc822 format available.

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

From: Štěpán Němec <stepnem <at> gmail.com>
To: Michael Heerdegen <michael_heerdegen <at> web.de>
Cc: Ivan Andrus <darthandrus <at> gmail.com>, 40692 <at> debbugs.gnu.org
Subject: Re: bug#40692: 28.0.50; Constant list modified inside  function
Date: Sun, 19 Apr 2020 09:08:37 +0200
On Sun, 19 Apr 2020 00:49:02 +0200
Michael Heerdegen wrote:

> Štěpán Němec <stepnem <at> gmail.com> writes:
>
>> > Now, I understand reference semantics of lists in general, but it
>> > seems like this should be different. If this behavior is intentional,
>>
>> I think it is, although I admit I was confused by it, too, as I've
>> somehow come to believe that e.g. `(list) macroexpands to (list 'list),
>> but that's not the case: it expands to '(list).
>
> Yes, I had been bitten by this as well some time ago.  I don't find the
> discussion anymore, I had asked somewhere and the answer was that it's
> an intended feature of backquote to produce an expansion like that.

The "optimization" in absence of unquoted terms seems reasonable, and
e.g. SBCL behaves the same.

I find http://www.lispworks.com/documentation/HyperSpec/Body/02_df.htm
somewhat ambiguous, but http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html
is quite clear:

"Semantics: If no unquote or unquote-splicing forms appear within the <qq
template>, the result of evaluating (quasiquote <qq template>) is
equivalent to the result of evaluating (quote <qq template>)."

Also:

"A quasiquote expression may return either fresh, mutable objects or
literal structure for any structure that is constructed at run time
during the evaluation of the expression. Portions that do not need to be
rebuilt are always literal."

-- 
Štěpán




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#40692; Package emacs. (Sun, 29 Aug 2021 22:19:01 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Štěpán Němec <stepnem <at> gmail.com>
Cc: Michael Heerdegen <michael_heerdegen <at> web.de>,
 Ivan Andrus <darthandrus <at> gmail.com>, 40692 <at> debbugs.gnu.org
Subject: Re: bug#40692: 28.0.50; Constant list modified inside  function
Date: Mon, 30 Aug 2021 00:18:07 +0200
Štěpán Němec <stepnem <at> gmail.com> writes:

>> Yes, I had been bitten by this as well some time ago.  I don't find the
>> discussion anymore, I had asked somewhere and the answer was that it's
>> an intended feature of backquote to produce an expansion like that.
>
> The "optimization" in absence of unquoted terms seems reasonable, and
> e.g. SBCL behaves the same.

So it seems like this behaves as intended, and I'm closing this bug report.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




bug closed, send any further explanations to 40692 <at> debbugs.gnu.org and Ivan Andrus <darthandrus <at> gmail.com> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sun, 29 Aug 2021 22:19:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Mon, 27 Sep 2021 11:24:08 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 205 days ago.

Previous Next


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