cl-defun/cl-struct: Use static scoping for function args

* lisp/emacs-lisp/cl-macs.el (cl--slet*): New function.
(cl--transform-lambda): Use it to fix bug#47552.

* test/lisp/emacs-lisp/cl-macs-tests.el (cl-&key-arguments): Add test.
This commit is contained in:
Stefan Monnier 2023-06-23 10:45:49 -04:00
parent 01ce70cea9
commit 37a09a4c00
2 changed files with 27 additions and 5 deletions

View file

@ -243,6 +243,18 @@ The name is made by appending a number to PREFIX, default \"T\"."
(defvar cl--bind-enquote) ;Non-nil if &cl-quote was in the formal arglist!
(defvar cl--bind-lets) (defvar cl--bind-forms)
(defun cl--slet* (bindings body)
"Like `macroexp-let*' but uses static scoping for all the BINDINGS."
(pcase-exhaustive bindings
('() body)
(`((,var ,exp) . ,bindings)
(let ((rest (cl--slet* bindings body)))
(if (macroexp--dynamic-variable-p var)
;; FIXME: We use `identity' to obfuscate the code enough to
;; circumvent the known bug in `macroexp--unfold-lambda' :-(
`(funcall (identity (lambda (,var) ,@(macroexp-unprogn rest))) ,exp)
(macroexp-let* `((,var ,exp)) rest))))))
(defun cl--transform-lambda (form bind-block)
"Transform a function form FORM of name BIND-BLOCK.
BIND-BLOCK is the name of the symbol to which the function will be bound,
@ -337,10 +349,12 @@ FORM is of the form (ARGS . BODY)."
(list '&rest (car (pop cl--bind-lets))))))))
`((,@(nreverse simple-args) ,@rest-args)
,@header
,(macroexp-let* cl--bind-lets
(macroexp-progn
`(,@(nreverse cl--bind-forms)
,@body)))))))
;; Make sure that function arguments are unconditionally statically
;; scoped (bug#47552).
,(cl--slet* cl--bind-lets
(macroexp-progn
`(,@(nreverse cl--bind-forms)
,@body)))))))
;;;###autoload
(defmacro cl-defun (name args &rest body)