Subject: [PATCH] Band-aid for edebugging generator bodies (Bug#40434).

From: Philipp Stephani <[email-masked]>
Date: Wed, 10 Jun 2020 19:04:19 +0200

Edebug doesn't support them well.  Rather than trying to fix Edebug,
disable instrumentation for now to prevent annoying bugs.

* lisp/emacs-lisp/generator.el (iter-defun, iter-lambda, iter-make)
(iter-do): Don't attempt to instrument bodies that are mangled by the
CPS transformer.

* test/lisp/emacs-lisp/generator-tests.el
(generator-tests-edebug): New regression test.
---
 lisp/emacs-lisp/generator.el            | 8 ++++----
 test/lisp/emacs-lisp/generator-tests.el | 9 +++++++++
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/lisp/emacs-lisp/generator.el b/lisp/emacs-lisp/generator.el
index ba344eb515..c95c758a57 100644
--- a/lisp/emacs-lisp/generator.el
+++ b/lisp/emacs-lisp/generator.el
@@ -673,7 +673,7 @@ iter-defun
 encapsulates the state of a computation that produces a sequence
 of values.  Callers can retrieve each value using `iter-next'."
   (declare (indent defun)
-           (debug (&define name lambda-list lambda-doc def-body))
+           (debug (&define name lambda-list lambda-doc &rest sexp))
            (doc-string 3))
   (cl-assert lexical-binding)
   (let* ((parsed-body (macroexp-parse-body body))
@@ -687,14 +687,14 @@ iter-lambda
   "Return a lambda generator.
 `iter-lambda' is to `iter-defun' as `lambda' is to `defun'."
   (declare (indent defun)
-           (debug (&define lambda-list lambda-doc def-body)))
+           (debug (&define lambda-list lambda-doc &rest sexp)))
   (cl-assert lexical-binding)
   `(lambda ,arglist
      ,(cps-generate-evaluator body)))
 
 (defmacro iter-make (&rest body)
   "Return a new iterator."
-  (declare (debug t))
+  (declare (debug (&rest sexp)))
   (cps-generate-evaluator body))
 
 (defconst iter-empty (lambda (_op _val) (signal 'iter-end-of-sequence nil))
@@ -720,7 +720,7 @@ iter-do
 Evaluate BODY with VAR bound to each value from ITERATOR.
 Return the value with which ITERATOR finished iteration."
   (declare (indent 1)
-           (debug ((symbolp form) body)))
+           (debug ((symbolp form) &rest sexp)))
   (let ((done-symbol (cps--gensym "iter-do-iterator-done"))
         (condition-symbol (cps--gensym "iter-do-condition"))
         (it-symbol (cps--gensym "iter-do-iterator"))
diff --git a/test/lisp/emacs-lisp/generator-tests.el b/test/lisp/emacs-lisp/generator-tests.el
index bcfab20163..9b1a573ea6 100644
--- a/test/lisp/emacs-lisp/generator-tests.el
+++ b/test/lisp/emacs-lisp/generator-tests.el
@@ -304,4 +304,13 @@ iter-lambda-variable-shadowing
                                             (1+ it)))))))
                  -2)))
 
+(ert-deftest generator-tests-edebug ()
+  "Check that Bug#40434 is fixed."
+  (with-temp-buffer
+    (prin1 '(iter-defun generator-tests-edebug ()
+              (iter-yield 123))
+           (current-buffer))
+    (edebug-defun))
+  (should (eql (iter-next (generator-tests-edebug)) 123)))
+
 ;;; generator-tests.el ends here
-- 
2.27.0.278.ge193c7cf3a9-goog