%coerce-callable-to-fun lack of error checking sometimes

Bug #1987710 reported by Douglas Katzman
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
New
Undecided
Unassigned

Bug Description

SYMBOL-FUNCTION (and fdefinition) is specified to return an arbitrary implementation-chosen value for macros and special operators.
Historically SBCL (and CMUCL) return a function which when called complains that it's not really a function. A consequence of that choice is that an error is almost never trapped if no funcall occurs, such as in:

* (mapcar (symbol-function 'throw) '())
NIL
* (mapcar (symbol-function 'and) '())
NIL

Most other lisps return a magic value from symbol-function. We could do that also.
The desired outcome is that there is no extra cost to rejecting macros wherever the compiler has optimized a higher-order function to using %coerce-callable eagerly (i.e. before trying an actual funcall). We definitely don't want %coerce-callable to check some arbitrary property of the specified function to see if it's really supposed to be called. (We do that if the arg to %coerce-to-callable was a symbol, but in that event it already costs something to lookup the function)

It may work to have macros and symbols look internally like un-fboundp symbols, and also impart behavior to symbol-function, fdefinition, fboundp that don't signal an error. This would fix the %coerce-callable situation efficiently.
Granted it's only a problem for sequence traversal operations on empty sequences. Maybe there are some other situations, but this caught my attention.

Revision history for this message
Stas Boukarev (stassats) wrote :

It's kinda similar to (mapcar 'car nil nil) not signaling an error because it's never applied. The standard says it returns an implementation defined value, and returning a function is such a value. Then it's valid to pass functions to mapcar.

So, it's not a standard compliance nor a safety issue, just catching bad values early.

Revision history for this message
Douglas Katzman (dougk) wrote :

Well, I think there may be a minor compliance issue if we assume an unknown name refers to a function. Sharp quote can not take a macro but we treat #' and QUOTE the same.
Let's agree that FOO1 gets a free pass here; it's harder to justify that (FOO2 NIL) is legal.

(defun foo1 (x) (mapcar (symbol-function 'something) x)) ; dubious but OK
(defun foo2 (x) (mapcar #'something x)) ; "technically" should barf
(defmacro something () ) ; only after misusing #'something
;; FOO2 _assumed_ that #' is ok but ZOOK knows that it's not ok
(defun zook () #'something) ; and this rightly fails to compile

Revision history for this message
Stas Boukarev (stassats) wrote :

It says "SOMETHING is being redefined as a macro when it was previously assumed to be a function."

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.