]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix spurious "Lexical argument shadows the dynamic variable" due to inlining
authorStefan Monnier <monnier@iro.umontreal.ca>
Thu, 21 Jan 2021 18:15:05 +0000 (13:15 -0500)
committerStefan Monnier <monnier@iro.umontreal.ca>
Thu, 21 Jan 2021 18:15:05 +0000 (13:15 -0500)
Before this patch doing:

    rm lisp/calendar/calendar.elc
    make lisp/calendar/cal-hebrew.elc

would spew out lots of spurious such warnings about a `date` argument,
pointing to code which has no `date` argument in sight.  This was
because that code had calls to inlinable functions (taking a `date`
argument) defined in `calendar.el`, and while `date` is a normal
lexical var at the site of those functions' definitions, it was
declared as dynbound at the call site.

* lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand):
Don't impose our local context onto the inlined function.

* test/lisp/emacs-lisp/bytecomp-tests.el: Add matching test.

lisp/emacs-lisp/byte-opt.el
test/lisp/emacs-lisp/bytecomp-resources/foo-inlinable.el [new file with mode: 0644]
test/lisp/emacs-lisp/bytecomp-resources/nowarn-inline-after-defvar.el [new file with mode: 0644]
test/lisp/emacs-lisp/bytecomp-tests.el

index cfa407019a7c612c66cce14f04be8d2c135c350a..66a117fccc8effb70cebb7aba1f968985e9c3a9d 100644 (file)
                           ;; If `fn' is from the same file, it has already
                           ;; been preprocessed!
                           `(function ,fn)
-                        (byte-compile-preprocess
-                         (byte-compile--reify-function fn)))))
+                        ;; Try and process it "in its original environment".
+                        (let ((byte-compile-bound-variables nil))
+                          (byte-compile-preprocess
+                           (byte-compile--reify-function fn))))))
            (if (eq (car-safe newfn) 'function)
                (byte-compile-unfold-lambda `(,(cadr newfn) ,@(cdr form)))
              ;; This can happen because of macroexp-warn-and-return &co.
diff --git a/test/lisp/emacs-lisp/bytecomp-resources/foo-inlinable.el b/test/lisp/emacs-lisp/bytecomp-resources/foo-inlinable.el
new file mode 100644 (file)
index 0000000..4748157
--- /dev/null
@@ -0,0 +1,6 @@
+;; -*- lexical-binding: t; -*-
+
+(defsubst foo-inlineable (foo-var)
+  (+ foo-var 2))
+
+(provide 'foo-inlinable)
diff --git a/test/lisp/emacs-lisp/bytecomp-resources/nowarn-inline-after-defvar.el b/test/lisp/emacs-lisp/bytecomp-resources/nowarn-inline-after-defvar.el
new file mode 100644 (file)
index 0000000..5582b2a
--- /dev/null
@@ -0,0 +1,17 @@
+;; -*- lexical-binding: t; -*-
+
+;; In this test, we try and make sure that inlined functions's code isn't
+;; mistakenly re-interpreted in the caller's context: we import an
+;; inlinable function from another file where `foo-var' is a normal
+;; lexical variable, and then call(inline) it in a function where
+;; `foo-var' is a dynamically-scoped variable.
+
+(require 'foo-inlinable
+         (expand-file-name "foo-inlinable.el"
+                           (file-name-directory
+                            (or byte-compile-current-file load-file-name))))
+
+(defvar foo-var)
+
+(defun foo-fun ()
+  (+ (foo-inlineable 5) 1))
index 263736af4edf7c3645c6ee68405c2e3984648c4d..980b402ca2d50cd20d36091660f23b9de7039ab6 100644 (file)
@@ -713,6 +713,10 @@ Subtests signal errors if something goes wrong."
  "warn-wide-docstring-multiline.el"
  "defvar.*foo.*wider than.*characters")
 
+(bytecomp--define-warning-file-test
+ "nowarn-inline-after-defvar.el"
+ "Lexical argument shadows" 'reverse)
+
 \f
 ;;;; Macro expansion.