Commit 97cd7301 authored by Stefan Monnier's avatar Stefan Monnier
Browse files

* lisp/emacs-lisp/bytecomp.el: Add a warning and remove a spurious warning.

(byte-compile-initial-macro-environment): Don't compile before eval in
`eval-and-compile'.
(byte-compile-arglist-warn): Add check for defining macros after their
first use.  Check call use even if the function is fboundp.
parent c6bd7594
2014-11-08 Stefan Monnier <monnier@iro.umontreal.ca>
* emacs-lisp/bytecomp.el (byte-compile-initial-macro-environment):
Don't compile before eval in `eval-and-compile'.
(byte-compile-arglist-warn): Add check for defining macros after their
first use. Check call use even if the function is fboundp.
2014-11-08 Richard Stallman <rms@gnu.org> 2014-11-08 Richard Stallman <rms@gnu.org>
   
* mail/rmail.el (rmail-epa-decrypt): Detect armor with line prefixes. * mail/rmail.el (rmail-epa-decrypt): Detect armor with line prefixes.
......
...@@ -461,10 +461,13 @@ Return the compile-time value of FORM." ...@@ -461,10 +461,13 @@ Return the compile-time value of FORM."
(byte-compile-recurse-toplevel (byte-compile-recurse-toplevel
(cons 'progn body) (cons 'progn body)
(lambda (form) (lambda (form)
(let ((compiled (byte-compile-top-level ;; Don't compile here, since we don't know
(byte-compile-preprocess form)))) ;; whether to compile as byte-compile-form
(eval compiled lexical-binding) ;; or byte-compile-file-form.
compiled)))))) (let ((expanded
(byte-compile-preprocess form)))
(eval expanded lexical-binding)
expanded))))))
"The default macro-environment passed to macroexpand by the compiler. "The default macro-environment passed to macroexpand by the compiler.
Placing a macro here will cause a macro to have different semantics when Placing a macro here will cause a macro to have different semantics when
expanded by the compiler as when expanded by the interpreter.") expanded by the compiler as when expanded by the interpreter.")
...@@ -1361,6 +1364,33 @@ extra args." ...@@ -1361,6 +1364,33 @@ extra args."
;; Warn if the function or macro is being redefined with a different ;; Warn if the function or macro is being redefined with a different
;; number of arguments. ;; number of arguments.
(defun byte-compile-arglist-warn (name arglist macrop) (defun byte-compile-arglist-warn (name arglist macrop)
;; This is the first definition. See if previous calls are compatible.
(let ((calls (assq name byte-compile-unresolved-functions))
nums sig min max)
(when (and calls macrop)
(byte-compile-warn "macro `%s' defined too late" name))
(setq byte-compile-unresolved-functions
(delq calls byte-compile-unresolved-functions))
(setq calls (delq t calls)) ;Ignore higher-order uses of the function.
(when (cdr calls)
(when (and (symbolp name)
(eq (function-get name 'byte-optimizer)
'byte-compile-inline-expand))
(byte-compile-warn "defsubst `%s' was used before it was defined"
name))
(setq sig (byte-compile-arglist-signature arglist)
nums (sort (copy-sequence (cdr calls)) (function <))
min (car nums)
max (car (nreverse nums)))
(when (or (< min (car sig))
(and (cdr sig) (> max (cdr sig))))
(byte-compile-set-symbol-position name)
(byte-compile-warn
"%s being defined to take %s%s, but was previously called with %s"
name
(byte-compile-arglist-signature-string sig)
(if (equal sig '(1 . 1)) " arg" " args")
(byte-compile-arglist-signature-string (cons min max))))))
(let* ((old (byte-compile-fdefinition name macrop)) (let* ((old (byte-compile-fdefinition name macrop))
(initial (and macrop (initial (and macrop
(cdr (assq name (cdr (assq name
...@@ -1369,8 +1399,7 @@ extra args." ...@@ -1369,8 +1399,7 @@ extra args."
;; to a defined function. (Bug#8646) ;; to a defined function. (Bug#8646)
(and initial (symbolp initial) (and initial (symbolp initial)
(setq old (byte-compile-fdefinition initial nil))) (setq old (byte-compile-fdefinition initial nil)))
(if (and old (not (eq old t))) (when (and old (not (eq old t)))
(progn
(and (eq 'macro (car-safe old)) (and (eq 'macro (car-safe old))
(eq 'lambda (car-safe (cdr-safe old))) (eq 'lambda (car-safe (cdr-safe old)))
(setq old (cdr old))) (setq old (cdr old)))
...@@ -1389,32 +1418,7 @@ extra args." ...@@ -1389,32 +1418,7 @@ extra args."
name name
(byte-compile-arglist-signature-string sig1) (byte-compile-arglist-signature-string sig1)
(if (equal sig1 '(1 . 1)) "argument" "arguments") (if (equal sig1 '(1 . 1)) "argument" "arguments")
(byte-compile-arglist-signature-string sig2))))) (byte-compile-arglist-signature-string sig2)))))))
;; This is the first definition. See if previous calls are compatible.
(let ((calls (assq name byte-compile-unresolved-functions))
nums sig min max)
(setq byte-compile-unresolved-functions
(delq calls byte-compile-unresolved-functions))
(setq calls (delq t calls)) ;Ignore higher-order uses of the function.
(when (cdr calls)
(when (and (symbolp name)
(eq (function-get name 'byte-optimizer)
'byte-compile-inline-expand))
(byte-compile-warn "defsubst `%s' was used before it was defined"
name))
(setq sig (byte-compile-arglist-signature arglist)
nums (sort (copy-sequence (cdr calls)) (function <))
min (car nums)
max (car (nreverse nums)))
(when (or (< min (car sig))
(and (cdr sig) (> max (cdr sig))))
(byte-compile-set-symbol-position name)
(byte-compile-warn
"%s being defined to take %s%s, but was previously called with %s"
name
(byte-compile-arglist-signature-string sig)
(if (equal sig '(1 . 1)) " arg" " args")
(byte-compile-arglist-signature-string (cons min max)))))))))
(defvar byte-compile-cl-functions nil (defvar byte-compile-cl-functions nil
"List of functions defined in CL.") "List of functions defined in CL.")
......
...@@ -316,12 +316,12 @@ Subtests signal errors if something goes wrong." ...@@ -316,12 +316,12 @@ Subtests signal errors if something goes wrong."
(with-temp-buffer (with-temp-buffer
(dolist (form forms) (dolist (form forms)
(print form (current-buffer))) (print form (current-buffer)))
(write-region (point-min) (point-max) elfile)) (write-region (point-min) (point-max) elfile nil 'silent))
(if compile (if compile
(let ((byte-compile-dest-file-function (let ((byte-compile-dest-file-function
(lambda (e) elcfile))) (lambda (e) elcfile)))
(byte-compile-file elfile t)) (byte-compile-file elfile t))
(load elfile))) (load elfile nil 'nomessage)))
(when elfile (delete-file elfile)) (when elfile (delete-file elfile))
(when elcfile (delete-file elcfile))))) (when elcfile (delete-file elcfile)))))
(put 'test-byte-comp-compile-and-load 'lisp-indent-function 1) (put 'test-byte-comp-compile-and-load 'lisp-indent-function 1)
...@@ -360,6 +360,28 @@ Subtests signal errors if something goes wrong." ...@@ -360,6 +360,28 @@ Subtests signal errors if something goes wrong."
(defun def () (m)))) (defun def () (m))))
(should (equal (funcall 'def) 4))) (should (equal (funcall 'def) 4)))
(ert-deftest bytecomp-tests--warnings ()
(with-current-buffer (get-buffer-create "*Compile-Log*")
(let ((inhibit-read-only t)) (erase-buffer)))
(test-byte-comp-compile-and-load t
'(progn
(defun my-test0 ()
(my--test11 3)
(my--test12 3)
(my--test2 5))
(defmacro my--test11 (arg) (+ arg 1))
(eval-and-compile
(defmacro my--test12 (arg) (+ arg 1))
(defun my--test2 (arg) (+ arg 1)))))
(with-current-buffer (get-buffer-create "*Compile-Log*")
(goto-char (point-min))
;; Should warn that mt--test1[12] are first used as functions.
(should (re-search-forward "my--test11:\n.*macro" nil t))
(should (re-search-forward "my--test12:\n.*macro" nil t))
(goto-char (point-min))
;; Should not warn that mt--test2 is not known to be defined.
(should-not (re-search-forward "my--test2" nil t))))
(ert-deftest test-eager-load-macro-expansion () (ert-deftest test-eager-load-macro-expansion ()
(test-byte-comp-compile-and-load nil (test-byte-comp-compile-and-load nil
'(progn (defmacro abc (arg) 1) (defun def () (abc 2)))) '(progn (defmacro abc (arg) 1) (defun def () (abc 2))))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment