Subject: bug#5863: defadvice in byte compiled file does not work

From: Stefan Kangas <[email-masked]>
Date: Wed, 18 Nov 2020 19:23:05 -0800

Stefan Monnier <[email-masked]> writes:

>> The info page I pointed to says that defadvice should work for
>> primitives called from lisp code.  And that is what does not work for
>> me here.
>
> For such uses, there are several categories of functions:
> - functions that have their own byte-code (things like widen, car, cdr, ...).
> - functions that are implemented in C but don't have their own
>   byte-code.
> - other functions.
>
> Pieces of advice on the first kind of functions only work for
> calls from interpreted Lisp code.
> Pieces of advice on the second kind of functions only work for
> calls from Lisp code (both interpreted and byte-compiled).

BTW, it seems like advising primitives will work once we get native-comp
merged:

  The result of this procedure is that each newly activated function will
  use the trampoline in place of the original primitive and the
  trampoline will execute the call going through funcall making the
  advice effective!

  This works so well that in-fact now is even possible to advice
  effectively what wasn't effective in byte-code (ex the + function). But
  hey, don't try this a home!

  https://akrl.sdf.org/gccemacs.html#org3b7398e

I'm not sure how this would affect the status of this bug, if at all.
Perhaps we could expand the description of why advising primitives is a
bad idea in the Info node `(elisp) Advising Named Functions':

  It is possible to advise a primitive (*note What Is a Function::),
  but one should typically _not_ do so, for two reasons.  Firstly, some
  primitives are used by the advice mechanism, and advising them could
  cause an infinite recursion.  Secondly, many primitives are called
  directly from C, and such calls ignore advice; hence, one ends up in a
  confusing situation where some calls (occurring from Lisp code) obey the
  advice and other calls (from C code) do not.

We could perhaps simply say that advice on primitives with their own
bytecode will be ineffective if the code is run byte-compiled.