1 ;;;; gray-box testing of the constructor optimization machinery
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (load "test-util.lisp")
15 (load "compiler-test-util.lisp")
17 (defpackage "CTOR-TEST"
18 (:use
"CL" "TEST-UTIL" "COMPILER-TEST-UTIL"))
20 (in-package "CTOR-TEST")
22 (defclass no-slots
() ())
24 (defun make-no-slots ()
25 (make-instance 'no-slots
))
26 (compile 'make-no-slots
)
28 (with-test (:name
:instance-hash-starts-as-0
)
29 ;; These first two tests look the same but they aren't:
30 ;; the second one uses a CTOR function.
31 (assert (zerop (sb-kernel:%instance-ref
(make-instance 'no-slots
)
32 sb-pcl
::std-instance-hash-slot-index
)))
33 (assert (zerop (sb-kernel:%instance-ref
(make-no-slots)
34 sb-pcl
::std-instance-hash-slot-index
)))
35 (assert (not (zerop (sxhash (make-no-slots))))))
37 (defmethod update-instance-for-redefined-class
38 ((object no-slots
) added discarded plist
&rest initargs
)
39 (declare (ignore initargs
))
40 (error "Called U-I-F-R-C on ~A" object
))
42 (assert (typep (make-no-slots) 'no-slots
))
44 (make-instances-obsolete 'no-slots
)
46 (assert (typep (make-no-slots) 'no-slots
))
47 (assert (typep (funcall #'(sb-pcl::ctor no-slots nil
)) 'no-slots
))
52 (defun make-one-slot-a (a)
53 (make-instance 'one-slot
:a a
))
54 (compile 'make-one-slot-a
)
55 (defun make-one-slot-noa ()
56 (make-instance 'one-slot
))
57 (compile 'make-one-slot-noa
)
59 (defmethod update-instance-for-redefined-class
60 ((object one-slot
) added discarded plist
&rest initargs
)
61 (declare (ignore initargs
))
62 (error "Called U-I-F-R-C on ~A" object
))
64 (assert (= (slot-value (make-one-slot-a 3) 'a
) 3))
65 (assert (not (slot-boundp (make-one-slot-noa) 'a
)))
67 (make-instances-obsolete 'one-slot
)
69 (assert (= (slot-value (make-one-slot-a 3) 'a
) 3))
70 (assert (= (slot-value (funcall #'(sb-pcl::ctor one-slot nil
:a sb-pcl
::\.p0.
) 4) 'a
) 4))
71 (assert (not (slot-boundp (make-one-slot-noa) 'a
)))
72 (assert (not (slot-boundp (funcall #'(sb-pcl::ctor one-slot nil
)) 'a
)))
74 (defclass one-slot-superclass
()
76 (defclass one-slot-subclass
(one-slot-superclass)
79 (defun make-one-slot-subclass (b)
80 (make-instance 'one-slot-subclass
:b b
))
81 (compile 'make-one-slot-subclass
)
83 (defmethod update-instance-for-redefined-class
84 ((object one-slot-superclass
) added discarded plist
&rest initargs
)
85 (declare (ignore initargs
))
86 (error "Called U-I-F-R-C on ~A" object
))
88 (assert (= (slot-value (make-one-slot-subclass 2) 'b
) 2))
90 (make-instances-obsolete 'one-slot-subclass
)
92 (assert (= (slot-value (make-one-slot-subclass 2) 'b
) 2))
93 (assert (= (slot-value (funcall #'(sb-pcl::ctor one-slot-subclass nil
:b sb-pcl
::\.p0.
) 3) 'b
) 3))
94 (make-instances-obsolete 'one-slot-superclass
)
96 (assert (= (slot-value (make-one-slot-subclass 2) 'b
) 2))
97 (assert (= (slot-value (funcall #'(sb-pcl::ctor one-slot-subclass nil
:b sb-pcl
::\.p0.
) 4) 'b
) 4))
99 ;;; Tests for CTOR optimization of non-constant class args and constant class object args
100 (defun find-ctor-caches (fun)
101 (remove-if-not (lambda (value)
102 (and (consp value
) (eq 'sb-pcl
::ctor-cache
(car value
))))
103 (find-value-cell-values fun
)))
105 (let* ((cmacro (compiler-macro-function 'make-instance
))
107 (wrapper (lambda (form env
)
108 (let ((res (funcall cmacro form env
)))
109 (unless (eq form res
)
112 (sb-ext:without-package-locks
115 (setf (compiler-macro-function 'make-instance
) wrapper
)
116 (with-test (:name
(make-instance :non-constant-class
))
118 (let ((f (compile nil
`(lambda (class)
119 (make-instance class
:b t
)))))
120 (assert (= 1 (length (find-ctor-caches f
))))
122 (assert (typep (funcall f
'one-slot-subclass
) 'one-slot-subclass
))))
123 (with-test (:name
(make-instance :constant-class-object
))
124 (let ((f (compile nil
`(lambda ()
125 (make-instance ,(find-class 'one-slot-subclass
) :b t
)))))
126 (assert (not (find-ctor-caches f
)))
128 (assert (typep (funcall f
) 'one-slot-subclass
))))
129 (with-test (:name
(make-instance :constant-non-std-class-object
))
130 (let ((f (compile nil
`(lambda ()
131 (make-instance ,(find-class 'structure-object
))))))
132 (assert (not (find-ctor-caches f
)))
134 (assert (typep (funcall f
) 'structure-object
))))
135 (with-test (:name
(make-instance :constant-non-std-class-name
))
136 (let ((f (compile nil
`(lambda ()
137 (make-instance 'structure-object
)))))
138 (assert (not (find-ctor-caches f
)))
140 (assert (typep (funcall f
) 'structure-object
)))))
141 (setf (compiler-macro-function 'make-instance
) cmacro
))))
143 (with-test (:name
(make-instance :ctor-inline-cache-resize
))
144 (let* ((f (compile nil
`(lambda (name) (make-instance name
))))
145 (classes (loop repeat
(* 2 sb-pcl
::+ctor-table-max-size
+)
146 collect
(class-name (eval `(defclass ,(gentemp) () ())))))
148 (caches (find-ctor-caches f
))
149 (cache (pop caches
)))
151 (assert (not caches
))
152 (assert (not (cdr cache
)))
153 (dolist (class classes
)
154 (assert (typep (funcall f
(if (oddp count
) class
(find-class class
))) class
))
156 (cond ((<= count sb-pcl
::+ctor-list-max-size
+)
157 (unless (consp (cdr cache
))
158 (error "oops, wanted list cache, got: ~S" cache
))
159 (unless (= count
(length (cdr cache
)))
160 (error "oops, wanted ~S elts in cache, got: ~S" count cache
)))
162 (assert (simple-vector-p (cdr cache
))))))
163 (dolist (class classes
)
164 (assert (typep (funcall f
(if (oddp count
) class
(find-class class
))) class
))
167 ;;; Make sure we get default initargs right with on the FAST-MAKE-INSTANCE path CTORs
168 (defclass some-class
()
169 ((aroundp :initform nil
:reader aroundp
))
170 (:default-initargs
:x
:success1
))
172 (defmethod shared-initialize :around
((some-class some-class
) slots
&key
(x :fail?
))
173 (unless (eq x
:success1
)
174 (error "Default initarg lossage"))
175 (setf (slot-value some-class
'aroundp
) t
)
176 (when (next-method-p)
179 (with-test (:name
(make-instance :ctor-default-initargs-1
))
180 (assert (aroundp (eval `(make-instance 'some-class
))))
181 (let ((fun (compile nil
`(lambda () (make-instance 'some-class
)))))
182 (assert (aroundp (funcall fun
)))
183 ;; make sure we tested what we think we tested...
184 (let ((ctors (find-named-callees fun
:type
'sb-pcl
::ctor
)))
186 (assert (not (cdr ctors
)))
187 (assert (find-named-callees (car ctors
) :name
'sb-pcl
::fast-make-instance
)))))
189 ;;; Make sure we get default initargs right with on the FAST-MAKE-INSTANCE path CTORs
190 ;;; in more interesting cases as well...
191 (defparameter *some-counter
* 0)
192 (let* ((x 'success2
))
193 (defclass some-class2
()
194 ((aroundp :initform nil
:reader aroundp
))
195 (:default-initargs
:x
(progn (incf *some-counter
*) x
))))
197 (defmethod shared-initialize :around
((some-class some-class2
) slots
&key
(x :fail2?
))
198 (unless (eq x
'success2
)
199 (error "Default initarg lossage"))
200 (setf (slot-value some-class
'aroundp
) t
)
201 (when (next-method-p)
204 (with-test (:name
(make-instance :ctor-default-initargs-2
))
205 (assert (= 0 *some-counter
*))
206 (assert (aroundp (eval `(make-instance 'some-class2
))))
207 (assert (= 1 *some-counter
*))
208 (let ((fun (compile nil
`(lambda () (make-instance 'some-class2
)))))
209 (assert (= 1 *some-counter
*))
210 (assert (aroundp (funcall fun
)))
211 (assert (= 2 *some-counter
*))
212 ;; make sure we tested what we think we tested...
213 (let ((ctors (find-named-callees fun
:type
'sb-pcl
::ctor
)))
215 (assert (not (cdr ctors
)))
216 (assert (find-named-callees (car ctors
) :name
'sb-pcl
::fast-make-instance
)))))
218 ;;; No compiler notes, please
219 (locally (declare (optimize safety
))
220 (defclass type-check-thing
()
221 ((slot :type
(integer 0) :initarg
:slot
))))
222 (with-test (:name
(make-instance :no-compile-note-at-runtime
))
223 (let ((fun (compile nil
`(lambda (x)
224 (declare (optimize safety
))
225 (make-instance 'type-check-thing
:slot x
)))))
226 (handler-bind ((sb-ext:compiler-note
#'error
))
230 ;;; NO-APPLICABLE-METHOD called
231 (defmethod no-applicable-method ((gf (eql #'make-instance
)) &rest args
)
232 (cons :no-applicable-method args
))
233 (with-test (:name
:constant-invalid-class-arg
)
235 '(:no-applicable-method
"FOO" :quux
14)
236 (funcall (compile nil
`(lambda (x) (make-instance "FOO" :quux x
))) 14)))
238 '(:no-applicable-method
'abc zot
1 bar
2)
239 (funcall (compile nil
`(lambda (x y
) (make-instance ''abc
'zot x
'bar y
)))
241 (with-test (:name
:variable-invalid-class-arg
)
243 '(:no-applicable-method
"FOO" :quux
14)
244 (funcall (compile nil
`(lambda (c x
) (make-instance c
:quux x
))) "FOO" 14)))
246 '(:no-applicable-method
'abc zot
1 bar
2)
247 (funcall (compile nil
`(lambda (c x y
) (make-instance c
'zot x
'bar y
)))
250 (defclass sneaky-class
(standard-class)
253 (defmethod sb-mop:validate-superclass
((class sneaky-class
) (super standard-class
))
257 ((dirty :initform nil
:accessor dirty-slots
)
258 (a :initarg
:a
:reader sneaky-a
)
259 (b :initform
"b" :reader sneaky-b
)
260 (c :accessor sneaky-c
))
261 (:metaclass sneaky-class
))
263 (defvar *supervising
* nil
)
265 (defmethod (setf sb-mop
:slot-value-using-class
)
266 :before
(value (class sneaky-class
) (instance sneaky
) slotd
)
267 (unless *supervising
*
268 (let ((name (sb-mop:slot-definition-name slotd
))
270 (when (slot-boundp instance
'dirty
)
271 (pushnew name
(dirty-slots instance
))))))
273 (with-test (:name
(make-instance :setf-slot-value-using-class-hits-other-slots
))
274 (let ((fun (compile nil
`(lambda (a c
)
275 (let ((i (make-instance 'sneaky
:a a
)))
276 (setf (sneaky-c i
) c
)
279 do
(let ((i (funcall fun
"a" "c")))
280 (assert (equal '(c b a
) (dirty-slots i
)))
281 (assert (equal "a" (sneaky-a i
)))
282 (assert (equal "b" (sneaky-b i
)))
283 (assert (equal "c" (sneaky-c i
)))))))
285 (defclass bug-728650-base
()
290 (defmethod initialize-instance :after
((instance bug-728650-base
) &key
)
291 (with-slots (value) instance
293 (error "Impossible! Value slot not initialized in ~S" instance
))))
295 (defclass bug-728650-child-1
(bug-728650-base)
298 (defmethod initialize-instance :around
((instance bug-728650-child-1
) &rest initargs
&key
)
299 (apply #'call-next-method instance
:value
'provided-by-child-1 initargs
))
301 (defclass bug-728650-child-2
(bug-728650-base)
304 (defmethod initialize-instance :around
((instance bug-728650-child-2
) &rest initargs
&key
)
305 (let ((foo (make-instance 'bug-728650-child-1
)))
306 (apply #'call-next-method instance
:value foo initargs
)))
308 (with-test (:name
:bug-728650
)
309 (let ((child1 (slot-value (make-instance 'bug-728650-child-2
) 'value
)))
310 (assert (typep child1
'bug-728650-child-1
))
311 (assert (eq 'provided-by-child-1
(slot-value child1
'value
)))))
313 (defclass test-fancy-cnm
() ((a :initarg
:a
)))
314 (defmethod initialize-instance :around
((self test-fancy-cnm
) &rest args
)
315 ;; WALK-METHOD-LAMBDA would get to the second form of CALL-NEXT-METHOD
316 ;; and set the CALL-NEXT-METHOD-P flag to :SIMPLE
317 ;; even though it had already been set to T by the earlier call.
319 (call-next-method self
:a
`(expect-this ,(getf args
:a
)))
321 (defun fancy-cnm-in-ii-test (x) (make-instance 'test-fancy-cnm
:a x
))
322 (with-test (:name
:bug-1397454
)
323 (assert (equal (slot-value (fancy-cnm-in-ii-test 'hi
) 'a
)