Subject: Re: bug#28280: Maximally unhelpful diagnostic message.

From: [email-masked]
Date: Wed, 30 Aug 2017 08:17:24 -0400

Alan Mackenzie <[email-masked]> writes:

> Yes, but that's a separate problem from the essence of the bug report.
> Regardless of the lack of progress on bug #18913, the information
> provided by an assert being triggered is unhelpful and inadequate.

Here is a possible M-x cl-show-assert command:

>From 835486ccfaaf58476cbb0e097bed3b81adbcfebe Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Wed, 30 Aug 2017 08:12:18 -0400
Subject: [PATCH v1] New command to backtrace from assertion failure
 (Bug#28280)

* lisp/emacs-lisp/cl-lib.el (cl-show-assert): New command.
(cl-assert-last-backtrace): New variable.
* lisp/emacs-lisp/cl-preloaded.el (cl--assertion-failed): Set it.

TODO: keybinding? NEWS.
---
 lisp/emacs-lisp/cl-lib.el       | 15 +++++++++++++++
 lisp/emacs-lisp/cl-preloaded.el | 10 ++++++++--
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/lisp/emacs-lisp/cl-lib.el b/lisp/emacs-lisp/cl-lib.el
index 243622a301..59bbdbca51 100644
--- a/lisp/emacs-lisp/cl-lib.el
+++ b/lisp/emacs-lisp/cl-lib.el
@@ -614,6 +614,21 @@ cl-pairlis
 
 ;;; Miscellaneous.
 
+(defvar cl-assert-last-backtrace nil
+  "Holds a list of frames from the last assertion failure.")
+
+(declare-function debugger-insert-backtrace "debug" (backtrace doxrefs))
+
+(defun cl-show-assert ()
+  "Show backtrace from last assertion failure."
+  (interactive)
+  (unless cl-assert-last-backtrace
+    (user-error "There hasn't been an assertion failure yet"))
+  (require 'debug)
+  (with-current-buffer (get-buffer-create "*Assertion Failure*")
+    (debugger-insert-backtrace cl-assert-last-backtrace t)
+    (display-buffer (current-buffer))))
+
 (provide 'cl-lib)
 (unless (load "cl-loaddefs" 'noerror 'quiet)
   ;; When bootstrapping, cl-loaddefs hasn't been built yet!
diff --git a/lisp/emacs-lisp/cl-preloaded.el b/lisp/emacs-lisp/cl-preloaded.el
index ab6354de7c..d66a4b9726 100644
--- a/lisp/emacs-lisp/cl-preloaded.el
+++ b/lisp/emacs-lisp/cl-preloaded.el
@@ -41,13 +41,19 @@
 
 ;; The `assert' macro from the cl package signals
 ;; `cl-assertion-failed' at runtime so always define it.
-(define-error 'cl-assertion-failed (purecopy "Assertion failed"))
+(define-error 'cl-assertion-failed (purecopy "Assertion failed (\\[cl-show-assert] for backtrace)"))
+
+(defvar cl-assert-last-backtrace)
 
 (defun cl--assertion-failed (form &optional string sargs args)
   (if debug-on-error
       (funcall debugger 'error `(cl-assertion-failed (,form ,string ,@sargs)))
+    (setq cl-assert-last-backtrace (backtrace-frames 'cl--assertion-failed))
+    (autoload 'cl-show-assert "cl-lib"
+      "Show backtrace from last assertion failure." t)
     (if string
-        (apply #'error string (append sargs args))
+        (apply #'error (concat string " (\\[cl-show-assert] for backtrace)")
+               (append sargs args))
       (signal 'cl-assertion-failed `(,form ,@sargs)))))
 
 ;; When we load this (compiled) file during pre-loading, the cl--struct-class
-- 
2.14.1

> That aside, I can't see how the two assertions suggested in #18913 could
> trigger the message I saw.  I'll maybe have a look at the actual patch
> sometime when I'm less tired.

There is a third assertion added in [1: 62ee8b1a59] which is the one you
hit.

[1: 62ee8b1a59]: 2014-10-31 16:57:59 -0400
  * lisp/simple.el (newline): Add assertions to try and help catch bug#18913.
  http://git.savannah.gnu.org/cgit/emacs.git/commit/?id=62ee8b1a59d5343197cb59d9f0a52e07a23af1d8