9a7aa8fce561662b735bc204aa0c33c500e0299d
[sbcl.git] / src / code / target-error.lisp
1 ;;;; that part of the condition system which can or should come early
2 ;;;; (mostly macro-related)
3
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.
12
13 (in-package "SB!KERNEL")
14
15 (defun muffle-warning-p (warning)
16   (declare (special *muffled-warnings*))
17   (typep warning *muffled-warnings*))
18
19 (defun initial-handler-clusters ()
20   `(((warning . ,#'(lambda (warning)
21                      (when (muffle-warning-p warning)
22                        (muffle-warning warning)))))))
23
24 ;;; an alist with elements of the form
25 ;;;
26 ;;;  (CONDITION . (HANDLER1 HANDLER2 ...))
27 ;;;
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))
32
33 ;;; a list of lists of currently active RESTART instances. maintained
34 ;;; by RESTART-BIND.
35 (defvar *restart-clusters* '())
36
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
50   ;; extent.
51   (associated-conditions '() :type list))
52
53 (def!method print-object ((restart restart) stream)
54   (if *print-escape*
55       (print-unreadable-object (restart stream :type t :identity t)
56         (prin1 (restart-name restart) stream))
57       (restart-report restart stream)))
58
59 #!+sb-doc
60 (setf (fdocumentation 'restart-name 'function)
61       "Return the name of the given restart object.")
62
63 (defun restart-report (restart stream)
64   (funcall (or (restart-report-function restart)
65                (lambda (stream)
66                  (format stream "~S" (or (restart-name restart)
67                                          restart))))
68            stream))
69
70 (defvar *restart-test-stack* nil)
71
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
92                    ;; test call.
93                    (not (memq restart stack))
94                    (or (not call-test-p)
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))))))
99
100 (defun compute-restarts (&optional condition)
101   #!+sb-doc
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."
105   (collect ((result))
106     (map-restarts (lambda (restart) (result restart)) condition)
107     (result)))
108
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
117     ;; automatically?
118     (declare (truly-dynamic-extent #'eq-restart-p #'named-restart-p))
119     (if (typep identifier 'restart)
120         ;; TODO Questions for reviewer:
121         ;;
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
125         ;;
126         ;;    (INVOKE-RESTART RESTART)
127         ;; -> (FIND-RESTART-OR-CONTROL-ERROR RESTART)
128         ;; -> (FIND-RESTART RESTART)
129         ;;
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
135         ;; possibilities):
136         (map-restarts #'eq-restart-p condition call-test-p)
137
138         #+equivalent-to-previous-sbcl-behavior--faster-but-see-bug-774410
139         (map-restarts #'eq-restart-p nil nil)
140
141         #+previous-behavior--fastest-but-see-bug-774410
142         (and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
143              identifier)
144
145         (map-restarts #'named-restart-p condition call-test-p))))
146
147 (defun find-restart (identifier &optional condition)
148   #!+sb-doc
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
155   ;; respected.
156   (%find-restart identifier condition))
157
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))))
165
166 (defun invoke-restart (restart &rest values)
167   #!+sb-doc
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.
176   ;;
177   ;; Without disabling test functions for the RESTART instance case,
178   ;; the following problem would arise:
179   ;;
180   ;;  (restart-case
181   ;;      (handler-bind
182   ;;          ((some-condition (lambda (c)
183   ;;                             (invoke-restart (find-restart 'foo c)) ; a)
184   ;;                             (invoke-restart 'foo)                  ; b)
185   ;;                             )))
186   ;;        (signal 'some-condition))
187   ;;    (foo ()
188   ;;     :test (lambda (c) (typep c 'some-condition))))
189   ;;
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
194   ;; the invoke.
195   (let ((real-restart (find-restart-or-control-error
196                        restart nil (symbolp restart))))
197     (apply (restart-function real-restart) values)))
198
199 (defun interactive-restart-arguments (real-restart)
200   (let ((interactive-function (restart-interactive-function real-restart)))
201     (if interactive-function
202         (funcall interactive-function)
203         '())))
204
205 (defun invoke-restart-interactively (restart)
206   #!+sb-doc
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)))
213 \f
214 (defun assert-error (assertion args-and-values places datum &rest arguments)
215   (let ((cond (if datum
216                   (coerce-to-condition
217                    datum arguments 'simple-error 'error)
218                   (make-condition
219                    'simple-error
220                    :format-control "~@<The assertion ~S failed~:[.~:; ~
221                                     with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
222                    :format-arguments (list assertion args-and-values)))))
223     (restart-case
224         (error cond)
225       (continue ()
226         :report (lambda (stream)
227                   (format stream "Retry assertion")
228                   (if places
229                       (format stream " with new value~P for ~{~S~^, ~}."
230                               (length places) places)
231                       (format stream ".")))
232         nil))))
233
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)
238                             &rest prompt-args)
239   (apply #'format *query-io*
240          (if promptp prompt-control "~&Type a form to be evaluated: ")
241          prompt-args)
242   (finish-output *query-io*)
243   (list (eval (read *query-io*))))
244
245 (defun check-type-error (place place-value type type-string)
246   (let ((condition
247          (make-condition
248           'simple-type-error
249           :datum place-value
250           :expected-type type
251           :format-control
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)
255       (store-value (value)
256         :report (lambda (stream)
257                   (format stream "Supply a new value for ~S." place))
258         :interactive read-evaluated-form
259         value))))
260
261 (defun case-failure (name value keys)
262   (error 'case-failure
263          :name name
264          :datum value
265          :expected-type (if (eq name 'ecase)
266                             `(member ,@keys)
267                             `(or ,@keys))
268          :possibilities keys))
269
270 (defun case-body-error (name keyform keyform-value expected-type keys)
271   (restart-case
272       (error 'case-failure
273              :name name
274              :datum keyform-value
275              :expected-type expected-type
276              :possibilities keys)
277     (store-value (value)
278       :report (lambda (stream)
279                 (format stream "Supply a new value for ~S." keyform))
280       :interactive read-evaluated-form
281       value)))