Subject: bug#67196: M-: uses a wrong value of debug-on-error when it is nil.

From: Stefan Monnier <[email-masked]>
Date: Sat, 16 Dec 2023 23:23:31 -0500

> Side note: there's recently been several things pointing to the need to
> add something like Common Lisp's `handler-bind`.

BTW, I now have working code implementing `handler-bind` (see bug#67862
for a preview of the code).  With it I can fix this bug with the
patch below (well, plus a tweak in `debug.el` plus some docstring
adjustements).

        Stefan

diff --git a/lisp/simple.el b/lisp/simple.el
index cee1ddac52f..da07a249081 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -2120,23 +2120,17 @@ eval-expression
    (cons (read--expression "Eval: ")
          (eval-expression-get-print-arguments current-prefix-arg)))
 
-  (let (result)
+  (let* (result
+         (runfun
+          (lambda ()
+            (setq result
+                  (values--store-value
+                   (eval (let ((lexical-binding t)) (macroexpand-all exp))
+                         t))))))
     (if (null eval-expression-debug-on-error)
-        (setq result
-              (values--store-value
-               (eval (let ((lexical-binding t)) (macroexpand-all exp)) t)))
-      (let ((old-value (make-symbol "t")) new-value)
-        ;; Bind debug-on-error to something unique so that we can
-        ;; detect when evalled code changes it.
-        (let ((debug-on-error old-value))
-          (setq result
-	        (values--store-value
-                 (eval (let ((lexical-binding t)) (macroexpand-all exp)) t)))
-	  (setq new-value debug-on-error))
-        ;; If evalled code has changed the value of debug-on-error,
-        ;; propagate that change to the global binding.
-        (unless (eq old-value new-value)
-	  (setq debug-on-error new-value))))
+        (funcall runfun)
+      (handler-bind ((error debugger))
+        (funcall runfun)))
 
     (let ((print-length (unless no-truncate eval-expression-print-length))
           (print-level  (unless no-truncate eval-expression-print-level))