loop accumulation var does not have type declaration.

Bug #1934577 reported by SATO shinichi
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
Fix Released
Undecided
Unassigned

Bug Description

Actually, this is not a bug, just wishes.

(compile nil (lambda ()
               (declare (optimize speed))
               (loop :for c :across "foo"
                     :collect c :into l
                     :finally (return (coerce l 'string)))))

; in: LAMBDA ()
; (COERCE CL-USER::L 'STRING)
;
; note: unable to
; optimize
; due to type uncertainty:
; The first argument is a (OR CONS NULL
; (AND (NOT (VECTOR CHARACTER)) (NOT BASE-STRING)
; VECTOR)
; SB-KERNEL:EXTENDED-SEQUENCE), not a VECTOR.
;
; compilation unit finished
; printed 1 note
#<FUNCTION (LAMBDA ()) {5372198B}>
NIL
NIL

To muffle this note, currently, we need to wrap var (l in this case) with THE special form.

(compile nil (lambda ()
               (declare (optimize speed))
               (loop :for c :across "foo"
                     :collect c :into l
                     :finally (return (coerce
                                        (the list l) ;;;
                                        'string)))))
#<FUNCTION (LAMBDA ()) {5372198B}>
NIL
NIL

Ideally, user-head-var declared type automatically because
list-accumulation var is always the type of LIST.

Probably with-loop-list-collection-head may the ground zero.

(sb-xc:defmacro with-loop-list-collection-head
    ((head-var tail-var &optional user-head-var) &body body)
  (let ((l (and user-head-var (list (list user-head-var nil)))))
    `(let* ((,head-var (list nil)) (,tail-var ,head-var) ,@l)
       (declare (truly-dynamic-extent ,head-var)
  ,@(and user-head-var `((type list ,user-head-var)))) ;;;
       ,@body)))

Sorry, not tested.

Stas Boukarev (stassats)
Changed in sbcl:
status: New → Fix Committed
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.