'assoc' is not side-effect-free; constprop its pure subset

Since a supplied test function can do anything, assoc is not
side-effect-free (bug#44018).  However, with only two arguments it is
pure and should be optimised accordingly.

* lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Remove 'assoc'.
(byte-optimize-assoc): Constant-propagate through 2-arg assoc calls.
* test/lisp/emacs-lisp/bytecomp-tests.el
(byte-opt-testsuite-arith-data): Add test cases.
This commit is contained in:
Mattias Engdegård 2020-10-31 14:16:25 +01:00
parent 8d72075aeb
commit 0cbcc6223a
2 changed files with 14 additions and 7 deletions

View file

@ -830,11 +830,13 @@
(defun byte-optimize-assoc (form)
;; Replace 2-argument `assoc' with `assq', `rassoc' with `rassq',
;; if the first arg is a symbol.
(if (and (= (length form) 3)
(byte-optimize--constant-symbol-p (nth 1 form)))
(cons (if (eq (car form) 'assoc) 'assq 'rassq)
(cdr form))
form))
(cond
((/= (length form) 3)
form)
((byte-optimize--constant-symbol-p (nth 1 form))
(cons (if (eq (car form) 'assoc) 'assq 'rassq)
(cdr form)))
(t (byte-optimize-constant-args form))))
(defun byte-optimize-memq (form)
;; (memq foo '(bar)) => (and (eq foo 'bar) '(bar))
@ -1144,7 +1146,7 @@
;; I wonder if I missed any :-\)
(let ((side-effect-free-fns
'(% * + - / /= 1+ 1- < <= = > >= abs acos append aref ash asin atan
assoc assq
assq
boundp buffer-file-name buffer-local-variables buffer-modified-p
buffer-substring byte-code-function-p
capitalize car-less-than-car car cdr ceiling char-after char-before

View file

@ -365,7 +365,12 @@
'(((a b)) a b (c) (d)))
(mapcar (lambda (x) (cond ((memq '(a b) x) 1)
((equal x '(c)) 2)))
'(((a b)) a b (c) (d))))
'(((a b)) a b (c) (d)))
(assoc 'b '((a 1) (b 2) (c 3)))
(assoc "b" '(("a" 1) ("b" 2) ("c" 3)))
(let ((x '((a 1) (b 2) (c 3)))) (assoc 'c x))
(assoc 'a '((a 1) (b 2) (c 3)) (lambda (u v) (not (equal u v)))))
"List of expression for test.
Each element will be executed by interpreter and with
bytecompiled code, and their results compared.")