ccl icon indicating copy to clipboard operation
ccl copied to clipboard

File compiler incorrectly warns about actually applicable keyword arguments

Open Hexstream opened this issue 4 years ago • 2 comments

Here is test-case.lisp (this same code works without warnings in the REPL):

(in-package #:cl-user)

(progn
  (defclass location-mixin ()
    ((%location :initarg :location
                :initform nil)))

  (defgeneric location (system &key)
    (:method :around (system &key (errorp t))
      (or (call-next-method)
          (when errorp
            (error "There is no ~S for system ~S." 'defsys:location system))))
    (:method ((system location-mixin) &key)
      (slot-value system '%location)))

  (location (make-instance 'location-mixin) :errorp nil))

According to CLHS 7.6.5, I believe this should compile without problems, since:

When a generic function or any of its methods mentions &key in a lambda list, the specific set of keyword arguments accepted by the generic function varies according to the applicable methods. The set of keyword arguments accepted by the generic function for a particular call is the union of the keyword arguments accepted by all applicable methods and the keyword arguments mentioned after &key in the generic function definition, if any.

Since the around method (which accepts errorp) is always applicable, then the errorp keyword argument should always be accepted by the location generic function, without warnings.

Traditionally, what I did is declare errorp in the generic function and in all methods, but that's annoying because most methods don't even care about errorp since that's handled by the around method, so I had to declare and ignore it in the methods. Being able to use just &key is much nicer.

edit:

CL-USER> (lisp-implementation-version)
"Version 1.11.5/v1.11.5  (LinuxX8664)"

hexstream@dynamorph:~$ uname -a
Linux dynamorph 4.19.0-13-amd64 #1 SMP Debian 4.19.160-2 (2020-11-28) x86_64 GNU/Linux

Hexstream avatar Feb 17 '21 15:02 Hexstream

Issue reproduced on CCL 1.12. Minimal test case:

$ cat /tmp/foo.lisp
(defgeneric location (system &key)
  (:method :around (system &key errorp))
  (:method (system &key)))
(location nil :errorp nil)
$ ccl
Clozure Common Lisp Version 1.12 (v1.12) LinuxX8664

For more information about CCL, please see http://ccl.clozure.com.

CCL is free software.  It is distributed under the terms of the Apache
Licence, Version 2.0.
? (compile-file "/tmp/foo.lisp")
;Compiler warnings for "/tmp/foo.lisp" :
;   In (LOCATION :AROUND (T)) inside an anonymous lambda form: Unused lexical variable SYSTEM
;   In (LOCATION :AROUND (T)) inside an anonymous lambda form: Unused lexical variable ERRORP
;Compiler warnings for "/tmp/foo.lisp" :
;   In an anonymous lambda form at position 125: In the call to LOCATION with arguments ((LET* ((#:G5 (LOAD-TIME-VALUE (CCL::FIND-CLASS-CELL 'LOCATION-MIXIN T)))) (FUNCALL (CCL::CLASS-CELL-INSTANTIATE #:G5) #:G5)) :ERRORP NIL),
;     the keyword argument :ERRORP is not recognized by the definition of LOCATION visible in the current compilation unit.
#P"/tmp/foo.lx64fsl"
T
NIL

The compilation warning itself is reported at https://github.com/Clozure/ccl/blob/553c0f25f38b2b0d5922ca7b4f62f09eb85ace1c/compiler/nx-basic.lisp#L633-L671 but the compiler should be fixed earlier, where the warning is actually signaled. The two possible sources of that (functions capable of generating :unknown-gf-keywords warnings) are:

https://github.com/Clozure/ccl/blob/553c0f25f38b2b0d5922ca7b4f62f09eb85ace1c/compiler/nx0.lisp#L2360-L2387

https://github.com/Clozure/ccl/blob/553c0f25f38b2b0d5922ca7b4f62f09eb85ace1c/level-1/sysutils.lisp#L616-L647

phoe avatar Feb 17 '21 17:02 phoe

Thank you for the prompt detailed reply.

Hexstream avatar Feb 17 '21 17:02 Hexstream