Eliminate style-warning about undefined type GLOBAL-VAR
[sbcl.git] / src / code / target-error.lisp
blob298aae608c9d960310580ed154b1bab91ce6eed4
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 (def!method 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 (or (%find-restart identifier condition call-test-p)
160 (error 'simple-control-error
161 :format-control "No restart ~S is active~@[ for ~S~]."
162 :format-arguments (list identifier condition))))
164 (defun invoke-restart (restart &rest values)
165 #!+sb-doc
166 "Calls the function associated with the given restart, passing any given
167 arguments. If the argument restart is not a restart or a currently active
168 non-nil restart name, then a CONTROL-ERROR is signalled."
169 (/show "entering INVOKE-RESTART" restart)
170 ;; The following code calls MAP-RESTARTS (through
171 ;; FIND-RESTART-OR-CONTROL-ERROR -> %FIND-RESTART) such that restart
172 ;; test functions are respected when RESTART is a symbol, but not
173 ;; when RESTART is a RESTART instance.
175 ;; Without disabling test functions for the RESTART instance case,
176 ;; the following problem would arise:
178 ;; (restart-case
179 ;; (handler-bind
180 ;; ((some-condition (lambda (c)
181 ;; (invoke-restart (find-restart 'foo c)) ; a)
182 ;; (invoke-restart 'foo) ; b)
183 ;; )))
184 ;; (signal 'some-condition))
185 ;; (foo ()
186 ;; :test (lambda (c) (typep c 'some-condition))))
188 ;; In case a), INVOKE-RESTART receives the RESTART instance, but
189 ;; cannot supply the condition instance needed by the test. In case
190 ;; b) INVOKE-RESTART calls FIND-RESTART, but again cannot supply the
191 ;; condition instance. As a result, the restart would be impossible
192 ;; the invoke.
193 (let ((real-restart (find-restart-or-control-error
194 restart nil (symbolp restart))))
195 (apply (restart-function real-restart) values)))
197 (defun interactive-restart-arguments (real-restart)
198 (let ((interactive-function (restart-interactive-function real-restart)))
199 (if interactive-function
200 (funcall interactive-function)
201 '())))
203 (defun invoke-restart-interactively (restart)
204 #!+sb-doc
205 "Calls the function associated with the given restart, prompting for any
206 necessary arguments. If the argument restart is not a restart or a
207 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
208 ;; For an explanation of the call to FIND-RESTART-OR-CONTROL-ERROR,
209 ;; see comment in INVOKE-RESTART.
210 (let* ((real-restart (find-restart-or-control-error
211 restart nil (symbolp restart)))
212 (args (interactive-restart-arguments real-restart)))
213 (apply (restart-function real-restart) args)))
216 (defun assert-error (assertion args-and-values places datum &rest arguments)
217 (let ((cond (if datum
218 (coerce-to-condition
219 datum arguments 'simple-error 'error)
220 (make-condition
221 'simple-error
222 :format-control "~@<The assertion ~S failed~:[.~:; ~
223 with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
224 :format-arguments (list assertion args-and-values)))))
225 (restart-case
226 (error cond)
227 (continue ()
228 :report (lambda (stream)
229 (format stream "Retry assertion")
230 (if places
231 (format stream " with new value~P for ~{~S~^, ~}."
232 (length places) places)
233 (format stream ".")))
234 nil))))
236 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
237 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
238 ;;; and by CHECK-TYPE.
239 (defun read-evaluated-form (&optional (prompt-control nil promptp)
240 &rest prompt-args)
241 (apply #'format *query-io*
242 (if promptp prompt-control "~&Type a form to be evaluated: ")
243 prompt-args)
244 (finish-output *query-io*)
245 (list (eval (read *query-io*))))
247 (defun check-type-error (place place-value type type-string)
248 (let ((condition
249 (make-condition
250 'simple-type-error
251 :datum place-value
252 :expected-type type
253 :format-control
254 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
255 :format-arguments (list place place-value type-string type))))
256 (restart-case (error condition)
257 (store-value (value)
258 :report (lambda (stream)
259 (format stream "Supply a new value for ~S." place))
260 :interactive read-evaluated-form
261 value))))
263 (defun case-failure (name value keys)
264 (error 'case-failure
265 :name name
266 :datum value
267 :expected-type (if (eq name 'ecase)
268 `(member ,@keys)
269 `(or ,@keys))
270 :possibilities keys))
272 (defun case-body-error (name keyform keyform-value expected-type keys)
273 (restart-case
274 (error 'case-failure
275 :name name
276 :datum keyform-value
277 :expected-type expected-type
278 :possibilities keys)
279 (store-value (value)
280 :report (lambda (stream)
281 (format stream "Supply a new value for ~S." keyform))
282 :interactive read-evaluated-form
283 value)))