Subject: 24.2.50; wrong results using byte-compile-file with lexical-binding

From: Sergey Vinokurov <[email-masked]>
Date: Fri, 28 Sep 2012 14:36:34 +0300

On trunk Emacs compiling function with argument names declared as special
somewhere, like mode-name, resulting function produced by byte-compile-file is
different from function obtained by evaluating definition or byte-compiling
that definition in buffer. This code uses all three possibilities and reports
evaluating results

(progn
  (defvar *myvar* 'never-shows-up)
  (let ((tmp (make-temp-file "test" nil ".el"))
        (form '(defun quux (*myvar*)
                (mapcar (lambda (y)
                          (list *myvar* y))
                 '(a)))))
    (eval form) ;; direct load
    (message "plain: %s" (eval '(quux t)))

    (eval '(byte-compile #'quux)) ;; byte-compile definition alone
    (message "byte-compiled: %s" (eval '(quux t)))

    (with-temp-buffer
      (insert ";; -*- lexical-binding: t -*-\n")
      (princ form (current-buffer))
      ;; uncomment this and reassure that byte-compile-file is still wrong
      ;; (insert "\n\n;; Local Variables:\n;; lexical-binding: t\n;; End:")
      (write-region (point-min) (point-max) tmp))

    (byte-compile-file tmp)
    (load-file (byte-compile-dest-file tmp))
    (message "file byte-compiled: %s" (eval '(quux t)))
    t))

All three outputs should be equal, but result of loaded byte-compiled function
differs

plain: ((t a))
byte-compiled: ((t a))
...
file byte-compiled: ((never-shows-up a))

Also byte-compile-file produced a warning

test12592_hY.el:2:1:Warning: Argument *myvar* is not a lexical variable

but (byte-compile #'quux) hasn't.

Using line with ";; Local Variables:" instead of "-*- lexical-binding: t -*-"
does not solve the problem either. But if lexical-binding is not specified as
a file-local variable it behaves correctly.