2

To use directory-files-recursively in Emacs 24.x, I'm adding the following to a file cider-compat.el:

(eval-and-compile
  (unless (fboundp 'directory-files-recursively)
    (defun directory-files-recursively (dir regexp &optional include-directories)
      ...)))

Byte-compiling the file, however, gives an error in Emacs 24.x:

In toplevel form:
cider-compat.el:191:1:Error: the function `directory-files-recursively' might not be defined at runtime.
make: *** [cider-compat.elc-test] Error 1

Why is this? The eval-and-compile seems to work fine for other defuns, e.g. internal--build-binding.

Tianxiang Xiong
  • 3,878
  • 18
  • 28

1 Answers1

4

That's because the compiler simply sees that at compile-time the function is defined (because of the eval-and-), but it sees that at runtime, the function will only be defined (if <blabla>, IOW it will only be defined in some circumstances, and it doesn't bother to try to figure out what those circumstances would be, so it emits the warning because of the doubt.

You can typically avoid these kinds of issues (and avoid eval-and-compile as well, which tends to be a bit quirky) with:

(defalias 'my-directory-files-recursively
  (if (fboundp 'directory-files-recursively
      'directory-files-recursively)
    (lambda (dir regexp &optional include-directories)
      ...)))
Stefan
  • 26,404
  • 3
  • 48
  • 85
  • That's one solution, but a with-no-warnings around the body seems to work as well. – Tianxiang Xiong Jul 22 '17 at 03:42
  • 1
    Better than with-no-warning (which is a blunt tool), if you only want to silence the warning rather than make the code understandable to the compiler, you can use declare-function. – Stefan Jul 22 '17 at 16:29