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
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 (defun initial-handler-clusters ()
20 `(((warning . ,#'(lambda (warning)
21 (when (muffle-warning-p warning)
22 (muffle-warning warning)))))))
24 ;;; an alist with elements of the form
26 ;;; (CONDITION . (HANDLER1 HANDLER2 ...))
28 ;;; Recently established handlers are added at the beginning of the
29 ;;; list. Elements to the left of the alist take precedence over
30 ;;; elements to the right.
31 (defvar *handler-clusters* (initial-handler-clusters))
33 ;;; a list of lists of currently active RESTART instances. maintained
35 (defvar *restart-clusters* '())
37 (declaim (inline restart-test-function
38 restart-associated-conditions
39 (setf restart-associated-conditions)))
40 (defstruct (restart (:copier nil) (:predicate nil))
41 (name (missing-arg) :type symbol :read-only t)
42 (function (missing-arg) :type function :read-only t)
43 (report-function nil :type (or null function) :read-only t)
44 (interactive-function nil :type (or null function) :read-only t)
45 (test-function (lambda (cond) (declare (ignore cond)) t) :type function :read-only t)
46 ;; the list of conditions which are currently associated to the
47 ;; restart. maintained by WITH-CONDITION-RESTARTS in a neither
48 ;; thread- nor interrupt-safe way. This should not be a problem
49 ;; however, since safe uses of restarts have to assume dynamic
51 (associated-conditions '() :type list))
53 (def!method print-object ((restart restart) stream)
55 (print-unreadable-object (restart stream :type t :identity t)
56 (prin1 (restart-name restart) stream))
57 (restart-report restart stream)))
60 (setf (fdocumentation 'restart-name 'function)
61 "Return the name of the given restart object.")
63 (defun restart-report (restart stream)
64 (funcall (or (restart-report-function restart)
66 (format stream "~S" (or (restart-name restart)
70 (defvar *restart-test-stack* nil)
72 ;; Call FUNCTION with all restarts in the current dynamic environment,
73 ;; 1) that are associated to CONDITION (when CONDITION is NIL, all
74 ;; restarts are processed)
75 ;; 2) and for which the restart test returns non-NIL for CONDITION.
76 ;; When CALL-TEST-P is non-NIL, all restarts are processed.
77 (defun map-restarts (function &optional condition (call-test-p t))
78 ;; FIXME: if MAP-RESTARTS is internal, we could require the FUNCTION
79 ;; argument to be of type FUNCTION.
80 (let ((function (coerce function 'function))
81 (stack *restart-test-stack*))
82 (dolist (restart-cluster *restart-clusters*)
83 (dolist (restart restart-cluster)
84 (when (and (or (not condition)
85 (null (restart-associated-conditions restart))
86 (memq condition (restart-associated-conditions restart)))
87 ;; A call to COMPUTE-RESTARTS -- from an error,
88 ;; from user code, whatever -- inside the test
89 ;; function would cause infinite recursion here, so
90 ;; we disable each restart using
91 ;; *restart-test-stack* for the duration of the
93 (not (memq restart stack))
95 (let ((*restart-test-stack* (cons restart stack)))
96 (declare (truly-dynamic-extent *restart-test-stack*))
97 (funcall (restart-test-function restart) condition))))
98 (funcall function restart))))))
100 (defun compute-restarts (&optional condition)
102 "Return a list of all the currently active restarts ordered from most recently
103 established to less recently established. If CONDITION is specified, then only
104 restarts associated with CONDITION (or with no condition) will be returned."
106 (map-restarts (lambda (restart) (result restart)) condition)
109 (defun %find-restart (identifier &optional condition (call-test-p t))
110 (flet ((eq-restart-p (restart)
111 (when (eq identifier restart)
112 (return-from %find-restart restart)))
113 (named-restart-p (restart)
114 (when (eq identifier (restart-name restart))
115 (return-from %find-restart restart))))
116 ;; TODO Question for reviewer: does the compiler infer this dx
118 (declare (truly-dynamic-extent #'eq-restart-p #'named-restart-p))
119 (if (typep identifier 'restart)
120 ;; TODO Questions for reviewer:
122 ;; The code under #+previous-... below breaks the abstraction
123 ;; introduced by MAP-RESTARTS, but is about twice as
124 ;; fast as #+equivalent-... . Also, it is a common case due to
126 ;; (INVOKE-RESTART RESTART)
127 ;; -> (FIND-RESTART-OR-CONTROL-ERROR RESTART)
128 ;; -> (FIND-RESTART RESTART)
130 ;; However, both #+previous-... and #+equivalent-... may be
131 ;; wrong altogether because of
132 ;; https://bugs.launchpad.net/sbcl/+bug/774410:
133 ;; The behavior expected in that report can be achieved by the
134 ;; following line (which is, of course, the slowest of all
136 (map-restarts #'eq-restart-p condition call-test-p)
138 #+equivalent-to-previous-sbcl-behavior--faster-but-see-bug-774410
139 (map-restarts #'eq-restart-p nil nil)
141 #+previous-behavior--fastest-but-see-bug-774410
142 (and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
145 (map-restarts #'named-restart-p condition call-test-p))))
147 (defun find-restart (identifier &optional condition)
149 "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
150 then the innermost applicable restart with that name is returned. If IDENTIFIER
151 is a restart, it is returned if it is currently active. Otherwise NIL is
152 returned. If CONDITION is specified and not NIL, then only restarts associated
153 with that condition (or with no condition) will be returned."
154 ;; Calls MAP-RESTARTS such that restart test functions are
156 (%find-restart identifier condition))
158 ;;; helper for the various functions which are ANSI-spec'ed to do
159 ;;; something with a restart or signal CONTROL-ERROR if there is none
160 (defun find-restart-or-control-error (identifier &optional condition (call-test-p t))
161 (or (%find-restart identifier condition call-test-p)
162 (error 'simple-control-error
163 :format-control "No restart ~S is active~@[ for ~S~]."
164 :format-arguments (list identifier condition))))
166 (defun invoke-restart (restart &rest values)
168 "Calls the function associated with the given restart, passing any given
169 arguments. If the argument restart is not a restart or a currently active
170 non-nil restart name, then a CONTROL-ERROR is signalled."
171 (/show "entering INVOKE-RESTART" restart)
172 ;; The following code calls MAP-RESTARTS (through
173 ;; FIND-RESTART-OR-CONTROL-ERROR -> %FIND-RESTART) such that restart
174 ;; test functions are respected when RESTART is a symbol, but not
175 ;; when RESTART is a RESTART instance.
177 ;; Without disabling test functions for the RESTART instance case,
178 ;; the following problem would arise:
182 ;; ((some-condition (lambda (c)
183 ;; (invoke-restart (find-restart 'foo c)) ; a)
184 ;; (invoke-restart 'foo) ; b)
186 ;; (signal 'some-condition))
188 ;; :test (lambda (c) (typep c 'some-condition))))
190 ;; In case a), INVOKE-RESTART receives the RESTART instance, but
191 ;; cannot supply the condition instance needed by the test. In case
192 ;; b) INVOKE-RESTART calls FIND-RESTART, but again cannot supply the
193 ;; condition instance. As a result, the restart would be impossible
195 (let ((real-restart (find-restart-or-control-error
196 restart nil (symbolp restart))))
197 (apply (restart-function real-restart) values)))
199 (defun interactive-restart-arguments (real-restart)
200 (let ((interactive-function (restart-interactive-function real-restart)))
201 (if interactive-function
202 (funcall interactive-function)
205 (defun invoke-restart-interactively (restart)
207 "Calls the function associated with the given restart, prompting for any
208 necessary arguments. If the argument restart is not a restart or a
209 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
210 (let* ((real-restart (find-restart-or-control-error restart))
211 (args (interactive-restart-arguments real-restart)))
212 (apply (restart-function real-restart) args)))
214 (defun assert-error (assertion args-and-values places datum &rest arguments)
215 (let ((cond (if datum
217 datum arguments 'simple-error 'error)
220 :format-control "~@<The assertion ~S failed~:[.~:; ~
221 with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
222 :format-arguments (list assertion args-and-values)))))
226 :report (lambda (stream)
227 (format stream "Retry assertion")
229 (format stream " with new value~P for ~{~S~^, ~}."
230 (length places) places)
231 (format stream ".")))
234 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
235 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
236 ;;; and by CHECK-TYPE.
237 (defun read-evaluated-form (&optional (prompt-control nil promptp)
239 (apply #'format *query-io*
240 (if promptp prompt-control "~&Type a form to be evaluated: ")
242 (finish-output *query-io*)
243 (list (eval (read *query-io*))))
245 (defun check-type-error (place place-value type type-string)
252 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
253 :format-arguments (list place place-value type-string type))))
254 (restart-case (error condition)
256 :report (lambda (stream)
257 (format stream "Supply a new value for ~S." place))
258 :interactive read-evaluated-form
261 (defun case-failure (name value keys)
265 :expected-type (if (eq name 'ecase)
268 :possibilities keys))
270 (defun case-body-error (name keyform keyform-value expected-type keys)
275 :expected-type expected-type
278 :report (lambda (stream)
279 (format stream "Supply a new value for ~S." keyform))
280 :interactive read-evaluated-form