]> git.eshelyaron.com Git - emacs.git/commitdiff
Cease attempts to const-propagate through setq
authorMattias Engdegård <mattiase@acm.org>
Thu, 5 Aug 2021 09:50:25 +0000 (11:50 +0200)
committerMattias Engdegård <mattiase@acm.org>
Thu, 5 Aug 2021 13:33:05 +0000 (15:33 +0200)
The current method of propagating constants through setq was unsound
because it relied on each setq form only being traversed at most once
during optimisation, which isn't necessarily true in general; it could
be made to miscompile code in rare cases.

Since it was only used in limited circumstances, disabling this
optimisation doesn't cost us much.

* lisp/emacs-lisp/byte-opt.el (byte-optimize-form-code-walker):
Don't update the known value when traversing `setq`.
* test/lisp/emacs-lisp/bytecomp-tests.el (bytecomp-tests--test-cases):
Add test case.

lisp/emacs-lisp/byte-opt.el
test/lisp/emacs-lisp/bytecomp-tests.el

index 96072ea1b551410e523dd8b58c044f90a16d83b8..6475f69eded7d87fc487044eacc64e02c040f11b 100644 (file)
@@ -601,15 +601,9 @@ Same format as `byte-optimize--lexvars', with shared structure and contents.")
                   (lexvar (assq var byte-optimize--lexvars))
                   (value (byte-optimize-form expr nil)))
              (when lexvar
-               ;; Set a new value or inhibit further substitution.
-               (setcdr (cdr lexvar)
-                       (and
-                        ;; Inhibit if bound outside conditional code.
-                        (not (assq var byte-optimize--vars-outside-condition))
-                        ;; The new value must be substitutable.
-                        (byte-optimize--substitutable-p value)
-                        (list value)))
-               (setcar (cdr lexvar) t))   ; Mark variable to be kept.
+               (setcar (cdr lexvar) t)    ; Mark variable to be kept.
+               (setcdr (cdr lexvar) nil)) ; Inhibit further substitution.
+
              (push var var-expr-list)
              (push value var-expr-list))
            (setq args (cddr args)))
index 5aa853c721263eaed4fed0549a98036d4c3a1114..80003c264a2236a11152fb731ddb0c84b23f4dd4 100644 (file)
     (let ((x 2))
       (list (or (bytecomp-test-identity 'a) (setq x 3)) x))
 
+    (mapcar (lambda (b)
+              (let ((a nil))
+                (+ 0
+                   (progn
+                     (setq a b)
+                     (setq b 1)
+                     a))))
+            '(10))
+
     (let* ((x 1)
            (y (condition-case x
                   (/ 1 0)