all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Teika Kazura <teika@gmx.com>
Cc: 62317@debbugs.gnu.org
Subject: bug#62317: 28.2; This byte-compiled file behaves wrongly.
Date: Sat, 01 Apr 2023 12:27:35 -0400	[thread overview]
Message-ID: <jwv355jwple.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <20230321.125408.609857763486645873.teika@gmx.com> (Teika Kazura's message of "Tue, 21 Mar 2023 12:54:08 +0900 (JST)")

> * How to reproduce:
> First, ~/.emacs.d/init.el:
> ------------------------------------------------------------------------
> (eval-and-compile
>   (push user-emacs-directory load-path)
>   (require 'a))
>
> ;; Hereafter is ignored when byte-compiled.
>
> (defvar foo-var 'baz)
> (message "Hello, world.")
> (pop-to-buffer "*Messages*")
> ------------------------------------------------------------------------
>
> ~/.emacs.d/a.el:
> ------------------------------------------------------------------------
> (set-buffer "*Messages*") ;; or (set-buffer (get-buffer-create "bar"))
> (provide 'a)
> ------------------------------------------------------------------------

I suspect you can simplify the above to:

    (eval-and-compile (set-buffer "*Messages*"))
    ;; Hereafter is ignored when byte-compiled.
    (message "Hello, world.")

> Stefan, any ideas?  I think switching to a different buffer inside
> `eval-and-compile` is a bad idea, but maybe I'm missing something.

I tend to agree.
[ Side note: (push user-emacs-directory load-path) is also a bad idea.  ]

We could guard against this to some extent, but there will always be
ways for the code executed at compile-time to mess up the state of
the compiler, so I'm not sure where we should draw the line.

FWIW, in my book `set-buffer` is a code smell (usually better replaced
by `with-current-buffer`).

Admittedly, the resulting behavior can be very puzzling&frustrating for
the user, which would tend to argue in favor of trying to at least
detect the problem.
But note that if the code switched to a buffer where point is not at
EOB, we'd probably get helpful error messages during compilation, so
I'm leaning towards considering it a "minor corner case" issue, but
I think the untested patch below would be enough to plug the hole.


        Stefan


diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el
index 5df1205869c..e22ab94e378 100644
--- a/lisp/emacs-lisp/bytecomp.el
+++ b/lisp/emacs-lisp/bytecomp.el
@@ -2376,7 +2376,10 @@ byte-compile-from-buffer
                  (form (read-positioning-symbols inbuffer))
                  (warning (byte-run--unescaped-character-literals-warning)))
             (when warning (byte-compile-warn-x form "%s" warning))
-	    (byte-compile-toplevel-file-form form)))
+            ;; Defend against macros using `set-buffer' or `goto-char'
+            ;; bug#62317.
+            (save-excursion
+	      (byte-compile-toplevel-file-form form))))
 	;; Compile pending forms at end of file.
 	(byte-compile-flush-pending)
 	(byte-compile-warn-about-unresolved-functions)))






  parent reply	other threads:[~2023-04-01 16:27 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-21  3:54 bug#62317: 28.2; This byte-compiled file behaves wrongly Teika Kazura
2023-03-22  6:39 ` bug#62317: bug #62317: " Teika Kazura
2023-03-28  9:15 ` Teika Kazura
2023-03-28 12:08   ` Eli Zaretskii
2023-03-28 12:43     ` Andrea Corallo
2023-03-28 12:53       ` Eli Zaretskii
2023-03-28 13:49         ` Andrea Corallo
2023-03-30  9:20       ` Teika Kazura
2023-03-30  9:41         ` Eli Zaretskii
2023-04-01  7:36 ` Teika Kazura
2023-04-01  8:08   ` Eli Zaretskii
2023-04-01 16:27 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-04-01 17:10   ` bug#62317: " Eli Zaretskii
2023-04-01 23:09     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-04-02  5:17       ` Eli Zaretskii
2023-09-12  0:08         ` Stefan Kangas
2023-09-12 18:12           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-13 14:02             ` Stefan Kangas
2023-09-13 14:45               ` Eli Zaretskii
2023-09-13 15:46                 ` Stefan Kangas
2023-04-01 23:15   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-09-12  0:09     ` Stefan Kangas
2023-04-05  4:12   ` bug#62317: bug #62317: " Teika Kazura
     [not found] ` <handler.62317.C.169462003624017.notifdonectrl.0@debbugs.gnu.org>
2023-09-14  6:35   ` bug#62317: acknowledged by developer Teika Kazura

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwv355jwple.fsf-monnier+emacs@gnu.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=62317@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=teika@gmx.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.