all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#35770: [PATCH] Broken duplicate case elimination in switch byte-compilation
@ 2019-05-17  9:33 Mattias Engdegård
  2019-05-17 12:16 ` Stefan Monnier
  0 siblings, 1 reply; 16+ messages in thread
From: Mattias Engdegård @ 2019-05-17  9:33 UTC (permalink / raw)
  To: 35770; +Cc: Stefan Monnier, vibhavp

[-- Attachment #1: Type: text/plain, Size: 543 bytes --]

The byte-code compiler attempts to eliminate duplicated cases when turning a `cond' into a switch table, as in

  (cond ((eq x 'a) 1)
         (eq x 'b) 2)
         (eq x 'a) 3)) ; remove

but it doesn't work properly, because of a confusion between expressions and their values, and a logic error that would have discarded the entire table instead of just skipped the duplicate.

This patch attempts to rectify that. I also removed a seemingly redundant condition, the (consp condition), which should always be true at that point.


[-- Attachment #2: 0001-Correctly-eliminate-duplicate-cases-in-switch-compil.patch --]
[-- Type: application/octet-stream, Size: 2503 bytes --]

From adb7adf2271e65023adc0ce5d6251054b55c6452 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Fri, 17 May 2019 11:25:06 +0200
Subject: [PATCH] Correctly eliminate duplicate cases in switch compilation

Fix code mistakes that prevented the correct elimination of duplicated
cases when compiling a `cond' form to a switch bytecode, as in

  (cond ((eq x 'a) 1)
        ((eq x 'b) 2)
        ((eq x 'a) 3))  ; should be elided

* lisp/emacs-lisp/bytecomp.el (byte-compile-cond-vars): Return obj2 eval'ed.
(byte-compile-cond-jump-table-info):
Discard redundant condition.  Use `obj2' as evaluated.
Discard duplicated cases instead of failing the table generation.
---
 lisp/emacs-lisp/bytecomp.el | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el
index e76baf5ed0..7f7cbe5a68 100644
--- a/lisp/emacs-lisp/bytecomp.el
+++ b/lisp/emacs-lisp/bytecomp.el
@@ -4091,8 +4091,8 @@ that suppresses all warnings during execution of BODY."
   ;; and the other is a constant expression whose value can be
   ;; compared with `eq' (with `macroexp-const-p').
   (or
-   (and (symbolp obj1) (macroexp-const-p obj2) (cons obj1 obj2))
-   (and (symbolp obj2) (macroexp-const-p obj1) (cons obj2 obj1))))
+   (and (symbolp obj1) (macroexp-const-p obj2) (cons obj1 (eval obj2)))
+   (and (symbolp obj2) (macroexp-const-p obj1) (cons obj2 (eval obj1)))))
 
 (defconst byte-compile--default-val (cons nil nil) "A unique object.")
 
@@ -4121,12 +4121,11 @@ Return a list of the form ((TEST . VAR)  ((VALUE BODY) ...))"
                (unless prev-test
                  (setq prev-test test))
                (if (and obj1 (memq test '(eq eql equal))
-                        (consp condition)
                         (eq test prev-test)
-                        (eq obj1 prev-var)
-                        ;; discard duplicate clauses
-                        (not (assq obj2 cases)))
-                   (push (list (if (consp obj2) (eval obj2) obj2) body) cases)
+                        (eq obj1 prev-var))
+                   ;; discard duplicate clauses
+                   (unless (assq obj2 cases)
+                     (push (list obj2 body) cases))
                  (if (and (macroexp-const-p condition) condition)
 		     (progn (push (list byte-compile--default-val
 					(or body `(,condition)))
-- 
2.20.1 (Apple Git-117)


^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2019-06-01 21:53 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-05-17  9:33 bug#35770: [PATCH] Broken duplicate case elimination in switch byte-compilation Mattias Engdegård
2019-05-17 12:16 ` Stefan Monnier
2019-05-17 14:48   ` Mattias Engdegård
2019-05-22 10:52     ` Mattias Engdegård
2019-05-22 10:58       ` Eli Zaretskii
2019-05-22 11:11         ` Mattias Engdegård
2019-05-22 11:21           ` Noam Postavsky
2019-05-22 11:23             ` Eli Zaretskii
2019-05-22 14:19               ` Stefan Monnier
2019-05-26 17:05                 ` Mattias Engdegård
2019-05-26 18:43                   ` Eli Zaretskii
2019-05-26 22:06                     ` Stefan Monnier
2019-05-27 11:27                       ` Mattias Engdegård
2019-06-01 13:58                         ` Noam Postavsky
2019-06-01 15:47                           ` Eli Zaretskii
2019-06-01 21:53                             ` Noam Postavsky

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.