1 ;;;; This file contains code which knows about both the type
2 ;;;; representation and the compiler IR1 representation. This stuff is
3 ;;;; used for doing type checking.
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
19 ;;; These are the functions that are to be called when a problem is
20 ;;; detected. They are passed format arguments. If null, we don't do
21 ;;; anything. The error function is called when something is
22 ;;; definitely incorrect. The warning function is called when it is
23 ;;; somehow impossible to tell whether the call is correct.
24 (defvar *error-function*)
25 (defvar *warning-function*)
27 ;;; The function that we use for type checking. The derived type is
28 ;;; the first argument and the type we are testing against is the
29 ;;; second argument. The function should return values like CSUBTYPEP.
30 (defvar *test-function*)
31 ;;; FIXME: Why is this a variable? Explain.
33 (declaim (type (or function null) *error-function* *warning-function
36 ;;; *LOSSAGE-DETECTED* is set when a definite incompatibility is
37 ;;; detected. *SLIME-DETECTED* is set when we can't tell whether the
38 ;;; call is compatible or not.
39 (defvar *lossage-detected*)
40 (defvar *slime-detected*)
41 ;;; FIXME: SLIME is vivid and concise, but "DEFINITE-CALL-LOSSAGE" and
42 ;;; "POSSIBLE-CALL-LOSSAGE" would be more mnemonic.
44 ;;; Signal a warning if appropriate and set *LOSSAGE-DETECTED*.
45 (declaim (ftype (function (string &rest t) (values)) note-lossage note-slime))
46 (defun note-lossage (format-string &rest format-args)
47 (setq *lossage-detected* t)
48 (when *error-function*
49 (apply *error-function* format-string format-args))
51 (defun note-slime (format-string &rest format-args)
52 (setq *slime-detected* t)
53 (when *warning-function*
54 (apply *warning-function* format-string format-args))
57 (declaim (special *compiler-error-context*))
59 ;;;; stuff for checking a call against a function type
61 ;;;; FIXME: This is stuff to look at when I get around to fixing
62 ;;;; function type inference and declarations.
64 ;;; A dummy version of SUBTYPEP useful when we want a functional like
65 ;;; subtypep that always returns true.
66 (defun always-subtypep (type1 type2)
67 (declare (ignore type1 type2))
70 ;;; Determine whether a use of a function is consistent with its type.
71 ;;; These values are returned:
72 ;;; T, T: the call is definitely valid.
73 ;;; NIL, T: the call is definitely invalid.
74 ;;; NIL, NIL: unable to determine whether the call is valid.
76 ;;; The Argument-Test function is used to determine whether an
77 ;;; argument type matches the type we are checking against. Similarly,
78 ;;; the Result-Test is used to determine whether the result type
79 ;;; matches the specified result.
81 ;;; Unlike the argument test, the result test may be called on values
82 ;;; or function types. If Strict-Result is true and safety is
83 ;;; non-zero, then the Node-Derived-Type is always used. Otherwise, if
84 ;;; Cont's Type-Check is true, then the Node-Derived-Type is
85 ;;; intersected with the Cont's Asserted-Type.
87 ;;; The error and warning functions are functions that are called to
88 ;;; explain the result. We bind *compiler-error-context* to the
89 ;;; combination node so that Compiler-Warning and related functions
90 ;;; will do the right thing if they are supplied.
91 (defun valid-function-use (call type &key
92 ((:argument-test *test-function*) #'csubtypep)
93 (result-test #'values-subtypep)
95 ((:error-function *error-function*))
96 ((:warning-function *warning-function*)))
97 (declare (type function result-test) (type combination call)
98 (type function-type type))
99 (let* ((*lossage-detected* nil)
100 (*slime-detected* nil)
101 (*compiler-error-context* call)
102 (args (combination-args call))
103 (nargs (length args))
104 (required (function-type-required type))
105 (min-args (length required))
106 (optional (function-type-optional type))
107 (max-args (+ min-args (length optional)))
108 (rest (function-type-rest type))
109 (keyp (function-type-keyp type)))
112 ((function-type-wild-args type)
114 (arg args (cdr arg)))
116 (check-arg-type (car arg) *wild-type* i)))
117 ((not (or optional keyp rest))
118 (if (/= nargs min-args)
120 "The function was called with ~R argument~:P, but wants exactly ~R."
122 (check-fixed-and-rest args required nil)))
125 "The function was called with ~R argument~:P, but wants at least ~R."
128 (check-fixed-and-rest args (append required optional) rest))
129 ((not (or keyp rest))
131 "The function was called with ~R argument~:P, but wants at most ~R."
133 ((and keyp (oddp (- nargs max-args)))
135 "The function has an odd number of arguments in the keyword portion."))
137 (check-fixed-and-rest args (append required optional) rest)
139 (check-keywords args max-args type))))
141 (let* ((dtype (node-derived-type call))
142 (return-type (function-type-returns type))
143 (cont (node-cont call))
145 (if (or (not (continuation-type-check cont))
146 (and strict-result (policy call (/= safety 0))))
148 (values-type-intersection (continuation-asserted-type cont)
150 (multiple-value-bind (int win) (funcall result-test out-type return-type)
152 (note-slime "can't tell whether the result is a ~S"
153 (type-specifier return-type)))
155 (note-lossage "The result is a ~S, not a ~S."
156 (type-specifier out-type)
157 (type-specifier return-type))))))
159 (cond (*lossage-detected* (values nil t))
160 (*slime-detected* (values nil nil))
163 ;;; Check that the derived type of the continuation Cont is compatible
164 ;;; with Type. N is the arg number, for error message purposes. We
165 ;;; return true if arg is definitely o.k. If the type is a magic
166 ;;; CONSTANT-TYPE, then we check for the argument being a constant
167 ;;; value of the specified type. If there is a manifest type error
168 ;;; (DERIVED-TYPE = NIL), then we flame about the asserted type even
169 ;;; when our type is satisfied under the test.
170 (defun check-arg-type (cont type n)
171 (declare (type continuation cont) (type ctype type) (type index n))
173 ((not (constant-type-p type))
174 (let ((ctype (continuation-type cont)))
175 (multiple-value-bind (int win) (funcall *test-function* ctype type)
177 (note-slime "can't tell whether the ~:R argument is a ~S" n
178 (type-specifier type))
181 (note-lossage "The ~:R argument is a ~S, not a ~S." n
182 (type-specifier ctype)
183 (type-specifier type))
185 ((eq ctype *empty-type*)
186 (note-slime "The ~:R argument never returns a value." n)
189 ((not (constant-continuation-p cont))
190 (note-slime "The ~:R argument is not a constant." n)
193 (let ((val (continuation-value cont))
194 (type (constant-type-type type)))
195 (multiple-value-bind (res win) (ctypep val type)
197 (note-slime "can't tell whether the ~:R argument is a ~
199 n (type-specifier type) val)
202 (note-lossage "The ~:R argument is not a constant ~S:~% ~S"
203 n (type-specifier type) val)
207 ;;; Check that each of the type of each supplied argument intersects
208 ;;; with the type specified for that argument. If we can't tell, then
209 ;;; we complain about the slime.
210 (declaim (ftype (function (list list (or ctype null)) (values)) check-fixed-and-rest))
211 (defun check-fixed-and-rest (args types rest)
212 (do ((arg args (cdr arg))
213 (type types (cdr type))
215 ((or (null type) (null arg))
218 (check-arg-type arg rest n)
221 (check-arg-type (car arg) (car type) n))
224 ;;; Check that the keyword args are of the correct type. Each keyword
225 ;;; should be known and the corresponding argument should be of the
226 ;;; correct type. If the keyword isn't a constant, then we can't tell,
227 ;;; so we note slime.
228 (declaim (ftype (function (list fixnum function-type) (values)) check-keywords))
229 (defun check-keywords (args pre-key type)
230 (do ((key (nthcdr pre-key args) (cddr key))
231 (n (1+ pre-key) (+ n 2)))
236 ((not (check-arg-type k (specifier-type 'symbol) n)))
237 ((not (constant-continuation-p k))
238 (note-slime "The ~:R argument (in keyword position) is not a constant."
241 (let* ((name (continuation-value k))
242 (info (find name (function-type-keywords type)
243 :key #'key-info-name)))
245 (unless (function-type-allowp type)
246 (note-lossage "~S is not a known argument keyword."
249 (check-arg-type (second key) (key-info-type info)
253 ;;; Construct a function type from a definition.
255 ;;; Due to the lack of a (LIST X) type specifier, we can't reconstruct
257 (declaim (ftype (function (functional) function-type) definition-type))
258 (defun definition-type (functional)
259 (if (lambda-p functional)
261 :required (mapcar #'leaf-type (lambda-vars functional))
262 :returns (tail-set-type (lambda-tail-set functional)))
267 (dolist (arg (optional-dispatch-arglist functional))
268 (let ((info (lambda-var-arg-info arg))
269 (type (leaf-type arg)))
271 (ecase (arg-info-kind info)
272 (:required (req type))
273 (:optional (opt type))
275 (keys (make-key-info :name (arg-info-keyword info)
277 ((:rest :more-context)
278 (setq rest *universal-type*))
287 :keyp (optional-dispatch-keyp functional)
288 :allowp (optional-dispatch-allowp functional)
289 :returns (tail-set-type
291 (optional-dispatch-main-entry functional))))))))
293 ;;;; approximate function types
295 ;;;; FIXME: This is stuff to look at when I get around to fixing function
296 ;;;; type inference and declarations.
298 ;;;; Approximate function types provide a condensed representation of all the
299 ;;;; different ways that a function has been used. If we have no declared or
300 ;;;; defined type for a function, then we build an approximate function type by
301 ;;;; examining each use of the function. When we encounter a definition or
302 ;;;; proclamation, we can check the actual type for compatibity with the
305 (defstruct (approximate-function-type)
306 ;; The smallest and largest numbers of arguments that this function has been
308 (min-args call-arguments-limit :type fixnum)
309 (max-args 0 :type fixnum)
310 ;; A list of lists of the all the types that have been used in each argument
312 (types () :type list)
313 ;; A list of the Approximate-Key-Info structures describing all the things
314 ;; that looked like keyword arguments. There are distinct structures
315 ;; describing each argument position in which the keyword appeared.
316 (keys () :type list))
318 (defstruct (approximate-key-info)
319 ;; The keyword name of this argument. Although keyword names don't have to
320 ;; be keywords, we only match on keywords when figuring an approximate type.
321 (name (required-argument) :type keyword)
322 ;; The position at which this keyword appeared. 0 if it appeared as the
323 ;; first argument, etc.
324 (position (required-argument) :type fixnum)
325 ;; A list of all the argument types that have been used with this keyword.
326 (types nil :type list)
327 ;; True if this keyword has appeared only in calls with an obvious
328 ;; :allow-other-keys.
329 (allowp nil :type (member t nil)))
331 ;;; Return an Approximate-Function-Type representing the context of
332 ;;; Call. If Type is supplied and not null, then we merge the
333 ;;; information into the information already accumulated in Type.
334 (declaim (ftype (function (combination
335 &optional (or approximate-function-type null))
336 approximate-function-type)
338 (defun note-function-use (call &optional type)
339 (let* ((type (or type (make-approximate-function-type)))
340 (types (approximate-function-type-types type))
341 (args (combination-args call))
342 (nargs (length args))
343 (allowp (some #'(lambda (x)
344 (and (constant-continuation-p x)
345 (eq (continuation-value x) :allow-other-keys)))
348 (setf (approximate-function-type-min-args type)
349 (min (approximate-function-type-min-args type) nargs))
350 (setf (approximate-function-type-max-args type)
351 (max (approximate-function-type-max-args type) nargs))
353 (do ((old types (cdr old))
354 (arg args (cdr arg)))
356 (setf (approximate-function-type-types type)
358 (mapcar #'(lambda (x)
359 (list (continuation-type x)))
361 (when (null arg) (return))
362 (pushnew (continuation-type (car arg))
366 (collect ((keys (approximate-function-type-keys type) cons))
367 (do ((arg args (cdr arg))
369 ((or (null arg) (null (cdr arg)))
370 (setf (approximate-function-type-keys type) (keys)))
371 (let ((key (first arg))
373 (when (constant-continuation-p key)
374 (let ((name (continuation-value key)))
375 (when (keywordp name)
378 (and (eq (approximate-key-info-name x) name)
379 (= (approximate-key-info-position x)
382 (val-type (continuation-type val)))
385 (approximate-key-info-types old)
388 (setf (approximate-key-info-allowp old) nil)))
390 (keys (make-approximate-key-info
394 :types (list val-type))))))))))))
397 ;;; Similar to Valid-Function-Use, but checks an
398 ;;; Approximate-Function-Type against a real function type.
399 (declaim (ftype (function (approximate-function-type function-type
400 &optional function function function)
401 (values boolean boolean))
402 valid-approximate-type))
403 (defun valid-approximate-type (call-type type &optional
404 (*test-function* #'types-intersect)
405 (*error-function* #'compiler-warning)
406 (*warning-function* #'compiler-note))
407 (let* ((*lossage-detected* nil)
408 (*slime-detected* nil)
409 (required (function-type-required type))
410 (min-args (length required))
411 (optional (function-type-optional type))
412 (max-args (+ min-args (length optional)))
413 (rest (function-type-rest type))
414 (keyp (function-type-keyp type)))
416 (when (function-type-wild-args type)
417 (return-from valid-approximate-type (values t t)))
419 (let ((call-min (approximate-function-type-min-args call-type)))
420 (when (< call-min min-args)
422 "Function previously called with ~R argument~:P, but wants at least ~R."
425 (let ((call-max (approximate-function-type-max-args call-type)))
426 (cond ((<= call-max max-args))
427 ((not (or keyp rest))
429 "Function previously called with ~R argument~:P, but wants at most ~R."
431 ((and keyp (oddp (- call-max max-args)))
433 "Function previously called with an odd number of arguments in ~
434 the keyword portion.")))
436 (when (and keyp (> call-max max-args))
437 (check-approximate-keywords call-type max-args type)))
439 (check-approximate-fixed-and-rest call-type (append required optional)
442 (cond (*lossage-detected* (values nil t))
443 (*slime-detected* (values nil nil))
446 ;;; Check that each of the types used at each arg position is
447 ;;; compatible with the actual type.
448 (declaim (ftype (function (approximate-function-type list (or ctype null))
450 check-approximate-fixed-and-rest))
451 (defun check-approximate-fixed-and-rest (call-type fixed rest)
452 (do ((types (approximate-function-type-types call-type) (cdr types))
454 (arg fixed (cdr arg)))
456 (let ((decl-type (or (car arg) rest)))
457 (unless decl-type (return))
458 (check-approximate-arg-type (car types) decl-type "~:R" n)))
461 ;;; Check that each of the call-types is compatible with Decl-Type,
462 ;;; complaining if not or if we can't tell.
463 (declaim (ftype (function (list ctype string &rest t) (values))
464 check-approximate-arg-type))
465 (defun check-approximate-arg-type (call-types decl-type context &rest args)
466 (let ((losers *empty-type*))
467 (dolist (ctype call-types)
468 (multiple-value-bind (int win) (funcall *test-function* ctype decl-type)
471 (note-slime "can't tell whether previous ~? argument type ~S is a ~S"
472 context args (type-specifier ctype) (type-specifier decl-type)))
474 (setq losers (type-union ctype losers))))))
476 (unless (eq losers *empty-type*)
477 (note-lossage "~:(~?~) argument should be a ~S but was a ~S in a previous call."
478 context args (type-specifier decl-type) (type-specifier losers))))
481 ;;; Check the types of each manifest keyword that appears in a keyword
482 ;;; argument position. Check the validity of all keys that appeared in
483 ;;; valid keyword positions.
485 ;;; ### We could check the Approximate-Function-Type-Types to make
486 ;;; sure that all arguments in keyword positions were manifest
488 (defun check-approximate-keywords (call-type max-args type)
489 (let ((call-keys (approximate-function-type-keys call-type))
490 (keys (function-type-keywords type)))
492 (let ((name (key-info-name key)))
493 (collect ((types nil append))
494 (dolist (call-key call-keys)
495 (let ((pos (approximate-key-info-position call-key)))
496 (when (and (eq (approximate-key-info-name call-key) name)
497 (> pos max-args) (evenp (- pos max-args)))
498 (types (approximate-key-info-types call-key)))))
499 (check-approximate-arg-type (types) (key-info-type key) "~S" name))))
501 (unless (function-type-allowp type)
502 (collect ((names () adjoin))
503 (dolist (call-key call-keys)
504 (let ((pos (approximate-key-info-position call-key)))
505 (when (and (> pos max-args) (evenp (- pos max-args))
506 (not (approximate-key-info-allowp call-key)))
507 (names (approximate-key-info-name call-key)))))
509 (dolist (name (names))
510 (unless (find name keys :key #'key-info-name)
511 (note-lossage "Function previously called with unknown argument keyword ~S."
514 ;;;; ASSERT-DEFINITION-TYPE
516 ;;; Intersect Lambda's var types with Types, giving a warning if there
517 ;;; is a mismatch. If all intersections are non-null, we return lists
518 ;;; of the variables and intersections, otherwise we return NIL, NIL.
519 (defun try-type-intersections (vars types where)
520 (declare (list vars types) (string where))
522 (mapc #'(lambda (var type)
523 (let* ((vtype (leaf-type var))
524 (int (type-intersection vtype type)))
526 ((eq int *empty-type*)
528 "Definition's declared type for variable ~A:~% ~S~@
529 conflicts with this type from ~A:~% ~S"
530 (leaf-name var) (type-specifier vtype)
531 where (type-specifier type))
532 (return-from try-type-intersections (values nil nil)))
536 (values vars (res))))
538 ;;; Check that the optional-dispatch OD conforms to Type. We return
539 ;;; the values of TRY-TYPE-INTERSECTIONS if there are no syntax
540 ;;; problems, otherwise NIL, NIL.
542 ;;; Note that the variables in the returned list are the actual
543 ;;; original variables (extracted from the optional dispatch arglist),
544 ;;; rather than the variables that are arguments to the main entry.
545 ;;; This difference is significant only for keyword args with hairy
546 ;;; defaults. Returning the actual vars allows us to use the right
547 ;;; variable name in warnings.
549 ;;; A slightly subtle point: with keywords and optionals, the type in
550 ;;; the function type is only an assertion on calls --- it doesn't
551 ;;; constrain the type of default values. So we have to union in the
552 ;;; type of the default. With optionals, we can't do any assertion
553 ;;; unless the default is constant.
555 ;;; With keywords, we exploit our knowledge about how hairy keyword
556 ;;; defaulting is done when computing the type assertion to put on the
557 ;;; main-entry argument. In the case of hairy keywords, the default
558 ;;; has been clobbered with NIL, which is the value of the main-entry
559 ;;; arg in the unsupplied case, whatever the actual default value is.
560 ;;; So we can just assume the default is constant, effectively
561 ;;; unioning in NULL, and not totally blow off doing any type
563 (defun find-optional-dispatch-types (od type where)
564 (declare (type optional-dispatch od) (type function-type type)
566 (let* ((min (optional-dispatch-min-args od))
567 (req (function-type-required type))
568 (opt (function-type-optional type)))
569 (flet ((frob (x y what)
572 "Definition has ~R ~A arg~P, but ~A has ~R."
574 (frob min (length req) "fixed")
575 (frob (- (optional-dispatch-max-args od) min) (length opt) "optional"))
576 (flet ((frob (x y what)
579 "Definition ~:[doesn't have~;has~] ~A, but ~
580 ~A ~:[doesn't~;does~]."
582 (frob (optional-dispatch-keyp od) (function-type-keyp type)
584 (unless (optional-dispatch-keyp od)
585 (frob (not (null (optional-dispatch-more-entry od)))
586 (not (null (function-type-rest type)))
588 (frob (optional-dispatch-allowp od) (function-type-allowp type)
589 "&allow-other-keys"))
591 (when *lossage-detected*
592 (return-from find-optional-dispatch-types (values nil nil)))
596 (let ((keys (function-type-keywords type))
597 (arglist (optional-dispatch-arglist od)))
598 (dolist (arg arglist)
600 ((lambda-var-arg-info arg)
601 (let* ((info (lambda-var-arg-info arg))
602 (default (arg-info-default info))
603 (def-type (when (constantp default)
604 (ctype-of (eval default)))))
605 (ecase (arg-info-kind info)
607 (let* ((key (arg-info-keyword info))
608 (kinfo (find key keys :key #'key-info-name)))
611 (res (type-union (key-info-type kinfo)
612 (or def-type (specifier-type 'null)))))
615 "Defining a ~S keyword not present in ~A."
617 (res *universal-type*)))))
618 (:required (res (pop req)))
620 (res (type-union (pop opt) (or def-type *universal-type*))))
622 (when (function-type-rest type)
623 (res (specifier-type 'list))))
625 (when (function-type-rest type)
626 (res *universal-type*)))
628 (when (function-type-rest type)
629 (res (specifier-type 'fixnum)))))
631 (when (arg-info-supplied-p info)
632 (res *universal-type*)
633 (vars (arg-info-supplied-p info)))))
639 (unless (find (key-info-name key) arglist
641 (let ((info (lambda-var-arg-info x)))
643 (arg-info-keyword info)))))
645 "Definition lacks the ~S keyword present in ~A."
646 (key-info-name key) where))))
648 (try-type-intersections (vars) (res) where))))
650 ;;; Check that Type doesn't specify any funny args, and do the
652 (defun find-lambda-types (lambda type where)
653 (declare (type clambda lambda) (type function-type type) (string where))
654 (flet ((frob (x what)
657 "Definition has no ~A, but the ~A did."
659 (frob (function-type-optional type) "optional args")
660 (frob (function-type-keyp type) "keyword args")
661 (frob (function-type-rest type) "rest arg"))
662 (let* ((vars (lambda-vars lambda))
663 (nvars (length vars))
664 (req (function-type-required type))
666 (unless (= nvars nreq)
667 (note-lossage "Definition has ~R arg~:P, but the ~A has ~R."
669 (if *lossage-detected*
671 (try-type-intersections vars req where))))
673 ;;; Check for syntactic and type conformance between the definition
674 ;;; Functional and the specified Function-Type. If they are compatible
675 ;;; and Really-Assert is T, then add type assertions to the definition
676 ;;; from the Function-Type.
678 ;;; If there is a syntactic or type problem, then we call
679 ;;; Error-Function with an error message using Where as context
680 ;;; describing where Function-Type came from.
682 ;;; If there is no problem, we return T (even if Really-Assert was
683 ;;; false). If there was a problem, we return NIL.
684 (defun assert-definition-type
685 (functional type &key (really-assert t)
686 ((:error-function *error-function*) #'compiler-warning)
688 (where "previous declaration"))
689 (declare (type functional functional)
690 (type function *error-function*)
692 (unless (function-type-p type) (return-from assert-definition-type t))
693 (let ((*lossage-detected* nil))
694 (multiple-value-bind (vars types)
695 (if (function-type-wild-args type)
697 (etypecase functional
699 (find-optional-dispatch-types functional type where))
701 (find-lambda-types functional type where))))
702 (let* ((type-returns (function-type-returns type))
703 (return (lambda-return (main-entry functional)))
705 (continuation-asserted-type (return-result return)))))
707 ((and atype (not (values-types-intersect atype type-returns)))
709 "The result type from ~A:~% ~S~@
710 conflicts with the definition's result type assertion:~% ~S"
711 where (type-specifier type-returns) (type-specifier atype))
713 (*lossage-detected* nil)
714 ((not really-assert) t)
717 (assert-continuation-type (return-result return) atype))
718 (loop for var in vars and type in types do
719 (cond ((basic-var-sets var)
720 (when (and warning-function
721 (not (csubtypep (leaf-type var) type)))
722 (funcall warning-function
723 "Assignment to argument: ~S~% ~
724 prevents use of assertion from function ~
726 (leaf-name var) where (type-specifier type))))
728 (setf (leaf-type var) type)
729 (dolist (ref (leaf-refs var))
730 (derive-node-type ref type)))))