surprising behavior of dynamic-extent in do forms

Bug #1766003 reported by Cyrus Harmon on 2018-04-21
This bug affects 1 person
Affects Status Importance Assigned to Milestone

Bug Description

I'm seeing some surprising (to me at least) behavior caused by dynamic-extent declarations in do forms. Phoe on #sbcl reduced the issue to, roughly, the following test case:

(defun foo (startr endr)
  (let ((acc 0))
    (do ((i startr (1+ i))) ((> i endr))
      (declare (dynamic-extent i))
      (incf acc i))

[note that this works without the acc stuff, I just used this to convince myself that this wasn't all just optimized away]

When I compile this form I get:
cd /home/sly/projects/clem/src/
1 compiler notes:

    could not stack allocate STARTR in: (DO ((I STARTR (1+ I))) ((> I ENDR)) (DECLARE (DYNAMIC-EXTENT I)) (INCF ACC I))
    --> BLOCK LET

Why should the dynamic-extent declaration of i affect whether or not startr is stack allocated?

A trivial rewrite of this using loop does not trigger the same compiler note.

Stas Boukarev (stassats) wrote :

Because it's the initial value of I.

Cyrus Harmon (ch-launchpad) wrote :

Why is the behavior of do so different from:

(defun foo2 (startr endr)
  (declare (dynamic-extent startr))
  (let ((acc 0))
    (loop for i fixnum from startr to endr
       do (incf acc i))

where, again, the initial value of i is startr, but I see no warning? I'm imagine I'm missing something obvious here.

Stas Boukarev (stassats) wrote :

There's no warning in your first form either.

And no notes for external entry point variables is a new thing, since in some cases it can be dxed at the call time now.

Cyrus Harmon (ch-launchpad) wrote :

Ok, I misspoke. note, not warning. I still don't see why the do form issues a note but the loop form doesn't.

Stas Boukarev (stassats) wrote :

The loop form doesn't even have any dynamic-extent declarations, so I'm not sure how you're comparing them.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers