Ticket #1094 (closed defect: notabug)

Opened 2 years ago

Last modified 2 years ago

self-modified function doesn't behave correctly

Reported by: hasarkinn Owned by:
Priority: normal Milestone:
Component: IDE Version: trunk
Keywords: Cc:


(defun f () (setf (symbol-function 'f) (lambda () 1)) (f))

Such a function will cause an infinite loop when it is call at first time but when interupting and calling (f) again, it has been modified to (lambda () 1) I have tried several CL implementation and they all behave correctly, they return 1 and do not cause an infinite loop.

Change History

comment:1 Changed 2 years ago by rme

  • Status changed from new to closed
  • Resolution set to notabug

You need to add a (declare (notinline f)) to get that to behave as you desire.

The compiler is allowed to assume that functional references to f within the body of (defun f ...) refer to the function being defined. This allows it to omit the implicit call to symbol-function on self-calls. Declaring the function notinline forbids the compiler from making that assumption.

This same issue affects tracing recursive functions, since trace works by changing what symbol-function returns. See also  http://clozure.com/pipermail/openmcl-devel/2003-March/004655.html

comment:2 Changed 2 years ago by rme

Here is the part of the spec that talks about this:  http://www.lispworks.com/documentation/HyperSpec/Body/03_bbc.htm (in particular, see the fourth bullet item).

Note: See TracTickets for help on using tickets.