From unknown Wed Jun 25 05:42:13 2025 X-Loop: bug-gnu-emacs@gnu.org Subject: bug#5728: Byte compile doesn't work right on macro Resent-From: Tom Breton Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-To: owner@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 16 Mar 2010 02:37:01 +0000 Resent-Message-ID: Resent-Sender: bug-gnu-emacs@gnu.org X-GNU-PR-Message: report 5728 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: To: 5728@debbugs.gnu.org X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.12687069975037 (code B ref -1); Tue, 16 Mar 2010 02:37:01 +0000 Received: (at submit) by debbugs.gnu.org; 16 Mar 2010 02:36:37 +0000 Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NrMe2-0001JC-M8 for submit@debbugs.gnu.org; Mon, 15 Mar 2010 22:36:36 -0400 Received: from mail.gnu.org ([199.232.76.166] helo=mx10.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NrMTp-0001F8-ON for submit@debbugs.gnu.org; Mon, 15 Mar 2010 22:26:04 -0400 Received: from lists.gnu.org ([199.232.76.165]:47635) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NrMTm-00067k-1W for submit@debbugs.gnu.org; Mon, 15 Mar 2010 22:25:58 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NrMTl-0006gI-Ck for bug-gnu-emacs@gnu.org; Mon, 15 Mar 2010 22:25:57 -0400 Received: from [140.186.70.92] (port=52411 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NrMTj-0006eL-SM for bug-gnu-emacs@gnu.org; Mon, 15 Mar 2010 22:25:57 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.0 (2010-01-18) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_WEB, T_RP_MATCHES_RCVD autolearn=no version=3.3.0 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NrMTi-0003UF-AS for bug-gnu-emacs@gnu.org; Mon, 15 Mar 2010 22:25:55 -0400 Received: from mail2.panix.com ([166.84.1.73]:61149) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NrMTi-0003UB-7X for bug-gnu-emacs@gnu.org; Mon, 15 Mar 2010 22:25:54 -0400 Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by mail2.panix.com (Postfix) with ESMTP id 75DA638E4D for ; Mon, 15 Mar 2010 22:25:53 -0400 (EDT) Received: from localhost (pool-96-252-49-112.bstnma.fios.verizon.net [96.252.49.112]) by mailbackend.panix.com (Postfix) with ESMTP id 56E932FD44 for ; Mon, 15 Mar 2010 22:25:53 -0400 (EDT) Received: from tehom by localhost with local (Exim 4.69) (envelope-from ) id 1NrMTf-0000f5-TZ for bug-gnu-emacs@gnu.org; Mon, 15 Mar 2010 22:25:51 -0400 Message-Id: From: Tom Breton Date: Mon, 15 Mar 2010 22:25:51 -0400 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Spam-Score: -2.7 (--) X-Mailman-Approved-At: Mon, 15 Mar 2010 22:36:33 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: debbugs-submit-bounces@debbugs.gnu.org Errors-To: debbugs-submit-bounces@debbugs.gnu.org X-Spam-Score: -4.0 (----) When I byte-compile certain code, the results are different than when it's not byte-compiled. It seems to wrongly merge lists. The one unusual thing I was doing with the code is using a macro to generate a call to a ctor (as generated by defstruct) I have attached two elisp files which demonstrate the buggy behavior. The first, "byte-compile-bug.el", is meant to be alternately loaded plain or loaded byte-compiled. The second, "demo-byte-compile-bug.el" is partly a script which demonstartes that the behavior is indeed different when byte-compiled, and partly a collection of (quote-protected) calls to slightly different examples of the bug and examples of similar code which does not exhibit the bug, all defined in the first file. In GNU Emacs 22.2.1 (i486-pc-linux-gnu, X toolkit, Xaw3d scroll bars) of 2008-11-09 on raven, modified by Debian Windowing system distributor `The X.Org Foundation', version 11.0.10402000 configured using `configure '--build=i486-linux-gnu' '--host=i486-linux-gnu' '--prefix=/usr' '--sharedstatedir=/var/lib' '--libexecdir=/usr/lib' '--localstatedir=/var/lib' '--infodir=/usr/share/info' '--mandir=/usr/share/man' '--with-pop=yes' '--enable-locallisppath=/etc/emacs22:/etc/emacs:/usr/local/share/emacs/22.2/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/22.2/site-lisp:/usr/share/emacs/site-lisp:/usr/share/emacs/22.2/leim' '--with-x=yes' '--with-x-toolkit=athena' '--with-toolkit-scroll-bars' 'build_alias=i486-linux-gnu' 'host_alias=i486-linux-gnu' 'CFLAGS=-DDEBIAN -g -O2' 'LDFLAGS=-g' 'CPPFLAGS='' Important settings: value of $LC_ALL: nil value of $LC_COLLATE: nil value of $LC_CTYPE: nil value of $LC_MESSAGES: nil value of $LC_MONETARY: nil value of $LC_NUMERIC: nil value of $LC_TIME: nil value of $LANG: nil locale-coding-system: nil default-enable-multibyte-characters: t Major mode: Lisp Interaction Minor modes in effect: tooltip-mode: t tool-bar-mode: t mouse-wheel-mode: t menu-bar-mode: t file-name-shadow-mode: t global-font-lock-mode: t font-lock-mode: t blink-cursor-mode: t unify-8859-on-encoding-mode: t utf-translate-cjk-mode: t auto-compression-mode: t line-number-mode: t Recent input: M-x r e p o r t Recent messages: ("emacs" "-Q") For information about GNU Emacs and the GNU system, type C-h C-a. Making completion list... Loading help-mode...done Loading emacsbug... Loading regexp-opt...done Loading emacsbug...done next-history-element: Beginning of history; no preceding item ===File ~/projects/elisp/bugs/byte-compile-bug.el============ ;;;_ byte-compile-bug.el --- File to reproduce byte-compile bug ;;;_. Headers ;;;_ , Commentary: ;;Bug report by Tom Breton (Tehom) ;; The bug seems to involve: ;; defstruct ;; macro that builds a ctor. ;; a while loop ;; The byte compiler ;;Observe that it occurs only in compiled code. ;;The macro is made by defmacro* but the same occurs with defmacro ;;feeding defun* ;;;_ , Requires (eval-when-compile (require 'cl)) ;;;_. Body ;;;_ , Type (defstruct (BUG:structure (:conc-name BUG:structure->) (:constructor BUG:make-structure)) "An ADT make by defstruct" edits a) ;;;_ , BUG:helper (defun BUG:helper (accessor oobj form) "" (subst (list accessor oobj) '-old- (copy-tree form))) (defmacro* BUG:make-form (oobj &key edits a) "Construct an BUG:structure object adapted from CAND. Syntax is almost that of a ctor, but in each form, the symbol `-old-' is replaced by the value of the respective field of OOBJ." `(BUG:make-structure :edits ,(BUG:helper 'BUG:structure->edits oobj edits) :a ,(BUG:helper 'BUG:structure->a oobj a))) ;;;_ , BUG (defun BUG (a) "Contains a bug involving the byte-compiler" (let ( (cand (BUG:make-structure :edits '() :a a))) (catch 'BUG:answer (while t (let ( (a (BUG:structure->a cand))) (unless a (throw 'BUG:answer (reverse (BUG:structure->edits cand)))) (setq cand (BUG:make-form cand :edits (cons (car a) -old-) :a (cdr -old-)))))))) (defun BUG-2 (a) "Equivalent to BUG except no catch/throw. Still a bug." (let ( (done nil) (cand (BUG:make-structure :edits '() :a a))) (while (not done) (let ( (a (BUG:structure->a cand))) (if a (setq cand (BUG:make-form cand :edits (cons (car a) -old-) :a (cdr -old-))) (setq done t)))) (reverse (BUG:structure->edits cand)))) (defun BUG-3 (a) "Equivalent to BUG. Also buggy, slightly different manifestation. Slightly different form not involving the second field `a' of BUG:structure." (let ( (cand (BUG:make-structure :edits '()))) (catch 'BUG:answer (while t (unless a (throw 'BUG:answer (reverse (BUG:structure->edits cand)))) (setq cand (BUG:make-form cand :edits (cons (car a) -old-))) (pop a))))) (defun BUG-4 (a) "Equivalent to BUG. Also buggy, slightly different manifestation. Slightly different form not directly using a while loop." (let ( (cand (BUG:make-structure :edits '()))) (dotimes (i (length a)) (setq cand (BUG:make-form cand :edits (cons (car a) -old-))) (pop a)) (reverse (BUG:structure->edits cand)))) (defun NOBUG (a) "Equivalent to BUG. This form extracts elements from list `a' before using them. No buggy behavior." (let ( (cand (BUG:make-structure :edits '() :a a))) (catch 'BUG:answer (progn (dolist (x a) (setq cand (BUG:make-form cand :edits (cons x -old-) :a (cdr -old-)))) (throw 'BUG:answer (reverse (BUG:structure->edits cand))))))) (defun NOBUG-2 (a) "Equivalent to BUG except doesn't use the second field `a' of BUG:structure and extracts elements from list `a' before using them. No buggy behavior." (let ( (cand (BUG:make-structure :edits '()))) (catch 'BUG:answer (while t (let ((x (pop a))) (unless x (throw 'BUG:answer (reverse (BUG:structure->edits cand)))) (setq cand (BUG:make-form cand :edits (cons x -old-)))))))) ;;;_. Footers ;;;_ , Provides (provide 'byte-compile-bug) ;;;_ * Local emacs vars. ;;;_ + Local variables: ;;;_ + mode: allout ;;;_ + End: ;;;_ , End ;;; byte-compile-bug.el ends here ============================================================ ===File ~/projects/elisp/bugs/demo-byte-compile-bug.el====== ;;File to demonstrate this bug. eval-buffer to see it. Also various ;;examples of it are included for easy C-x C-e exploration. (defun BUG:assert-works-ok (str) "" (message str) (assert (equal (BUG '(a b c d)) '(a b c d)) t)) (progn (load-file "byte-compile+cl.el") (BUG:assert-works-ok "Uncompiled version works OK") (byte-compile-file "byte-compile+cl.el") (load-file "byte-compile+cl.elc") (BUG:assert-works-ok "Compiled version doesn't")) ;;For comparison: ;;Buggy equivalents ' (assert (equal (BUG-2 '(a b c d)) '(a b c d)) t) ' (assert (equal (BUG-3 '(a b c d)) '(a b c d)) t) ' (assert (equal (BUG-4 '(a b c d)) '(a b c d)) t) ;;Non-buggy equivalents ' (assert (equal (NOBUG '(a b c d)) '(a b c d)) t) ' (assert (equal (NOBUG-2 '(a b c d)) '(a b c d)) t)============================================================ From unknown Wed Jun 25 05:42:13 2025 X-Loop: help-debbugs@gnu.org Subject: bug#5728: Byte compile doesn't work right on macro Resent-From: Stefan Monnier Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-To: owner@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 08 Apr 2010 20:04:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 5728 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: To: Tom Breton Cc: 5728@debbugs.gnu.org Received: via spool by 5728-submit@debbugs.gnu.org id=B5728.127075698827135 (code B ref 5728); Thu, 08 Apr 2010 20:04:01 +0000 Received: (at 5728) by debbugs.gnu.org; 8 Apr 2010 20:03:08 +0000 Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NzxwS-00073c-IV for submit@debbugs.gnu.org; Thu, 08 Apr 2010 16:03:08 -0400 Received: from pruche.dit.umontreal.ca ([132.204.246.22]) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NzxwP-00073H-Lk for 5728@debbugs.gnu.org; Thu, 08 Apr 2010 16:03:06 -0400 Received: from ceviche.home (faina.iro.umontreal.ca [132.204.26.177]) by pruche.dit.umontreal.ca (8.14.1/8.14.1) with ESMTP id o38K2vpA001255; Thu, 8 Apr 2010 16:02:57 -0400 Received: by ceviche.home (Postfix, from userid 20848) id 3259FB408D; Thu, 8 Apr 2010 16:02:55 -0400 (EDT) From: Stefan Monnier Message-ID: References: Date: Thu, 08 Apr 2010 16:02:55 -0400 In-Reply-To: (Tom Breton's message of "Mon, 15 Mar 2010 22:25:51 -0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-NAI-Spam-Score: 0 X-NAI-Spam-Rules: 1 Rules triggered RV3509=0 X-Spam-Score: -1.2 (-) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: debbugs-submit-bounces@debbugs.gnu.org Errors-To: debbugs-submit-bounces@debbugs.gnu.org X-Spam-Score: -1.9 (-) > When I byte-compile certain code, the results are different than when > it's not byte-compiled. It seems to wrongly merge lists. The one > unusual thing I was doing with the code is using a macro to generate a > call to a ctor (as generated by defstruct) You're victim of a form of "name capture". E.g. if you rename `a' to `b' in BUG3, the bug disappears. And if you macroexpand by hand the call to BUG:make-form the bug is still there. More to the point, the bug is in the compiler-macro for BUG:make-structure: ELISP> (compiler-macroexpand '(BUG:make-structure :edits (cons (car a) (BUG:structure->edits cand)) :a nil)) (block BUG:make-structure (vector 'cl-struct-BUG:structure (cons (car nil) (BUG:structure->edits cand)) nil)) ELISP> (defsubst* toto (a1 a2) (+ a1 a2)) toto ELISP> (compiler-macroexpand '(toto a2 nil)) (block toto (+ nil nil)) ELISP> The problem is that cl-defsubst-expand does the substitution "a1 -> a2 and a2 -> nil" one after the other rather than simultaneously. I've just installed a fix for it in the emacs-23 branch. Stefan From debbugs-submit-bounces@debbugs.gnu.org Tue Feb 14 09:20:18 2012 Received: (at control) by debbugs.gnu.org; 14 Feb 2012 14:20:18 +0000 Received: from localhost ([127.0.0.1]:39832 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1RxJEw-00030i-AE for submit@debbugs.gnu.org; Tue, 14 Feb 2012 09:20:18 -0500 Received: from fencepost.gnu.org ([140.186.70.10]:55485 ident=Debian-exim) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1RxJEu-00030b-Cz for control@debbugs.gnu.org; Tue, 14 Feb 2012 09:20:16 -0500 Received: from bb121-7-229-69.singnet.com.sg ([121.7.229.69]:43417 helo=furball) by fencepost.gnu.org with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1RxJDL-0005Mm-BI for control@debbugs.gnu.org; Tue, 14 Feb 2012 09:18:40 -0500 From: Chong Yidong To: control@debbugs.gnu.org Subject: close 5728 Date: Tue, 14 Feb 2012 22:18:31 +0800 Message-ID: <87aa4l33d4.fsf@gnu.org> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: -4.2 (----) X-Debbugs-Envelope-To: control X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: debbugs-submit-bounces@debbugs.gnu.org Errors-To: debbugs-submit-bounces@debbugs.gnu.org X-Spam-Score: -4.2 (----) close 5728 thanks