1.0.19.24: incorrect function type canonicalization
[sbcl/pkhuong.git] / src / code / target-error.lisp
blob845c64708ec95da0698cd0d8beddf279bd5fd5da
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 ;;; a list of lists of restarts
16 (defvar *restart-clusters* '())
18 ;;; an ALIST (condition . restarts) which records the restarts currently
19 ;;; associated with Condition
20 (defvar *condition-restarts* ())
22 (defun initial-handler-clusters ()
23 `(((warning . ,#'(lambda (warning)
24 (when (typep warning
25 (locally
26 (declare (special sb!ext:*muffled-warnings*))
27 sb!ext:*muffled-warnings*))
28 (muffle-warning warning)))))))
30 (defvar *handler-clusters* (initial-handler-clusters))
32 (defstruct (restart (:copier nil) (:predicate nil))
33 (name (missing-arg) :type symbol :read-only t)
34 (function (missing-arg) :type function)
35 (report-function nil :type (or null function))
36 (interactive-function nil :type (or null function))
37 (test-function (lambda (cond) (declare (ignore cond)) t) :type function))
38 (def!method print-object ((restart restart) stream)
39 (if *print-escape*
40 (print-unreadable-object (restart stream :type t :identity t)
41 (prin1 (restart-name restart) stream))
42 (restart-report restart stream)))
44 (defvar *restart-test-stack* nil)
46 (defun compute-restarts (&optional condition)
47 #!+sb-doc
48 "Return a list of all the currently active restarts ordered from most recently
49 established to less recently established. If CONDITION is specified, then only
50 restarts associated with CONDITION (or with no condition) will be returned."
51 (let ((associated ())
52 (other ()))
53 (dolist (alist *condition-restarts*)
54 (if (eq (car alist) condition)
55 (setq associated (cdr alist))
56 (setq other (append (cdr alist) other))))
57 (collect ((res))
58 (let ((stack *restart-test-stack*))
59 (dolist (restart-cluster *restart-clusters*)
60 (dolist (restart restart-cluster)
61 (when (and (or (not condition)
62 (memq restart associated)
63 (not (memq restart other)))
64 ;; A call to COMPUTE-RESTARTS -- from an error, from
65 ;; user code, whatever -- inside the test function
66 ;; would cause infinite recursion here, so we disable
67 ;; each restart using *restart-test-stack* for the
68 ;; duraction of the test call.
69 (not (memq restart stack))
70 (let ((*restart-test-stack* (cons restart stack)))
71 (declare (truly-dynamic-extent *restart-test-stack*))
72 (funcall (restart-test-function restart) condition)))
73 (res restart)))))
74 (res))))
76 #!+sb-doc
77 (setf (fdocumentation 'restart-name 'function)
78 "Return the name of the given restart object.")
80 (defun restart-report (restart stream)
81 (funcall (or (restart-report-function restart)
82 (let ((name (restart-name restart)))
83 (lambda (stream)
84 (if name (format stream "~S" name)
85 (format stream "~S" restart)))))
86 stream))
88 (defun find-restart (identifier &optional condition)
89 #!+sb-doc
90 "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
91 then the innermost applicable restart with that name is returned. If IDENTIFIER
92 is a restart, it is returned if it is currently active. Otherwise NIL is
93 returned. If CONDITION is specified and not NIL, then only restarts associated
94 with that condition (or with no condition) will be returned."
95 ;; see comment above
96 (if (typep identifier 'restart)
97 (and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
98 identifier)
99 (find identifier (compute-restarts condition) :key #'restart-name)))
101 ;;; helper for the various functions which are ANSI-spec'ed to do
102 ;;; something with a restart or signal CONTROL-ERROR if there is none
103 (defun find-restart-or-control-error (identifier &optional condition)
104 (or (find-restart identifier condition)
105 (error 'simple-control-error
106 :format-control "No restart ~S is active~@[ for ~S~]."
107 :format-arguments (list identifier condition))))
109 (defun invoke-restart (restart &rest values)
110 #!+sb-doc
111 "Calls the function associated with the given restart, passing any given
112 arguments. If the argument restart is not a restart or a currently active
113 non-nil restart name, then a CONTROL-ERROR is signalled."
114 (/show "entering INVOKE-RESTART" restart)
115 (let ((real-restart (find-restart-or-control-error restart)))
116 (apply (restart-function real-restart) values)))
118 (defun interactive-restart-arguments (real-restart)
119 (let ((interactive-function (restart-interactive-function real-restart)))
120 (if interactive-function
121 (funcall interactive-function)
122 '())))
124 (defun invoke-restart-interactively (restart)
125 #!+sb-doc
126 "Calls the function associated with the given restart, prompting for any
127 necessary arguments. If the argument restart is not a restart or a
128 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
129 (let* ((real-restart (find-restart-or-control-error restart))
130 (args (interactive-restart-arguments real-restart)))
131 (apply (restart-function real-restart) args)))
133 (defun assert-error (assertion places datum &rest arguments)
134 (let ((cond (if datum
135 (coerce-to-condition datum
136 arguments
137 'simple-error
138 'error)
139 (make-condition 'simple-error
140 :format-control "The assertion ~S failed."
141 :format-arguments (list assertion)))))
142 (restart-case
143 (error cond)
144 (continue ()
145 :report (lambda (stream)
146 (format stream "Retry assertion")
147 (if places
148 (format stream
149 " with new value~P for ~{~S~^, ~}."
150 (length places)
151 places)
152 (format stream ".")))
153 nil))))
155 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
156 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
157 ;;; and by CHECK-TYPE.
158 (defun read-evaluated-form ()
159 (format *query-io* "~&Type a form to be evaluated:~%")
160 (list (eval (read *query-io*))))
162 (defun check-type-error (place place-value type type-string)
163 (let ((condition
164 (make-condition
165 'simple-type-error
166 :datum place-value
167 :expected-type type
168 :format-control
169 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
170 :format-arguments (list place place-value type-string type))))
171 (restart-case (error condition)
172 (store-value (value)
173 :report (lambda (stream)
174 (format stream "Supply a new value for ~S." place))
175 :interactive read-evaluated-form
176 value))))
178 (defun case-body-error (name keyform keyform-value expected-type keys)
179 (restart-case
180 (error 'case-failure
181 :name name
182 :datum keyform-value
183 :expected-type expected-type
184 :possibilities keys)
185 (store-value (value)
186 :report (lambda (stream)
187 (format stream "Supply a new value for ~S." keyform))
188 :interactive read-evaluated-form
189 value)))