Subject: bug#6878: bool-vectors of length 0 signal error when aref/aset the 0th element

From: MON KEY <[email-masked]>
Date: Wed, 18 Aug 2010 00:19:38 -0400

When aref/aset'ing the 0th element of a bool-vectors of length 0 i get
an args-out-of-range error:

(setq tt--bv (make-bool-vector 29 t))
;=> #&29"\377\377\377"

(vconcat (make-bool-vector 29 t))
; => [t { ... 27 t's ... } t]

(aref tt--bv 0)
;=> t

(aset tt--bv 0 nil)
;=> nil

(setq tt--bv (make-bool-vector 0 t))
;=> #&0""

(vconcat (make-bool-vector 0 t))
;=> []

(aref tt--bv 0)
;=> Debugger entered--Lisp error: (args-out-of-range #&0"" 0)

(aset tt--bv 0 t)
;=> Debugger entered--Lisp error: (args-out-of-range #&0"" 0)

As with the the first bool-vector of length 29 the second bool-vector
of length 0 should also have a value at element 0 that evaluates to
`t'.  (Or, it should according to the manual):

,---- (info "(elisp)Bool-Vector Type")
|
| "A "bool-vector" is a one-dimensional array of elements that must be
| `t' or `nil'."
|
`----

I can't find mention in the docs that the 0th element of a bool-vector
is void for 0 length bool-vectors. Indeed, it isn't at all clear why
it should.

I find this is problematic because there aren't any equivalents to
`car-safe'/`safe-length' for generalized bool-vectors operations.

Obv. taking the 0th index of a vector or char-table also signals an
args-out-of-range error however I belive this for slightly different
reasons.

(setq tt--mv (make-vector 0 t))
;=> []

(vectorp tt--mv)
;=> t

(vectorp tt--bv)
;=> nil

(char-table-p tt--bv)
;=> nil

(vector-or-char-table-p tt--bv)
;=> nil

(arrayp tt--bv)
;=> t

(bool-vector-p tt--bv)
;=> t

(null tt--bv)
;=> nil

(null (append tt--bv))
;=> nil

(null (append tt--bv nil))
;=> t

Maybe something like this is needed:

(defun safe-aref-bool-vector (bool-vector idx)
  (if (and (bool-vector-p bool-vector)
           (not (null (append bool-vector nil))))
      (aref bool-vector idx)
    0))

(defun safe-aset-bool-vector (bool-vector idx)
  (if (and (bool-vector-p bool-vector)
           (not (null (append bool-vector nil))))
      (aset bool-vector idx)
    0))