Subject: bug#47588: 28.0.50; C-M-x in emacs-lisp-mode: elisp--eval-defun: Symbol’s function definition is void: nil [3 times]

From: Gregory Heytings <[email-masked]>
Date: Sat, 17 Apr 2021 23:48:53 +0000

>
> 1. emacs -Q
>
> 2. Go to *scratch* (but it is happening anywhere)
>
> 2. Evaluate (number-to-string nil) and get error, do not press Q in 
> *Backtrace*, switch back to *scratch*
>
> 3. Try evaluating any defun with C-M-x it will give error: 
> elisp--eval-defun: Symbol’s function definition is void: nil
>
> 4. You can evaluate with C-x C-e on the end of the defun, but not in the 
> middle with C-M-x
>
> 5. Go to *Backtrace*, click q or erase *Backtrace*, come back and it 
> works again.
>

IIUC, this is a kind chicken-and-egg problem: eval-defun at that point 
does not work without a (require 'edebug), which executes 
(edebug-install-read-eval-functions), which puts an advice around 
eval-defun...

Here's a patch, which I'm sure is not TRT, but which at least fixes that 
bug.  Cc'ing Stefan, who wrote bae2cfe63c, who might have a better idea on 
how this should be done.

From d17d17f39e9f1b85d8d19fd76827e238bb15a1ec Mon Sep 17 00:00:00 2001
From: Gregory Heytings <[email-masked]>
Date: Sat, 17 Apr 2021 23:34:34 +0000
Subject: [PATCH] Load edebug before actually calling eval-defun

* lisp/progmodes/elisp-mode.el (do-eval-defun): New function.
(emacs-lisp-mode-map): Use it.
(eval-defun): Do not load edebug anymore.s
---
 lisp/progmodes/elisp-mode.el | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el
index 203712f45d..d3b7adc884 100644
--- a/lisp/progmodes/elisp-mode.el
+++ b/lisp/progmodes/elisp-mode.el
@@ -49,7 +49,7 @@ emacs-lisp-mode-map
   (let ((map (make-sparse-keymap)))
     (set-keymap-parent map lisp-mode-shared-map)
     (define-key map "\e\t" 'completion-at-point)
-    (define-key map "\e\C-x" 'eval-defun)
+    (define-key map "\e\C-x" 'do-eval-defun)
     (define-key map "\e\C-q" 'indent-pp-sexp)
     map)
   "Keymap for Emacs Lisp mode.
@@ -1366,6 +1366,11 @@ elisp--eval-defun
       (if str (princ str)))
     elisp--eval-defun-result))
 
+(defun do-eval-defun (edebug-it)
+  (interactive "P")
+  (require 'edebug)
+  (call-interactively 'eval-defun edebug-it))
+
 (defun eval-defun (edebug-it)
   "Evaluate the top-level form containing point, or after point.
 
@@ -1393,7 +1398,6 @@ eval-defun
 which see."
   (interactive "P")
   (cond (edebug-it
-	 (require 'edebug)
 	 (defvar edebug-all-defs)
 	 (eval-defun (not edebug-all-defs)))
 	(t
-- 
2.30.2