unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattias.engdegard@gmail.com>
To: Basil Contovounesios <contovob@tcd.ie>
Cc: 64127@debbugs.gnu.org
Subject: bug#64127: 30.0.50; mutate-constant warning with pure function
Date: Sat, 17 Jun 2023 13:58:08 +0200	[thread overview]
Message-ID: <42E68F81-EDE7-45E4-AF08-BF420EE53F4B@gmail.com> (raw)
In-Reply-To: <87ttv6b9zh.fsf@epfl.ch>

17 juni 2023 kl. 13.32 skrev Basil Contovounesios <contovob@tcd.ie>:

> Given a file foo.el that defines a pure function foo:

> (defun foo (x) (declare (pure t)) (list x))

> and another file bar.el that mutates the result of foo:

> (setcar (foo nil) t)

> byte-compiling bar.el emits a mutate-constant warning, even though the
> result of foo is a fresh list:

No, the `pure` declaration means that the function can be evaluated at compile time which the compiler happily does, yielding a constant list, which your code then attempts to modify.

This is why the function `list` itself is not declared `pure` -- while it does look like a pure function when speaking informally, users relies on it returning a freshly allocated list that can be modified and that makes it non-pure. (If lists were immutable, then `list` would naturally be pure.)

Only the mutate-constant warning is new here; previously, the compiler would have let you make this mistake undisturbed.

Thus either you remove the pure-declaration from your function, or you don't mutate what it returns.






  reply	other threads:[~2023-06-17 11:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-17 11:32 bug#64127: 30.0.50; mutate-constant warning with pure function Basil Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-06-17 11:58 ` Mattias Engdegård [this message]
2023-06-17 16:28   ` Basil Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-06-17 17:03     ` Mattias Engdegård
2023-06-17 20:56       ` Basil Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42E68F81-EDE7-45E4-AF08-BF420EE53F4B@gmail.com \
    --to=mattias.engdegard@gmail.com \
    --cc=64127@debbugs.gnu.org \
    --cc=contovob@tcd.ie \
    /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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).