destructuring-bind uses second value of initform as supplied-p value

Bug #1870004 reported by Steve Losh
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
Fix Released
Undecided
Unassigned

Bug Description

When using destructuring-bind with a &key parameter that has a supplied-p variable, if the keyword is not supplied and the initform returns multiple values, the second value will be used as the value of the supplied-p variable instead of NIL:

[SBCL] CL-USER> (destructuring-bind
                    (&key (x (values 1 2) x-supplied))
                    '()
                  (list x x-supplied))
; => (1 2)
; should be (1 NIL)

[SBCL] CL-USER> (lisp-implementation-version)
"2.0.1"

[SBCL] CL-USER> *features*
(:QUICKLISP :SB-BSD-SOCKETS-ADDRINFO :ASDF3.3 :ASDF3.2 :ASDF3.1 :ASDF3 :ASDF2 :ASDF :OS-UNIX
 :NON-BASE-CHARS-EXIST-P :ASDF-UNICODE :X86-64 :64-BIT :ANSI-CL :COMMON-LISP :ELF :GENCGC
 :IEEE-FLOATING-POINT :LINUX :LITTLE-ENDIAN :PACKAGE-LOCAL-NICKNAMES :SB-LDB :SB-PACKAGE-LOCKS
 :SB-THREAD :SB-UNICODE :SBCL :UNIX)

uname -a
Linux ouroboros 4.15.0-91-generic #92-Ubuntu SMP Fri Feb 28 11:09:48 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

Revision history for this message
James Kalenius (aeshtaer) wrote :

The source of the problem is pretty clear from the macroexpansion:

(destructuring-bind (&key (a (values :foo 'what) a?)) args
  (list args a a?))
=>
(SB-INT:BINDING* ((#:G486
                   (SB-C::CHECK-DS-LIST/&KEY ARGS 0 0 '(&KEY (A)) #(:A)))
                  (#:G487 (SB-C::DS-GETF #:G486 ':A))
                  ((A A?)
                   (IF (EQL #:G487 0)
                       (VALUES :FOO 'WHAT)
                       (VALUES (CAR (TRULY-THE CONS #:G487)) T))))
  (LIST ARGS A A?))

I think it can be fixed by wrapping another values call around the default value, i.e. in expand-ds-bind

(gen-test sense sup-p-form
          (if sup-p-var `(values ,val-form t) val-form)
          def)

should be

(gen-test sense sup-p-form
          (if sup-p-var `(values ,val-form t) val-form)
          `(values ,def nil))

Stas Boukarev (stassats)
Changed in sbcl:
status: New → Fix Committed
Stas Boukarev (stassats)
Changed in sbcl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.