]> git.eshelyaron.com Git - emacs.git/commitdiff
Allow `letrec` binding without init expression
authorMattias EngdegÄrd <mattiase@acm.org>
Sat, 4 May 2024 12:09:23 +0000 (14:09 +0200)
committerEshel Yaron <me@eshelyaron.com>
Mon, 6 May 2024 16:39:02 +0000 (18:39 +0200)
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)

lisp/subr.el
test/lisp/subr-tests.el

index 8b222cee48094b525f122ca15cfd28d0a1c6502d..de5d5c7291155b6b0889706a2c2b451191283630 100644 (file)
@@ -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.
index 4e3f743cc93bfd991e4f685c97844cb5250d08f4..119c124f3a5e423d7d6b2bd37a0ff8c77622c54a 100644 (file)
@@ -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)