From: Mattias EngdegÄrd Date: Sat, 4 May 2024 12:09:23 +0000 (+0200) Subject: Allow `letrec` binding without init expression X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=c1d379d41a99aa61768536f6a5bd0b97d8519ce1;p=emacs.git Allow `letrec` binding without init expression For example, (letrec (... (x) ...) ...) is now allowed. * lisp/subr.el (letrec): Allow omitted init expression. * test/lisp/subr-tests.el (subr--tests-letrec): Add test case. (cherry picked from commit fd859fbea2e9d13e76db1c5295d9ddd1c5955d83) --- diff --git a/lisp/subr.el b/lisp/subr.el index 8b222cee480..de5d5c72911 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -2282,7 +2282,9 @@ all symbols are bound before any of the VALUEFORMs are evalled." (let ((nbody (if (null binders) (macroexp-progn body) `(let ,(mapcar #'car binders) - ,@(mapcar (lambda (binder) `(setq ,@binder)) binders) + ,@(mapcan (lambda (binder) + (and (cdr binder) (list `(setq ,@binder)))) + binders) ,@body)))) (cond ;; All bindings are recursive. diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index 4e3f743cc93..119c124f3a5 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -744,7 +744,14 @@ See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19350." (+ subr-tests-var1 subr-tests-var2))) '(let* ((subr-tests-var1 1) (subr-tests-var2 subr-tests-var1)) - (+ subr-tests-var1 subr-tests-var2))))) + (+ subr-tests-var1 subr-tests-var2)))) + ;; Check that the init expression can be omitted, as in `let'/`let*'. + (should (equal (letrec ((a (lambda () (funcall c))) + (b) + (c (lambda () b))) + (setq b 'ok) + (funcall a)) + 'ok))) (defvar subr-tests--hook nil)