7

Consider the following Common Lisp code.

(print (let* ((x nil) (y x)) (setq x t) (and y x)))  ; T
(print (let* ((x nil) (y x)) (setq x t) (and x y)))  ; NIL
(print (let* ((x nil) (y t)) (setq y x) (setq x t) (and y x)))  ; NIL
(defun noop (x) (when x (princ "")))
(print (let* ((x nil) (y x)) (noop y) (setq x t) (and y x)))  ; NIL
(print (let* ((x nil) (y x)) (setq x t) (noop y) (and y x)))  ; NIL

Running the above code in SBCL 2.4.9 in linux, the first line prints T while the remaining lines print NIL. This online SBCL interpreter shows the same results. This is not the case in CLISP, for instance, where all the lines print NIL.

Why am I getting this seemingly strange result?


UPDATE 1: I reported this to the SBCL bug tracker: https://bugs.launchpad.net/sbcl/+bug/2092588

UPDATE 2: Apparently, one of the project's maintainers committed a fix for this bug in response to my report. That was really fast!

9
  • Looks like a compiler error. Commented Dec 27, 2024 at 15:26
  • 1
    @RainerJoswig, Does that mean that I should report this to the SBCL bug tracker? Commented Dec 27, 2024 at 15:28
  • That would be a good idea! That would be very helpful. Thanks! Commented Dec 27, 2024 at 15:34
  • 1
    Most likely an optimizer bug. Does changing (DECLARE (OPTIMIZE ...)) settings affect the result? Commented Dec 27, 2024 at 15:53
  • Happens with DEBUG qualities 0 and 1. Here with macOS 15.2 SBCL 2.4.11 on an M4 Pro. Commented Dec 27, 2024 at 16:08

1 Answer 1

2

As you said yourself, this was a compiler bug in versions up to 2.4.11. It was fixed in 2.5.0:

  • bug fix: fix miscompilation of IF where the consequent and alternative would have the same value but for an intervening side-effect. (#2092588, reported by JA)

It was apparently introduced in 1.4.14:

zeta@machine:$ cat bad.lisp
(let* ((x nil) (y x)) (setq x t) (if (and y x)
                                   (format t "BUG~%")
                                   (format t "Fine~%")))
zeta@machine:$ podman run --quiet --rm -it -v `pwd`:/tmp fukamachi/sbcl:1.4.13-alpine --script /tmp/bad.lisp
Fine
zeta@machine:$ podman run --quiet --rm -it -v `pwd`:/tmp fukamachi/sbcl:1.4.14-alpine --script /tmp/bad.lisp
BUG

If you use Debian, then all versions up to Debian 12 are affected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.