From: William Harold Newman Date: Wed, 14 Aug 2002 18:22:57 +0000 (+0000) Subject: 0.7.6.23: X-Git-Url: http://repo.macrolet.net/gitweb/?a=commitdiff_plain;h=f5133ab2ffcddbcdb330cbbceff3af8d66673ce8;p=sbcl.git 0.7.6.23: merged APD patch for bugs 191-b (making gf lambda lists conform to ANSI 3.4.2) from sbcl-devel added tests, and fixed old tests whose brokenness is now detected (speculated about reusing SB-C:PARSE-LAMBDA-LIST instead of adding new PROCESS-LAMBDA-LIST, but didn't actually actually do that, in favor of checking in something that works and fixes a bug. Maybe next version...) reverted handling of illegal function name in DEFGENERIC (from previous commit) since I guess it should be PROGRAM-ERROR after all --- diff --git a/BUGS b/BUGS index af81ba1..103f490 100644 --- a/BUGS +++ b/BUGS @@ -1438,11 +1438,6 @@ WORKAROUND: results in a STYLE-WARNING: undefined-function SB-SLOT-ACCESSOR-NAME::|COMMON-LISP-USER A-CLASS-X slot READER| - b. DEFGENERIC does not check lambda list syntax; from the REPL: - * (defgeneric gf ("a" #p"b")) - - # - * c. the examples in CLHS 7.6.5.1 (regarding generic function lambda lists and &KEY arguments) do not signal errors when they should. diff --git a/NEWS b/NEWS index 29b3762..a18fd98 100644 --- a/NEWS +++ b/NEWS @@ -1221,6 +1221,8 @@ changes in sbcl-0.7.7 relative to sbcl-0.7.6: LOGAND and stack-allocated arguments. (thanks to Raymond Toy) * Bug fix: We no longer segfault on passing a non-FILE-STREAM stream to a functions expecting a PATHNAME-DESIGNATOR. + * Bug fix: DEFGENERIC now enforces the ANSI restrictions on its + lambda lists. (thanks to Alexey Dejneka) * Minor incompatible change: COMPILE-FILE-PATHNAME now merges its OUTPUT-FILE argument with its INPUT-FILE argument, resulting in behaviour analogous to RENAME-FILE. This puts its behaviour more diff --git a/src/code/class.lisp b/src/code/class.lisp index 0733b25..5646967 100644 --- a/src/code/class.lisp +++ b/src/code/class.lisp @@ -938,7 +938,10 @@ :state :read-only) ;; FIXME: Are COLLECTION and MUTABLE-COLLECTION used for anything - ;; any more? COLLECTION is not defined in ANSI Common Lisp.. + ;; any more? COLLECTION is not defined in ANSI Common Lisp. + ;; (Zhivago in #lisp IRC 2002-08-14 said that these things, and + ;; also GENERIC-FOO, are related to Dylan functionality and so + ;; truly are irrelevant these days.) (collection :hierarchical-p nil :state :read-only) (mutable-collection :state :read-only :inherits (collection)) diff --git a/src/code/describe.lisp b/src/code/describe.lisp index 35cd265..b937104 100644 --- a/src/code/describe.lisp +++ b/src/code/describe.lisp @@ -13,7 +13,7 @@ (in-package "SB-IMPL") ;(SB-IMPL, not SB!IMPL, since we're built in warm load.) (declaim (ftype (function (t stream)) describe-object)) -(defgeneric describe-object ((x t) stream)) +(defgeneric describe-object (x stream)) (defun describe (x &optional (stream-designator *standard-output*)) #+sb-doc @@ -209,7 +209,7 @@ (defmethod describe-object ((x function) s) (%describe-fun x s :function)) -(defgeneric describe-symbol-fdefinition (function stream &key (name nil) )) +(defgeneric describe-symbol-fdefinition (function stream &key name)) (defmethod describe-symbol-fdefinition ((fun function) stream &key name) (%describe-fun fun stream :function name)) diff --git a/src/code/profile.lisp b/src/code/profile.lisp index 837ff7e..20a3c03 100644 --- a/src/code/profile.lisp +++ b/src/code/profile.lisp @@ -222,9 +222,7 @@ (etypecase name (symbol (funcall function name)) (list - ;; We call this just for the side effect of checking that - ;; NAME is a legal function name: - (fun-name-block-name name) + (legal-fun-name-or-type-error name) ;; Then we map onto it. (funcall function name)) (string (let ((package (find-undeleted-package-or-lose name))) diff --git a/src/pcl/boot.lisp b/src/pcl/boot.lisp index 3e51cf8..b9633df 100644 --- a/src/pcl/boot.lisp +++ b/src/pcl/boot.lisp @@ -157,7 +157,11 @@ bootstrapping. (defmacro defgeneric (fun-name lambda-list &body options) (declare (type list lambda-list)) - (legal-fun-name-or-type-error fun-name) + (unless (legal-fun-name-p fun-name) + (error 'simple-program-error + :format-control "illegal generic function name ~S" + :format-arguments (list fun-name))) + (check-gf-lambda-list lambda-list) (let ((initargs ()) (methods ())) (flet ((duplicate-option (name) @@ -228,6 +232,29 @@ bootstrapping. :lambda-list lambda-list :definition-source `((defgeneric ,fun-name) ,*load-truename*) initargs)) + +;;; As per section 3.4.2 of the ANSI spec, generic function lambda +;;; lists have a number of limitations, which we check here. +(defun check-gf-lambda-list (lambda-list) + (macrolet ((ensure (condition) + `(unless ,condition + (error "Invalid argument ~S in the generic function lambda list ~S." + it lambda-list)))) + (process-lambda-list lambda-list + (&required (ensure (symbolp it))) + (&optional (ensure (or (symbolp it) + (and (consp it) (symbolp (car it)) (null (cdr it)))))) + (&rest (ensure (symbolp it))) + (&key (ensure (or (symbolp it) + (and (consp it) + (or (symbolp (car it)) + (and (consp (car it)) + (symbolp (caar it)) + (symbolp (cadar it)) + (null (cddar it)))) + (null (cdr it)))))) + ((&aux (error "&AUX is not allowed in the generic function lambda list ~S." + lambda-list)))))) (defmacro defmethod (&rest args &environment env) (multiple-value-bind (name qualifiers lambda-list body) diff --git a/src/pcl/generic-functions.lisp b/src/pcl/generic-functions.lisp index 14916d4..bca7464 100644 --- a/src/pcl/generic-functions.lisp +++ b/src/pcl/generic-functions.lisp @@ -440,12 +440,12 @@ (defgeneric get-method (generic-function qualifiers specializers - &optional (errorp t))) + &optional errorp)) (defgeneric find-method (generic-function qualifiers specializers - &optional (errorp t))) + &optional errorp)) (defgeneric remove-named-method (generic-function-name argument-specifiers diff --git a/src/pcl/gray-streams.lisp b/src/pcl/gray-streams.lisp index 02a3a06..c09524a 100644 --- a/src/pcl/gray-streams.lisp +++ b/src/pcl/gray-streams.lisp @@ -288,7 +288,7 @@ (defmethod stream-start-line-p ((stream fundamental-character-output-stream)) (eql (stream-line-column stream) 0)) -(defgeneric stream-write-string (stream string &optional (start 0) end) +(defgeneric stream-write-string (stream string &optional start end) #+sb-doc (:documentation "This is used by WRITE-STRING. It writes the string to the stream, diff --git a/src/pcl/macros.lisp b/src/pcl/macros.lisp index ef82f3c..e2fc870 100644 --- a/src/pcl/macros.lisp +++ b/src/pcl/macros.lisp @@ -255,5 +255,67 @@ `(setf ,name)) (defsetf slot-value set-slot-value) + +(defun misplaced-lambda-list-keyword (lambda-list keyword) + (error "Lambda list keyword ~S is misplaced in ~S." keyword lambda-list)) + +(defmacro process-lambda-list (lambda-list &rest clauses) + ;; (process-lambda-list '(a b &optional (c 1)) + ;; (&required) + ;; ((&optional (print "Started processing optional arguments")) + ;; (format "Optional argument: ~S~%" it)) + ;; (&rest (print "Rest"))) + (let ((clauses (loop for clause in clauses + collect + (cond ((symbolp (car clause)) + `(,(car clause) nil . ,(cdr clause))) + ((consp (car clause)) + `(,(caar clause) ,(cdar clause) . ,(cdr clause))) + (t (error "Invalid clause format: ~S." clause))))) + (ll (gensym "LL")) + (state (gensym "STATE")) + (restp (gensym "RESTP")) + (check-state (gensym "CHECK-STATE"))) + `(let ((,ll ,lambda-list) + (,state '&required) + (,restp nil)) + (dolist (it ,ll) + (flet ((,check-state (possible) + (unless (memq ,state possible) + (misplaced-lambda-list-keyword ,ll it)))) + (cond ((memq it lambda-list-keywords) + (case it + (&optional (,check-state '(&required)) + ,@(cadr (assoc '&optional clauses))) + (&rest (,check-state '(&required &optional)) + ,@(cadr (assoc '&rest clauses))) + (&key (,check-state '(&required &optional &rest)) + (when (and (eq ,state '&rest) + (not ,restp)) + (error "Omitted &REST variable in ~S." ,ll)) + ,@(cadr (assoc '&key clauses))) + (&allow-other-keys (,check-state '(&key)) + ,@(cadr (assoc '&allow-other-keys clauses))) + (&aux (when (and (eq ,state '&rest) + (not ,restp)) + (error "Omitted &REST variable in ~S." ,ll)) + ,@(cadr (assoc '&aux clauses))) + (t (error "Unsupported lambda list keyword ~S in ~S." + it ,ll))) + (setq ,state it)) + (t (case ,state + (&required ,@(cddr (assoc '&required clauses))) + (&optional ,@(cddr (assoc '&optional clauses))) + (&rest (when ,restp + (error "Too many variables after &REST in ~S." ,ll)) + (setq ,restp t) + ,@(cddr (assoc '&rest clauses))) + (&key ,@(cddr (assoc '&key clauses))) + (&allow-other-keys (error "Variable ~S after &ALLOW-OTHER-KEY in ~S." + it ,ll)) + (&aux ,@(cddr (assoc '&aux clauses)))))))) + (when (and (eq ,state '&rest) + (not ,restp)) + (error "Omitted &REST variable in ~S." ,ll))))) (/show "finished with pcl/macros.lisp") diff --git a/tests/clos.impure.lisp b/tests/clos.impure.lisp index ab0e2b4..eb3c5d4 100644 --- a/tests/clos.impure.lisp +++ b/tests/clos.impure.lisp @@ -22,7 +22,7 @@ (defmethod wiggle ((a struct-a)) (+ (struct-a-x a) (struct-a-y a))) -(defgeneric jiggle ((arg t))) +(defgeneric jiggle (arg)) (defmethod jiggle ((a struct-a)) (- (struct-a-x a) (struct-a-y a))) @@ -35,7 +35,7 @@ ;;; Compiling DEFGENERIC should prevent "undefined function" style ;;; warnings from code within the same file. -(defgeneric gf-defined-in-this-file ((x number) (y number))) +(defgeneric gf-defined-in-this-file (x y)) (defun function-using-gf-defined-in-this-file (x y n) (unless (minusp n) (gf-defined-in-this-file x y))) @@ -61,22 +61,71 @@ (ignore-errors (progn ,@body)) (declare (ignore res)) (typep condition 'error)))) - (assert (expect-error (macroexpand-1 '(defmethod foo0 ((x t) &rest) nil)))) - (assert (expect-error (defgeneric foo1 (x &rest)))) (assert (expect-error (defgeneric foo2 (x a &rest)))) - (defgeneric foo3 (x &rest y)) (defmethod foo3 ((x t) &rest y) nil) (defmethod foo4 ((x t) &key y &rest z) nil) -(defgeneric foo4 (x &key y &rest z)) - +(defgeneric foo4 (x &rest z &key y)) (assert (expect-error (defgeneric foo5 (x &rest)))) (assert (expect-error (macroexpand-1 '(defmethod foo6 (x &rest))))) +;;; more lambda-list checking +;;; +;;; DEFGENERIC lambda lists are subject to various limitations, as per +;;; section 3.4.2 of the ANSI spec. Since Alexey Dejneka's patch for +;;; bug 191-b ca. sbcl-0.7.22, these limitations should be enforced. +(labels ((coerce-to-boolean (x) + (if x t nil)) + (%like-or-dislike (expr expected-failure-p) + (declare (type boolean expected-failure-p)) + (format t "~&trying ~S~%" expr) + (multiple-value-bind (fun warnings-p failure-p) + (compile nil + `(lambda () + ,expr)) + (declare (ignore fun)) + ;; In principle the constraint on WARNINGS-P below seems + ;; reasonable, but in practice we get warnings about + ;; undefined functions from the DEFGENERICs, apparently + ;; because the DECLAIMs which ordinarily prevent such + ;; warnings don't take effect because EVAL-WHEN + ;; (:COMPILE-TOPLEVEL) loses its magic when compiled + ;; within a LAMBDA. So maybe we can't test WARNINGS-P + ;; after all? + ;;(unless expected-failure-p + ;; (assert (not warnings-p))) + (assert (eq (coerce-to-boolean failure-p) expected-failure-p)))) + (like (expr) + (%like-or-dislike expr nil)) + (dislike (expr) + (%like-or-dislike expr t))) + ;; basic sanity + (dislike '(defgeneric gf-for-ll-test-0 ("a" #p"b"))) + (like '(defgeneric gf-for-ll-test-1 ())) + (like '(defgeneric gf-for-ll-test-2 (x))) + ;; forbidden default or supplied-p for &OPTIONAL or &KEY arguments + (dislike '(defgeneric gf-for-ll-test-3 (x &optional (y 0)))) + (like '(defgeneric gf-for-ll-test-4 (x &optional y))) + (dislike '(defgeneric gf-for-ll-test-5 (x y &key (z :z z-p)))) + (like '(defgeneric gf-for-ll-test-6 (x y &key z))) + (dislike '(defgeneric gf-for-ll-test-7 (x &optional (y 0) &key z))) + (like '(defgeneric gf-for-ll-test-8 (x &optional y &key z))) + (dislike '(defgeneric gf-for-ll-test-9 (x &optional y &key (z :z)))) + (like '(defgeneric gf-for-ll-test-10 (x &optional y &key z))) + (dislike '(defgeneric gf-for-ll-test-11 (&optional &key (k :k k-p)))) + (like '(defgeneric gf-for-ll-test-12 (&optional &key k))) + ;; forbidden &AUX + (dislike '(defgeneric gf-for-ll-test-13 (x y z &optional a &aux g h))) + (like '(defgeneric gf-for-ll-test-14 (x y z &optional a))) + ;; also can't use bogoDEFMETHODish type-qualifier-ish decorations + ;; on required arguments + (dislike '(defgeneric gf-for-11-test-15 ((arg t)))) + (like '(defgeneric gf-for-11-test-16 (arg)))) + ;;; structure-class tests setup (defclass structure-class-foo1 () () (:metaclass cl:structure-class)) (defclass structure-class-foo2 (structure-class-foo1) @@ -153,7 +202,7 @@ ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect. -(defgeneric bug180 ((x t)) +(defgeneric bug180 (x) (:method-combination list :most-specific-last)) (defmethod bug180 list ((x number)) 'number) diff --git a/version.lisp-expr b/version.lisp-expr index 06b4608..0e49bcb 100644 --- a/version.lisp-expr +++ b/version.lisp-expr @@ -18,4 +18,4 @@ ;;; for internal versions, especially for internal versions off the ;;; main CVS branch, it gets hairier, e.g. "0.pre7.14.flaky4.13".) -"0.7.6.22" +"0.7.6.23"