I created a buffer, test1.el:

  (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). – Drew Jan 9 '16 at 22:49
  • Yes, but macros defined during compilation should be available during compilation. They are in normal byte compilation: (byte-compile-file "test1.el") is error-free. Why are there errors during Flycheck compilation? – Michael Hoffman Jan 9 '16 at 22:58
  • By the way, the (eval-when-compile) form above is the entire contents of the file. There is no code that should be evaluated after compilation. – Michael Hoffman Jan 9 '16 at 22:59
  • 1
    I see the same thing when just doing 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 separate eval-when-compile also succeeds without error, perhaps this indicates a bug in Emacs' eager macro expansion. – npostavs Jan 10 '16 at 3:02
  • 1
    When I run byte-compile-file I already have the use-package macro loaded but Flycheck runs it in a new emacs -Q subprocess. That would explain the difference, although not why a macro defined during compilation is not available during compilation without splitting into two eval-when-compile forms. – Michael Hoffman Jan 10 '16 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.