From eda75474d4216b1f03c901dd77bf03bd7dd7eee0 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs@gmail.com>
Date: Wed, 10 Jan 2018 14:07:27 -0500
Subject: [PATCH v2] Allow suppressing obsoletion warnings per-version
 (Bug#30039)

* lisp/emacs-lisp/bytecomp.el (byte-compile-not-obsolete-since): New
variable.
(byte-compile-obsolete-p): New function, extracted from
byte-compile-warn-obsolete, consult the new variable.
* lisp/emacs-lisp/macroexp.el (macroexp-macroexpand): Use the new
function.
* etc/NEWS: Announce `byte-compile-not-obsolete-since'.
* test/lisp/emacs-lisp/bytecomp-tests.el
(byte-compile-not-obsolete-since): New test.
---
 etc/NEWS                               |  5 +++++
 lisp/emacs-lisp/bytecomp.el            | 40 +++++++++++++++++++++++++++-------
 lisp/emacs-lisp/macroexp.el            | 16 ++++++++------
 test/lisp/emacs-lisp/bytecomp-tests.el | 31 ++++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 15 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index b4c489c..7983ea3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1613,6 +1613,11 @@ which means ask about all file-visiting buffers.
 ---
 ** string-(to|as|make)-(uni|multi)byte are now declared obsolete.
 
+---
+** New variable 'byte-compile-not-obsolete-since'.
+It may be used to suppress obsoletion warnings later than some
+specified version.
+
 +++
 ** New variable 'while-no-input-ignore-events' which allow
 setting which special events 'while-no-input' should ignore.
diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el
index acba9e2..eae43e7 100644
--- a/lisp/emacs-lisp/bytecomp.el
+++ b/lisp/emacs-lisp/bytecomp.el
@@ -392,6 +392,18 @@ byte-compile-global-not-obsolete-vars
 (defvar byte-compile-not-obsolete-funcs nil
   "List of functions that shouldn't be reported as obsolete.")
 
+(defvar byte-compile-not-obsolete-since nil
+  "Disable obsoletion warnings later than specified version.
+For example, if set to \"25.1\", a function marked as obsolete
+\"since 26.1\" will not be reported as obsolete.  If nil, then
+all obsoletion warnings are enabled (as long as they are not
+already suppressed via `byte-compile-warnings',
+`byte-compile-not-obsolete-funcs', or
+`byte-compile-not-obsolete-vars').")
+
+;;;###autoload
+(put 'byte-compile-not-obsolete-since 'safe-local-variable #'stringp)
+
 (defcustom byte-compile-generate-call-tree nil
   "Non-nil means collect call-graph information when compiling.
 This records which functions were called and from where.
@@ -1239,16 +1251,28 @@ byte-compile-warn
       (error "%s" format)		; byte-compile-file catches and logs it
     (byte-compile-log-warning format t :warning)))
 
-(defun byte-compile-warn-obsolete (symbol)
-  "Warn that SYMBOL (a variable or function) is obsolete."
+(defun byte-compile-obsolete-p (symbol)
+  "Return obsoletion message if SYMBOL is obsolete, nil otherwise.
+Consult `byte-compile-warnings',
+`byte-compile-not-obsolete-funcs' and
+`byte-compile-not-obsolete-since'."
   (when (byte-compile-warning-enabled-p 'obsolete)
     (let* ((funcp (get symbol 'byte-obsolete-info))
-           (msg (macroexp--obsolete-warning
-                 symbol
-                 (or funcp (get symbol 'byte-obsolete-variable))
-                 (if funcp "function" "variable"))))
-      (unless (and funcp (memq symbol byte-compile-not-obsolete-funcs))
-	(byte-compile-warn "%s" msg)))))
+           (var-info (get symbol 'byte-obsolete-variable)))
+      (unless (or (and funcp (memq symbol byte-compile-not-obsolete-funcs))
+                  (and (stringp byte-compile-not-obsolete-since)
+                       (version< byte-compile-not-obsolete-since
+                                 (nth 2 (or funcp var-info)))))
+        (macroexp--obsolete-warning
+         symbol
+         (or funcp var-info)
+         (if funcp "function" "variable"))))))
+
+(defun byte-compile-warn-obsolete (symbol)
+  "Warn that SYMBOL (a variable or function) is obsolete."
+  (let ((msg (byte-compile-obsolete-p symbol)))
+    (when msg
+      (byte-compile-warn "%s" msg))))
 
 (defun byte-compile-report-error (error-info &optional fill)
   "Report Lisp error in compilation.
diff --git a/lisp/emacs-lisp/macroexp.el b/lisp/emacs-lisp/macroexp.el
index b2ac8be..6fddec3 100644
--- a/lisp/emacs-lisp/macroexp.el
+++ b/lisp/emacs-lisp/macroexp.el
@@ -188,13 +188,15 @@ macroexp-macroexpand
              (or (not (fboundp 'byte-compile-warning-enabled-p))
                  (byte-compile-warning-enabled-p 'obsolete)))
         (let* ((fun (car form))
-               (obsolete (get fun 'byte-obsolete-info)))
-          (macroexp--warn-and-return
-           (macroexp--obsolete-warning
-            fun obsolete
-            (if (symbolp (symbol-function fun))
-                "alias" "macro"))
-           new-form))
+               (obsolete (get fun 'byte-obsolete-info))
+               (msg (if (fboundp 'byte-compile-obsolete-p)
+                        (byte-compile-obsolete-p (car form))
+                      (macroexp--obsolete-warning
+                       fun obsolete
+                       (if (symbolp (symbol-function fun))
+                           "alias" "macro")))))
+          (when msg
+            (macroexp--warn-and-return msg new-form)))
       new-form)))
 
 (defun macroexp--expand-all (form)
diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/bytecomp-tests.el
index 13df591..afe7b99 100644
--- a/test/lisp/emacs-lisp/bytecomp-tests.el
+++ b/test/lisp/emacs-lisp/bytecomp-tests.el
@@ -431,6 +431,37 @@ test-byte-comp-compile-and-load
     ;; Should not warn that mt--test2 is not known to be defined.
     (should-not (re-search-forward "my--test2" nil t))))
 
+(ert-deftest byte-compile-not-obsolete-since ()
+  (with-current-buffer (get-buffer-create "*Compile-Log*")
+    (let ((inhibit-read-only t)) (erase-buffer)))
+  (let ((forms
+         '((defun test-obsolete-26 ())
+           (defun test-obsolete-25 ())
+           (defun test-obsolete-24 ())
+           ;; Normally, the obsoletion would only take effect after
+           ;; loading the file.
+           (eval-when-compile (make-obsolete 'test-obsolete-26 nil "26.1"))
+           (eval-when-compile (make-obsolete 'test-obsolete-25 nil "25.1"))
+           (eval-when-compile (make-obsolete 'test-obsolete-24 nil "24.1"))
+           (test-obsolete-26) (test-obsolete-25) (test-obsolete-24))))
+    (apply #'test-byte-comp-compile-and-load t forms)
+    (with-current-buffer (get-buffer-create "*Compile-Log*")
+      (goto-char (point-min))
+      (should (search-forward "obsolete-26"))
+      (should (search-forward "obsolete-25"))
+      (should (search-forward "obsolete-24")))
+    (let ((byte-compile-not-obsolete-since "25.1"))
+      (apply #'test-byte-comp-compile-and-load t forms)
+      (with-current-buffer (get-buffer-create "*Compile-Log*")
+        (goto-char (point-min))
+        (should (search-forward "obsolete-25"))
+        (should (search-forward "obsolete-24"))))
+    (let ((byte-compile-not-obsolete-since "25"))
+      (apply #'test-byte-comp-compile-and-load t forms)
+      (with-current-buffer (get-buffer-create "*Compile-Log*")
+        (goto-char (point-min))
+        (should (search-forward "obsolete-24"))))))
+
 (ert-deftest test-eager-load-macro-expansion ()
   (test-byte-comp-compile-and-load nil
     '(progn (defmacro abc (arg) 1) (defun def () (abc 2))))
-- 
2.6.2.windows.1

