Subject: bug#31090: 26.0.91; Edebug incorrectly instruments unquotes in nested backquotes

From: Gemini Lasswell <[email-masked]>
Date: Sat, 07 Apr 2018 16:44:59 -0700

Edebug incorrectly instruments unquotes inside of nested backquotes,
causing errors if the incorrectly instrumented forms are part of a
macro expansion that then gets executed in another context.

To reproduce, enter this code into *scratch*:

(defun my-wrap-form (form description)
  `(unless ,form
     (message "failed %s" ,description)))

(defmacro my-should (form)
  (declare (debug t))
  (let ((fn (gensym "fn-"))
        (args (gensym "args-"))
        (value (gensym "value-")))
    `(let ((,fn (function ,(car form)))
           (,args (list ,@(cdr form)))
           ,value)
       ,(my-wrap-form
         `(setq ,value (apply ,fn ,args))
         `(nconc (list :form `(,,fn ,@,args))
                 (list :value ,value))))))

(defun my-test ()
  (my-should (= 1 2)))

Then:

Navigate to the definition of my-wrap-form and evaluate it with C-M-x
Navigate to the definition of my-should and evaluate it with C-u C-M-x
Navigate to the definition of my-test and evaluate it with C-M-x
g
M-: (my-test) RET

Result: The debugger appears with an error (wrong-type-argument consp nil)
in edebug-before.

The sample code above is a much simplified version of the
implementation of ert.el's 'should' macro, which Edebug does not
instrument correctly.

While this nested backquote construction is a valid use of backquote,
and Edebug should be fixed to handle it correctly, I think that this
is a case where backquote makes readability worse instead of better
and the code in ert.el would be more readable if `(,,fn ,@,args) was
replaced by a non-backquoted way of doing the same thing, such as
(cons ,fn ,args).

In GNU Emacs 26.0.91 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.21)
 of 2018-04-03 built on localhost
Windowing system distributor 'The X.Org Foundation', version 11.0.11905000
Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
my-wrap-form
Quit
Edebug: my-should
Mark set
Go...
my-test
Entering debugger...
Back to top level

Configured using:
 'configure
 --prefix=/nix/store/4bablkw2fkvnglq8ha4gj1vj29hiavff-emacs-26.0
 --with-modules --with-x-toolkit=gtk3 --with-xft'

Configured features:
XPM JPEG TIFF GIF PNG RSVG SOUND DBUS GSETTINGS NOTIFY LIBSELINUX
GNUTLS LIBXML2 FREETYPE XFT ZLIB TOOLKIT_SCROLL_BARS GTK3 X11 MODULES
THREADS

Important settings:
  value of $EMACSLOADPATH: /nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Emacs-Lisp

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
/nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/elpy-1.16.0/elpy hides /nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/elpy-1.9.0/elpy
/nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/elpy-1.16.0/elpy-refactor hides /nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/elpy-1.9.0/elpy-refactor
/nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/elpy-1.16.0/elpy-pkg hides /nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/elpy-1.9.0/elpy-pkg
/nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/elpy-1.16.0/elpy-autoloads hides /nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/elpy-1.9.0/elpy-autoloads
/nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/soap-client-3.1.3/soap-inspect hides /nix/store/4bablkw2fkvnglq8ha4gj1vj29hiavff-emacs-26.0/share/emacs/26.0.91/lisp/net/soap-inspect
/nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/soap-client-3.1.3/soap-client hides /nix/store/4bablkw2fkvnglq8ha4gj1vj29hiavff-emacs-26.0/share/emacs/26.0.91/lisp/net/soap-client
/nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/seq-2.20/seq hides /nix/store/4bablkw2fkvnglq8ha4gj1vj29hiavff-emacs-26.0/share/emacs/26.0.91/lisp/emacs-lisp/seq
/nix/store/0gdqa4xzckgz3w0h4cyzsv8al82a601k-emacs-packages-deps/share/emacs/site-lisp/elpa/let-alist-1.0.5/let-alist hides /nix/store/4bablkw2fkvnglq8ha4gj1vj29hiavff-emacs-26.0/share/emacs/26.0.91/lisp/emacs-lisp/let-alist

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
format-spec rfc822 mml mml-sec password-cache epa derived epg
epg-config gnus-util rmail rmail-loaddefs mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail
rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils help-mode
cl-print debug edebug easymenu misearch multi-isearch map seq seq-25
byte-opt gv bytecomp byte-compile cconv cl-loaddefs cl-lib elec-pair
time-date mule-util tooltip eldoc electric uniquify ediff-hook
vc-hooks lisp-float-type mwheel term/x-win x-win term/common-win x-dnd
tool-bar dnd fontset image regexp-opt fringe tabulated-list replace
newcomment text-mode elisp-mode lisp-mode prog-mode register page
menu-bar rfn-eshadow isearch timer select scroll-bar mouse jit-lock
font-lock syntax facemenu font-core term/tty-colors frame cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet
lao korean japanese eucjp-ms cp51932 hebrew greek romanian slovak
czech european ethiopic indian cyrillic chinese composite charscript
charprop case-table epa-hook jka-cmpr-hook help simple abbrev obarray
minibuffer cl-preloaded nadvice loaddefs button faces cus-face
macroexp files text-properties overlay sha1 md5 base64 format env
code-pages mule custom widget hashtable-print-readable backquote
dbusbind inotify dynamic-setting system-font-setting
font-render-setting move-toolbar gtk x-toolkit x multi-tty
make-network-process emacs)

Memory information:
((conses 16 101864 7133)
 (symbols 48 21035 1)
 (miscs 40 52 165)
 (strings 32 30023 1659)
 (string-bytes 1 816911)
 (vectors 16 15702)
 (vector-slots 8 511581 6094)
 (floats 8 51 164)
 (intervals 56 270 0)
 (buffers 992 13))