Reduce WITH-SIMPLE-RESTART and RESTART-CASE expansion.
[sbcl.git] / src / code / target-error.lisp
blob80c7c7c4d9b410a8f6277de4742fc6fc95bf53ed
1 ;;;; that part of the condition system which can or should come early
2 ;;;; (mostly macro-related)
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!KERNEL")
15 (defun muffle-warning-p (warning)
16 (declare (special *muffled-warnings*))
17 (typep warning *muffled-warnings*))
19 ;; Host lisp does not need a value for this, so start it out as NIL.
20 (defglobal **initial-handler-clusters** nil)
21 (setq **initial-handler-clusters**
22 `(((,(find-classoid-cell 'warning :create t)
24 ,(named-lambda "MAYBE-MUFFLE" (warning)
25 (when (muffle-warning-p warning)
26 (muffle-warning warning)))))))
28 ;;; Each cluster is an alist of the form
29 ;;;
30 ;;; ((TYPE-TEST1 . HANDLER1) (TYPE-TEST2 . HANDLER2) ...)
31 ;;;
32 ;;; where TYPE-TESTN are functions of one argument which test a given
33 ;;; condition instance for the type required by the corresponding
34 ;;; HANDLERN. HANDLERN are function designators.
35 ;;;
36 ;;; Newly established handlers are added at the beginning of the
37 ;;; list. Elements to the left of the alist take precedence over
38 ;;; elements to the right.
39 ;;;
40 ;;; Lists to which *HANDLER-CLUSTERS* is bound generally have dynamic
41 ;;; extent.
42 (defvar *handler-clusters* **initial-handler-clusters**)
44 ;;; a list of lists of currently active RESTART instances. maintained
45 ;;; by RESTART-BIND.
46 ;;; This variable is proclaimed to be "eventually" always-bound,
47 ;;; meaning in the loaded code. If DEFVAR were used, the compiler knows that
48 ;;; BOUNDP will be T, and therefore a code deletion note should be issused
49 ;;; for the initialization expression (UNLESS (BOUNDP x) <initform>).
50 ;;; Technically it's not even right to use DEFVAR because it works only
51 ;;; if the initializer is really NIL, since that is what %DEFVAR will assign
52 ;;; on account of the fact that an initializer was supplied at all.
53 (defparameter *restart-clusters* '())
55 (defmethod print-object ((restart restart) stream)
56 (if *print-escape*
57 (print-unreadable-object (restart stream :type t :identity t)
58 (prin1 (restart-name restart) stream))
59 (restart-report restart stream)))
61 #!+sb-doc
62 (setf (fdocumentation 'restart-name 'function)
63 "Return the name of the given restart object.")
65 (defun restart-report (restart stream)
66 (if (restart-report-function restart)
67 (funcall (truly-the function (restart-report-function restart))
68 stream)
69 (prin1 (or (restart-name restart)
70 restart)
71 stream)))
73 (defvar *restart-test-stack* nil)
75 ;; Call FUNCTION with all restarts in the current dynamic environment,
76 ;; 1) that are associated to CONDITION (when CONDITION is NIL, all
77 ;; restarts are processed)
78 ;; 2) and for which the restart test returns non-NIL for CONDITION.
79 ;; When CALL-TEST-P is non-NIL, all restarts are processed.
80 (defun map-restarts (function &optional condition (call-test-p t))
81 (declare (function function))
82 (let ((stack *restart-test-stack*))
83 (dolist (restart-cluster *restart-clusters*)
84 (dolist (restart restart-cluster)
85 (when (and (or (not condition)
86 (null (restart-associated-conditions restart))
87 (memq condition (restart-associated-conditions restart)))
88 ;; A call to COMPUTE-RESTARTS -- from an error,
89 ;; from user code, whatever -- inside the test
90 ;; function would cause infinite recursion here, so
91 ;; we disable each restart using
92 ;; *restart-test-stack* for the duration of the
93 ;; test call.
94 (not (memq restart stack))
95 (or (not call-test-p)
96 (let ((*restart-test-stack* (cons restart stack)))
97 (declare (truly-dynamic-extent *restart-test-stack*))
98 (funcall (restart-test-function restart) condition))))
99 (funcall function restart))))))
101 (defun compute-restarts (&optional condition)
102 #!+sb-doc
103 "Return a list of all the currently active restarts ordered from most recently
104 established to less recently established. If CONDITION is specified, then only
105 restarts associated with CONDITION (or with no condition) will be returned."
106 (collect ((result))
107 (map-restarts (lambda (restart) (result restart)) condition)
108 (result)))
110 (defun %find-restart (identifier condition &optional (call-test-p t))
111 (flet ((eq-restart-p (restart)
112 (when (eq identifier restart)
113 (return-from %find-restart restart)))
114 (named-restart-p (restart)
115 (when (eq identifier (restart-name restart))
116 (return-from %find-restart restart))))
117 ;; KLUDGE: can the compiler infer this dx automatically?
118 (declare (truly-dynamic-extent #'eq-restart-p #'named-restart-p))
119 (if (typep identifier 'restart)
120 ;; The code under #+previous-... below breaks the abstraction
121 ;; introduced by MAP-RESTARTS, but is about twice as
122 ;; fast as #+equivalent-... . Also, it is a common case due to
124 ;; (INVOKE-RESTART RESTART)
125 ;; -> (FIND-RESTART-OR-CONTROL-ERROR RESTART)
126 ;; -> (FIND-RESTART RESTART)
128 ;; However, both #+previous-... and #+equivalent-... may be
129 ;; wrong altogether because of
130 ;; https://bugs.launchpad.net/sbcl/+bug/774410:
131 ;; The behavior expected in that report can be achieved by the
132 ;; following line (which is, of course, the slowest of all
133 ;; possibilities):
134 (map-restarts #'eq-restart-p condition call-test-p)
136 #+equivalent-to-previous-sbcl-behavior--faster-but-see-bug-774410
137 (map-restarts #'eq-restart-p nil nil)
139 #+previous-behavior--fastest-but-see-bug-774410
140 (and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
141 identifier)
143 (map-restarts #'named-restart-p condition call-test-p))))
145 (defun find-restart (identifier &optional condition)
146 #!+sb-doc
147 "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
148 then the innermost applicable restart with that name is returned. If IDENTIFIER
149 is a restart, it is returned if it is currently active. Otherwise NIL is
150 returned. If CONDITION is specified and not NIL, then only restarts associated
151 with that condition (or with no condition) will be returned."
152 ;; Calls MAP-RESTARTS such that restart test functions are
153 ;; respected.
154 (%find-restart identifier condition))
156 ;;; helper for the various functions which are ANSI-spec'ed to do
157 ;;; something with a restart or signal CONTROL-ERROR if there is none
158 (defun find-restart-or-control-error (identifier &optional condition (call-test-p t))
159 (declare (optimize allow-non-returning-tail-call))
160 (or (%find-restart identifier condition call-test-p)
161 (error 'simple-control-error
162 :format-control "No restart ~S is active~@[ for ~S~]."
163 :format-arguments (list identifier condition))))
165 (defun invoke-restart (restart &rest values)
166 #!+sb-doc
167 "Calls the function associated with the given restart, passing any given
168 arguments. If the argument restart is not a restart or a currently active
169 non-nil restart name, then a CONTROL-ERROR is signalled."
170 (/show "entering INVOKE-RESTART" restart)
171 ;; The following code calls MAP-RESTARTS (through
172 ;; FIND-RESTART-OR-CONTROL-ERROR -> %FIND-RESTART) such that restart
173 ;; test functions are respected when RESTART is a symbol, but not
174 ;; when RESTART is a RESTART instance.
176 ;; Without disabling test functions for the RESTART instance case,
177 ;; the following problem would arise:
179 ;; (restart-case
180 ;; (handler-bind
181 ;; ((some-condition (lambda (c)
182 ;; (invoke-restart (find-restart 'foo c)) ; a)
183 ;; (invoke-restart 'foo) ; b)
184 ;; )))
185 ;; (signal 'some-condition))
186 ;; (foo ()
187 ;; :test (lambda (c) (typep c 'some-condition))))
189 ;; In case a), INVOKE-RESTART receives the RESTART instance, but
190 ;; cannot supply the condition instance needed by the test. In case
191 ;; b) INVOKE-RESTART calls FIND-RESTART, but again cannot supply the
192 ;; condition instance. As a result, the restart would be impossible
193 ;; the invoke.
194 (let ((real-restart (find-restart-or-control-error
195 restart nil (symbolp restart))))
196 (apply (restart-function real-restart) values)))
198 (defun interactive-restart-arguments (real-restart)
199 (let ((interactive-function (restart-interactive-function real-restart)))
200 (if interactive-function
201 (funcall interactive-function)
202 '())))
204 (defun invoke-restart-interactively (restart)
205 #!+sb-doc
206 "Calls the function associated with the given restart, prompting for any
207 necessary arguments. If the argument restart is not a restart or a
208 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
209 ;; For an explanation of the call to FIND-RESTART-OR-CONTROL-ERROR,
210 ;; see comment in INVOKE-RESTART.
211 (let* ((real-restart (find-restart-or-control-error
212 restart nil (symbolp restart)))
213 (args (interactive-restart-arguments real-restart)))
214 (apply (restart-function real-restart) args)))
216 ;;; To reduce expansion size of RESTART-CASE
217 (defun with-simple-condition-restarts (function cerror-arg datum &rest arguments)
218 (let ((condition (coerce-to-condition datum arguments
219 (case function
220 (warn 'simple-warning)
221 (signal 'simple-condition)
222 (t 'simple-error))
223 function)))
224 (with-condition-restarts condition (car *restart-clusters*)
225 (if (eq function 'cerror)
226 (cerror cerror-arg condition)
227 (funcall function condition)))))
230 (defun assert-error (assertion &optional args-and-values places datum &rest arguments)
231 (let ((cond (if datum
232 (coerce-to-condition
233 datum arguments 'simple-error 'error)
234 (make-condition
235 'simple-error
236 :format-control "~@<The assertion ~S failed~:[.~:; ~
237 with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
238 :format-arguments (list assertion args-and-values)))))
239 (restart-case
240 (error cond)
241 (continue ()
242 :report (lambda (stream)
243 (format stream "Retry assertion")
244 (if places
245 (format stream " with new value~P for ~{~S~^, ~}."
246 (length places) places)
247 (format stream ".")))
248 nil))))
250 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
251 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
252 ;;; and by CHECK-TYPE.
253 (defun read-evaluated-form (&optional (prompt-control nil promptp)
254 &rest prompt-args)
255 (apply #'format *query-io*
256 (if promptp prompt-control "~&Type a form to be evaluated: ")
257 prompt-args)
258 (finish-output *query-io*)
259 (list (eval (read *query-io*))))
261 (defun check-type-error (place place-value type &optional type-string)
262 (let ((condition
263 (make-condition
264 'simple-type-error
265 :datum place-value
266 :expected-type type
267 :format-control
268 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
269 :format-arguments (list place place-value type-string type))))
270 (restart-case (error condition)
271 (store-value (value)
272 :report (lambda (stream)
273 (format stream "Supply a new value for ~S." place))
274 :interactive read-evaluated-form
275 value))))
277 (defun etypecase-failure (value keys)
278 (declare (optimize allow-non-returning-tail-call))
279 (error 'case-failure
280 :name 'etypecase
281 :datum value
282 :expected-type `(or ,@keys)
283 :possibilities keys))
285 (defun ecase-failure (value keys)
286 (declare (optimize allow-non-returning-tail-call))
287 (error 'case-failure
288 :name 'ecase
289 :datum value
290 :expected-type `(member ,@keys)
291 :possibilities keys))
293 (defun case-body-error (name keyform keyform-value expected-type keys)
294 (restart-case
295 (error 'case-failure
296 :name name
297 :datum keyform-value
298 :expected-type expected-type
299 :possibilities keys)
300 (store-value (value)
301 :report (lambda (stream)
302 (format stream "Supply a new value for ~S." keyform))
303 :interactive read-evaluated-form
304 value)))