unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: Matt Armstrong <matt@rfc20.org>, 47552@debbugs.gnu.org
Subject: bug#47552: 27.1; cl-defstruct field names matching read-only variables -> bad code
Date: Sun, 18 Jun 2023 15:03:25 -0400	[thread overview]
Message-ID: <jwv5y7kzko2.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87pmz0236v.fsf@gnus.org> (Lars Ingebrigtsen's message of "Sun, 11 Apr 2021 18:31:20 +0200")

> But...  Indeed doing this "doesn't work":
>
> (cl-defsubst foo4 (&key gcs-done)
>   gcs-done)
>
> (foo4 :foo 1)
> -> Debugger entered--Lisp error: (wrong-type-argument numberp nil)

I think the problem is that ELisp function arguments are defined as
being always-statically-scoped, but the macroexpansion of

    (cl-defun foo4 (&key gcs-done)
      gcs-done)

uses `let` rather than `lambda` to bind `gcs-done`, so it ends up being
dynamically-scoped.  Maybe we should introduce something like

    (defmacro slet* (bindings &rest body)
      (named-let expand ((bindings bindings))
        (pcase-exhaustive bindings
          ('() (macroexp-progn body))
          (`((,var ,exp) . ,bindings)
           (let ((rest (expand bindings)))
     	     (if (macroexp--dynamic-variable-p var)
     	         `(funcall (lambda (,var) ,rest) ,exp)
     	       (macroexp-let* `((,var ,exp)) rest)))))))

Except I see that `macroexpand-all` will incorrectly turn the
funcall+lambda into a `let`.
Some wise ass knew about it but did it anyway.  They even wrote
a comment about it:

    ;; In lexical-binding mode, let and functions don't bind vars in the same way
    ;; (let obey special-variable-p, but functions don't).  But luckily, this
    ;; doesn't matter here, because function's behavior is underspecified so it
    ;; can safely be turned into a `let', even though the reverse is not true.

so we need to either fix that `macroexp--unfold-lambda` or circumvent it
by obfuscating the code, e.g.:

    (defmacro slet* (bindings &rest body)
      (named-let expand ((bindings bindings))
        (pcase-exhaustive bindings
          ('() (macroexp-progn body))
          (`((,var ,exp) . ,bindings)
           (let ((rest (expand bindings)))
     	     (if (macroexp--dynamic-variable-p var)
     	         `(funcall (identity (lambda (,var) ,rest)) ,exp)
     	       (macroexp-let* `((,var ,exp)) rest)))))))

Another way to look at it is that maybe we should introduce an
`un-defvar`, such that we can use

    (un-defvar gcs-done)
    (cl-defun foo4 (&key gcs-done)
      gcs-done)

and have `gcs-done` bound statically.


        Stefan






  parent reply	other threads:[~2023-06-18 19:03 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-01 18:38 bug#47552: 27.1; cl-defstruct field names matching read-only variables -> bad code Matt Armstrong
2021-04-04 20:17 ` Lars Ingebrigtsen
2021-04-04 22:59   ` Stefan Monnier
2021-04-11 16:31     ` Lars Ingebrigtsen
2023-06-16  3:48       ` Michael Heerdegen
2023-06-18 19:03       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-06-23 15:37         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-06-24  0:19           ` Michael Heerdegen
2023-06-24 15:45             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-06-25  3:43               ` Michael Heerdegen
2023-06-25  4:03                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-06-25  4:45                   ` Michael Heerdegen

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=jwv5y7kzko2.fsf-monnier+emacs@gnu.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=47552@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    --cc=matt@rfc20.org \
    --cc=monnier@iro.umontreal.ca \
    /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).