1 ;;;; package lock tests with side effects
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.
16 (load "assertoid.lisp")
17 (load "compiler-test-util.lisp")
18 (use-package "ASSERTOID")
20 ;;;; Our little labrats and a few utilities
22 (defpackage :test-used
)
24 (defpackage :test-unused
)
26 (defpackage :test-nicknamed
)
28 (defpackage :test-not-nicknamed
)
30 (defpackage :test-aux
(:export
#:noslot
#:noslot2
))
61 (defvar *uninterned
* "UNINTERNED")
62 (defvar *interned
* "INTERNED")
64 (defun maybe-unintern (name package
)
65 (let ((s (find-symbol name package
)))
67 (unintern s package
))))
69 (defun set-test-locks (lock-p)
70 (dolist (p '(:test
:test-aux
:test-delete
))
71 (when (find-package p
)
73 (sb-ext:lock-package p
)
74 (sb-ext:unlock-package p
)))))
76 (defun reset-test (lock)
77 "Reset TEST package to a known state, ensure that TEST-DELETE exists."
78 (unless (find-package :test-delete
)
79 (make-package :test-delete
))
80 (sb-ext:with-unlocked-packages
(:test
:test-aux
)
81 (dolist (s '(test:nosymbol-macro
82 test
:noclass test
:nostruct test
:nostruct2 test
:nocondition
))
85 (intern (symbol-name s
) :test
))
86 (rename-package (find-package :test
) :test
)
87 (dolist (nickname (package-local-nicknames :test
))
88 (remove-package-local-nickname (car nickname
) :test
))
89 (add-package-local-nickname :nicknamed
:test-nicknamed
:test
)
90 (unexport (intern "INTERNAL" :test
) :test
)
91 (intern *interned
* :test
)
92 (use-package :test-used
:test
)
93 (export 'test
::external
:test
)
94 (unuse-package :test-unused
:test
)
95 (defclass test
:class
() ())
96 (defun test:function
() 'test
:function
)
97 (defmacro test
:macro
() ''test
:macro
)
98 (defparameter test
:*special
* 'test
:*special
*)
99 (defconstant test
:constant
'test
:constant
)
100 (intern "UNUSED" :test
)
101 (dolist (s '(test:nocondition-slot test
:noclass-slot test
:nostruct-slot
102 test-aux
:noslot test-aux
:noslot2
))
104 (ignore-errors (progn
105 (fmakunbound 'test
:unused
)
106 (makunbound 'test
:unused
)))
107 (maybe-unintern *uninterned
* :test
)
108 (maybe-unintern "NOT-FROM-TEST" :test
)
109 (defconstant test
:num
0)
110 (define-symbol-macro test
:symbol-macro
"SYMBOL-MACRO")
111 (defun test:numfun
(n) n
)
112 (defun test:car
(cons) (cl:car cons
))
113 (defun (setf test
:cdr
) (obj cons
) (setf (cl:cdr cons
) obj
))
114 (assert (not (find-symbol *uninterned
* :test
))))
115 (set-test-locks lock
))
117 (defun tmp-fmakunbound (x)
118 "FMAKUNDBOUND x, then restore the original binding."
119 (let ((f (fdefinition x
)))
121 (ignore-errors (setf (fdefinition x
) f
))))
123 (defmacro with-error-info
((string &rest args
) &body forms
)
124 `(handler-bind ((error (lambda (e)
125 (declare (ignorable e
))
126 (format t
,string
,@args
)
132 ;;; A collection of forms that are legal both with and without package
134 (defvar *legal-forms
*
135 '(;; package alterations that don't actually mutate the package
136 (intern *interned
* :test
)
137 (import 'test
:unused
:test
)
138 (shadowing-import 'test
:shadowed
:test
)
139 (export 'test
:unused
:test
)
140 (unexport 'test
::internal
:test
)
141 (let ((p (find-package :test
)))
142 (rename-package p
:test
))
143 (use-package :test-used
:test
)
144 (unuse-package :test-unused
:test
)
145 (shadow "SHADOWED" :test
)
146 (let ((s (with-unlocked-packages (:test
)
147 (let ((s (intern *uninterned
* :test
)))
152 ;; binding and altering value
153 (let ((test:function
123))
154 (assert (eql test
:function
123)))
155 (let ((test:*special
* :foo
))
156 (assert (eql test
:*special
* :foo
)))
158 (setf test
:*special
* :quux
)
159 (assert (eql test
:*special
* :quux
)))
160 (let ((test:unused
:zot
))
161 (assert (eql test
:unused
:zot
)))
164 (symbol-macrolet ((test:function
:sym-ok
))
165 (assert (eql test
:function
:sym-ok
)))
166 (symbol-macrolet ((test:unused
:sym-ok2
))
167 (assert (eql test
:unused
:sym-ok2
)))
169 ;; binding as a function
170 (flet ((test:*special
* () :yes
))
171 (assert (eql (test:*special
*) :yes
)))
172 (flet ((test:unused
() :yes
!))
173 (assert (eql (test:unused
) :yes
!)))
174 (labels ((test:*special
* () :yes
))
175 (assert (eql (test:*special
*) :yes
)))
176 (labels ((test:unused
() :yes
!))
177 (assert (eql (test:unused
) :yes
!)))
179 ;; binding as a macro
180 (macrolet ((test:*special
* () :ok
))
181 (assert (eql (test:*special
*) :ok
)))
184 ;;; A collection of forms that cause runtime package lock violations
185 ;;; on TEST, and will also signal an error on LOAD even if first
186 ;;; compiled with COMPILE-FILE with TEST unlocked.
187 (defvar *illegal-runtime-forms
*
188 '(;; package alterations
189 (intern *uninterned
* :test
)
190 (import 'not-from-test
:test
)
191 (export 'test
::internal
:test
)
192 (unexport 'test
:external
:test
)
193 (shadowing-import 'not-from-test
:test
)
194 (let ((p (find-package :test
)))
195 (rename-package p
:test
'(:test-nick
)))
196 (use-package :test-unused
:test
)
197 (unuse-package :test-used
:test
)
198 (add-package-local-nickname :not-nicknamed
:test-not-nicknamed
:test
)
199 (remove-package-local-nickname :nicknamed
:test
)
200 (shadow 'not-from-test
:test
)
201 (unintern (or (find-symbol *interned
* :test
) (error "bugo")) :test
)
202 (delete-package :test-delete
)
204 ;; redefining or undefining as a function
205 (defun test:function
() 'foo
)
206 (setf (fdefinition 'test
:function
) (lambda () 'bar
))
207 (setf (symbol-function 'test
:function
) (lambda () 'quux
))
208 (tmp-fmakunbound 'test
:function
)
210 ;; defining or undefining as a macro or compiler macro
211 (defmacro test
:unused
() ''foo
)
212 (setf (macro-function 'test
:unused
) (constantly 'foo
))
213 (define-compiler-macro test
:unused
(&whole form arg
)
214 (declare (ignore arg
))
216 (setf (compiler-macro-function 'test
:unused
) (constantly 'foo
))
218 ;; type-specifier or structure
220 (defstruct test
:nostruct test
:nostruct-slot
)
221 ;; test creation as well, since the structure-class won't be
222 ;; finalized before that
223 (make-nostruct :nostruct-slot
:foo
))
224 (defclass test
:noclass
()
225 ((slot :initform nil
:accessor test
:noclass-slot
)))
226 (deftype test
:notype
() 'string
)
227 (define-condition test
:nocondition
(error)
228 ((slot :initform nil
:accessor test
:nocondition-slot
)))
231 (define-symbol-macro test
:nosymbol-macro
'foo
)
233 ;; declaration proclamation
234 (proclaim '(declaration test
:unused
))
237 (declaim (special test
:nospecial
))
238 (proclaim '(special test
:nospecial
))
241 (declaim (type fixnum test
:num
))
242 (proclaim '(type fixnum test
:num
))
245 (declaim (ftype (function (fixnum) fixnum
) test
:numfun
))
246 (proclaim '(ftype (function (fixnum) fixnum
) test
:numfun
))
249 (defsetf test
:car rplaca
) ; strictly speaking wrong, but ok as a test
250 (defsetf test
:car
(cons) (new-car)
251 `(setf (car ,cons
) ,new-car
))
252 (define-setf-expander test
:car
(place)
253 (multiple-value-bind (dummies vals newval setter getter
)
254 (get-setf-expansion place
)
255 (declare (ignore newval setter
))
256 (let ((store (gensym)))
260 `(progn (rplaca ,getter
,store
) ,store
)
263 ;; setf function names
264 (defun (setf test
:function
) (obj)
266 (tmp-fmakunbound '(setf test
:cdr
))
268 ;; define-method-combination
269 (define-method-combination test
:unused
)
272 (setf (find-class 'test
:class
) (find-class 'standard-class
))
275 ;;; Forms that cause violations on two distinct packages.
276 (defvar *illegal-double-forms
*
277 '((defclass test
:noclass
() ((x :accessor test-aux
:noslot
)))
278 (define-condition test
:nocondition
(error)
279 ((x :accessor test-aux
:noslot2
)))))
281 ;;; A collection of forms that cause compile-time package lock
282 ;;; violations on TEST, and will not signal an error on LOAD if first
283 ;;; compiled by COMPILE-FILE with test unlocked. CAR is the affected
284 ;;; symbol, CDR the form affecting it.
285 (defvar *illegal-lexical-forms-alist
*
288 ;; binding as a function
289 (test:function .
(flet ((test:function
() :shite
))
291 (test:function .
(labels ((test:function
() :shite
))
293 (test:macro .
(flet ((test:macro
() :shite
))
295 (test:macro .
(labels ((test:macro
() :shite
))
299 (test:function .
(macrolet ((test:function
() :yuk
))
301 (test:macro .
(macrolet ((test:macro
() :yuk
))
305 (test:function .
(flet (((setf test
:function
) (obj)
307 (setf (test:function
) 1)))
311 ;; The legacy interpreter doesn't do anything with ftype declarations
312 #+(or :sb-fasteval
(not :interpreter
))
313 (test:function .
(locally
314 (declare (ftype function test
:function
))
319 ;; Nor with type declarations
320 #+(or :sb-fasteval
(not :interpreter
))
322 (declare (type fixnum test
:num
))
326 (test:nospecial .
(locally
327 (declare (special test
:nospecial
))
331 #+(or :sb-fasteval
(not :interpreter
))
332 (test:numfun .
(locally
333 (declare (ftype (function (fixnum) fixnum
) test
:numfun
))
336 (defvar *illegal-lexical-forms
*
337 (mapcar #'cdr
*illegal-lexical-forms-alist
*))
339 (defvar *illegal-forms
* (append *illegal-runtime-forms
*
340 *illegal-lexical-forms
*
341 *illegal-double-forms
*))
343 ;;;; Running the tests
345 ;;; Unlocked. No errors nowhere.
348 (with-test (:name
:unlocked-package
)
349 (dolist (form (append *legal-forms
* *illegal-forms
*))
350 (with-error-info ("Unlocked form: ~S~%" form
)
353 ;;; Locked. Errors for all illegal forms, none for legal.
356 (with-test (:name
:locked-package
/legal-forms
)
357 (dolist (form *legal-forms
*)
358 (with-error-info ("locked legal form: ~S~%" form
)
361 (with-test (:name
:locked-package
/illegal-runtime-forms
)
362 (dolist (form (remove 'declaim
(append *illegal-runtime-forms
*
363 *illegal-double-forms
*)
365 (with-error-info ("locked illegal runtime form: ~S~%" form
)
366 (let ((fun (checked-compile `(lambda () ,form
))))
367 (assert-error (funcall fun
) sb-ext
:package-lock-violation
))
368 (assert-error (eval form
) sb-ext
:package-lock-violation
))))
370 (with-test (:name
:locked-package
/illegal-lexical-forms
)
371 (loop :for
(nil . form
) :in
*illegal-lexical-forms-alist
* :do
372 (with-error-info ("compile locked illegal lexical form: ~S~%" form
)
373 (let ((fun (checked-compile `(lambda () ,form
)
375 :allow-warnings
'simple-warning
)))
376 (assert-error (funcall fun
) program-error
))
377 (assert-error (let ((*error-output
* (make-broadcast-stream)))
379 ;; Let's not be pedantic here.
380 ;; PACKAGE-LOCK-VIOLATION is right,
381 ;; because the distinction between lexical analysis
382 ;; and running is artificial for interpreted code.
383 (or sb-ext
:package-lock-violation program-error
)))))
385 ;;; Locked, WITHOUT-PACKAGE-LOCKS
388 (with-test (:name
(sb-ext:without-package-locks
:locked-package
:illegal-runtime-forms
))
389 (dolist (form (remove 'declaim
*illegal-runtime-forms
* :key
#'first
))
390 (with-error-info ("without-package-locks illegal runtime form: ~S~%" form
)
391 (funcall (checked-compile `(lambda () (without-package-locks ,form
)))))))
393 (with-test (:name
(sb-ext:without-package-locks
:locked-package
:illegal-lexical-forms
))
394 (dolist (form *illegal-lexical-forms
*)
395 (let ((fun (without-package-locks (checked-compile `(lambda () ,form
)))))
397 (without-package-locks (eval form
))))
399 ;;; Locked, DISABLE-PACKAGE-LOCKS
402 (dolist (pair *illegal-lexical-forms-alist
*)
403 (destructuring-bind (sym . form
) pair
404 (with-error-info ("disable-package-locks on illegal form: ~S~%"
406 (funcall (checked-compile `(lambda ()
407 (declare (disable-package-locks ,sym
))
410 (declare (disable-package-locks ,sym
))
413 ;;; Locked, one error per "lexically apparent violated package", also
417 (with-test (:name
:illegal-runtime-forms
)
418 (dolist (form *illegal-runtime-forms
*)
419 (with-error-info ("one error per form ~S~%" form
)
421 (handler-bind ((package-lock-violation (lambda (e)
423 (error "multiple errors ~%~a~% and ~%~a"
429 (with-test (:name
:illegal-double-forms
)
430 (dolist (form *illegal-double-forms
*)
431 (with-error-info ("two errors per form: ~S~%" form
)
432 (let ((error-count 0))
433 ;; check that we don't get multiple errors from a single form
434 (handler-bind ((package-lock-violation (lambda (x)
435 (declare (ignorable x
))
439 (unless (= 2 error-count
)
440 (error "expected 2 errors per form, got ~A for ~A"
441 error-count form
)))))))
443 ;;; COMPILE-FILE when unlocked, LOAD locked -- *illegal-runtime-forms* only
445 ;;; This is not part of the interface, but it is the behaviour we want
446 (with-test (:name
(compile-file load
:locked-package
))
447 (let* ((tmp "package-locks.tmp.lisp")
448 (fasl (compile-file-pathname tmp
)))
449 (dolist (form *illegal-runtime-forms
*)
451 (with-simple-restart (next "~S failed, continue with next test" form
)
453 (with-open-file (f tmp
:direction
:output
)
455 (multiple-value-bind (file warnings failure-p
) (compile-file tmp
)
456 (declare (ignore file warnings failure-p
))
458 (assert-error (load fasl
) sb-ext
:package-lock-violation
)))
459 (when (probe-file tmp
)
461 (when (probe-file fasl
)
462 (delete-file fasl
))))))
464 ;;;; Tests for enable-package-locks declarations
467 (with-test (:name
(sb-ext:enable-package-locks
))
468 (loop :for
(sym . form
) :in
*illegal-lexical-forms-alist
* :do
469 (let ((fun (checked-compile
471 (declare (disable-package-locks ,sym
))
473 (locally (declare (enable-package-locks ,sym
))
476 :allow-warnings
'simple-warning
)))
477 (assert-error (funcall fun
) program-error
))
479 (let ((*error-output
* (make-broadcast-stream)))
480 (eval `(locally (declare (disable-package-locks ,sym
))
482 (locally (declare (enable-package-locks ,sym
))
484 (or sb-ext
:package-lock-violation program-error
))))
486 ;;;; See that trace on functions in locked packages doesn't break
488 (assert (trace test
:function
:break t
))
489 (untrace test
:function
)
491 ;;;; No bogus violations from defclass with accessors in a locked
492 ;;;; package. Reported by by Francois-Rene Rideau.
493 (with-test (:name
(defclass :accessor
:package-locked
))
494 (assert (package-locked-p :sb-gray
))
495 (let ((fun (checked-compile
497 (defclass fare-class
()
498 ((line-column :initform
0 :reader sb-gray
:stream-line-column
)))))))
499 (multiple-value-bind (class run-errors
) (ignore-errors (funcall fun
))
500 (assert (not run-errors
))
501 (assert (eq class
(find-class 'fare-class
))))))
503 ;;;; No bogus violations from DECLARE's done by PCL behind the
504 ;;;; scenes. Reported by David Wragg on sbcl-help.
507 (defmethod pcl-type-declaration-method-bug ((test:*special
* stream
))
509 (assert (eq *terminal-io
* (pcl-type-declaration-method-bug *terminal-io
*)))
511 ;; Interpreters don't walk into a method body until it's executed.
515 '(defmethod pcl-type-declaration-method-bug ((test:*special
* stream
))
516 (declare (type stream test
:*special
*))
520 ;;; Bogus package lock violations from LOOP
522 (with-test (:name
(loop :bogus sb-ext
:package-lock-violation
))
523 (assert (equal (loop :for
*print-base
* :from
2 :to
3 :collect
*print-base
*)
526 ;;; Package lock for DEFMACRO -> DEFUN and vice-versa.
528 (with-test (:name
:bug-576637
)
529 (assert-error (eval `(defun test:macro
(x) x
))
530 sb-ext
:package-lock-violation
)
531 (assert (eq 'test
:macro
(eval `(test:macro
))))
532 (assert-error (eval `(defmacro test
:function
(x) x
))
533 sb-ext
:package-lock-violation
)
534 (assert (eq 'test
:function
(eval `(test:function
)))))
536 (defpackage :macro-killing-macro-1
539 (:export
#:to-die-for
))
541 (defpackage :macro-killing-macro-2
542 (:use
:cl
:macro-killing-macro-1
))
545 `((in-package :macro-killing-macro-1
)
546 (defmacro to-die-for
()
550 (with-test (:name
:defmacro-killing-macro
)
553 `((in-package :macro-killing-macro-2
)
554 (defmacro to-die-for
()
556 (assert (eq :original
(macroexpand '(macro-killing-macro-1:to-die-for
)))))
558 (with-test (:name
:setf-macro-function-killing-macro
)
561 `((in-package :macro-killing-macro-2
)
562 (eval-when (:compile-toplevel
)
563 (setf (macro-function 'to-die-for
) (constantly :replacement2
))))))
564 (assert (eq :original
(macroexpand '(macro-killing-macro-1:to-die-for
)))))
566 (with-test (:name
:compile-time-defun-package-locked
)
567 ;; Make sure compile-time side-effects of DEFUN are protected against.
568 (let ((inline-lambda (function-lambda-expression #'fill-pointer
)))
569 ;; Make sure it's actually inlined...
570 (assert inline-lambda
)
573 (ctu:file-compile
`((defun fill-pointer (x) x
)))
574 (sb-ext:symbol-package-locked-error
(e)
575 (when (eq 'fill-pointer
576 (sb-ext:package-locked-error-symbol e
))
578 (assert (equal inline-lambda
579 (function-lambda-expression #'fill-pointer
)))))
581 (with-test (:name
:compile-time-defclass-package-locked
)
582 ;; Compiling (DEFCLASS FTYPE ...) used to break SBCL, but the package
583 ;; locks didn't kick in till later.
586 (ctu:file-compile
`((defclass ftype
() ())))
587 (sb-ext:symbol-package-locked-error
(e)
588 (when (eq 'ftype
(sb-ext:package-locked-error-symbol e
))
590 ;; Check for accessor violations as well.
593 (ctu:file-compile
`((defclass foo
() ((ftype :reader ftype
)))))
594 (sb-ext:symbol-package-locked-error
(e)
595 (when (eq 'ftype
(sb-ext:package-locked-error-symbol e
))
598 (with-test (:name
:assert-symbol-home-package-unlocked
)
599 (assert-error ; TODO use assert-signals
600 (sb-impl::assert-symbol-home-package-unlocked
601 'cl
:cons
"trying to foo ~S")
602 symbol-package-locked-error
)
604 (sb-impl::assert-symbol-home-package-unlocked
605 'cl
:cons
"trying to ~*~S ~2:*~A~* as a ~S"
607 symbol-package-locked-error
))
609 (with-test (:name
:defcostant-locks
)
610 (assert-error (defconstant test
:constant
100)
611 symbol-package-locked-error
))
613 (with-test (:name
:defstruct-compile-time-locks
)
614 (assert-error (ctu:file-compile
615 `((defstruct test
:nostruct
)))
616 symbol-package-locked-error
)
617 (assert-error (ctu:file-compile
618 `((defstruct (a-struct-test.1
621 symbol-package-locked-error
)
622 (assert-error (ctu:file-compile
623 `((defstruct (a-struct-test.2
624 (:predicate test
:nostruct
)))))
625 symbol-package-locked-error
)
626 (assert-error (ctu:file-compile
627 `((defstruct (a-struct-test.3
628 (:copier test
:nostruct
)))))
629 symbol-package-locked-error
)
630 (assert-error (ctu:file-compile
631 `((defstruct (a-struct-test.4
632 (:constructor test
:nostruct
)))))
633 symbol-package-locked-error
))