I created a buffer, test1.el
:
(eval-when-compile
(defmacro dummy-macro (_) nil)
(dummy-macro s))
Trying to compile this file, I get the following errors:
In toplevel form:
test1.el:3:16:Warning: reference to free variable `s'
test1.el:3:16:Error: Symbol's value as variable is void: s
If I change eval-when-compile
to eval-and-compile
I do not get these errors. Why is this?
I'm running Emacs 24.4.1.
eval-when-compile
substitutes the result returned after evaluating the body, so that only that result is available (as a constant) when the byte-compiled code is loaded/used. What does(use-package s :ensure t)
return? That's all that's available. IOW,eval-when-compile
does NOT make the code in its body available for evaluation.eval-and-compile
evaluates the body at load time (not just compile time).(byte-compile-file "test1.el")
is error-free. Why are there errors during Flycheck compilation?(eval-when-compile)
form above is the entire contents of the file. There is no code that should be evaluated after compilation.M-x byte-compile-file RET test1.el
, i.e. flycheck is not relevant to this question. I also see that splitting each form into a separateeval-when-compile
also succeeds without error, perhaps this indicates a bug in Emacs' eager macro expansion.byte-compile-file
I already have theuse-package
macro loaded but Flycheck runs it in a newemacs -Q
subprocess. That would explain the difference, although not why a macro defined during compilation is not available during compilation without splitting into twoeval-when-compile
forms.