Subject: bug#55038: 29.0.50; Does macroexp-file-name return nil unnecessarily?

From: Stefan Monnier <[email-masked]>
Date: Thu, 21 Apr 2022 09:20:22 -0400

> Consider a macro like
>
>         (defmacro foo ()
>           `(message "Defined in %s" ,(macroexp-file-name)))
>
> Why does this expand to
>
>         (message "Defined in %s" nil)
>
> when evaluated directly in a file (say using `eval-buffer' or
> `eval-last-sexp`).

No good reason.

> Shouldn't it be fair to return the file name of the buffer, if nothing
> else can be used:

In the case the code comes from the current buffer, yes.
OTOH if it comes from (say) `M-:`, no.

> diff --git a/lisp/emacs-lisp/macroexp.el b/lisp/emacs-lisp/macroexp.el
> index e4bc2df280..5df9d4b221 100644
> --- a/lisp/emacs-lisp/macroexp.el
> +++ b/lisp/emacs-lisp/macroexp.el
> @@ -143,7 +143,8 @@ macroexp-file-name
>    ;; so prefer using it over using `load-file-name'.
>    (let ((file (car (last current-load-list))))
>      (or (if (stringp file) file)
> -        (bound-and-true-p byte-compile-current-file))))
> +        (bound-and-true-p byte-compile-current-file)
> +        (buffer-file-name))))

I think we need to make `eval-buffer` and friends set a variable to
indicate when `buffer-file-name` can be used.

        Stefan