From 6fe5dee5f4ad0a6e18d73658d0392d4444ff1826 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Fri, 25 May 2018 08:40:55 -0400 Subject: [PATCH v1 2/2] Give warning if losing value to defvaralias (Bug#5950) * src/eval.c (Fdefvaralias): Call `display-warning' if the alias target has a non-eq value to the variable being aliased. * test/src/eval-tests.el (defvaralias-overwrite-warning): New test. --- src/eval.c | 10 ++++++++++ test/src/eval-tests.el | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/eval.c b/src/eval.c index 90d8c33518..354ab2c2d1 100644 --- a/src/eval.c +++ b/src/eval.c @@ -623,6 +623,16 @@ DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 3, 0, if (NILP (Fboundp (base_variable))) set_internal (base_variable, find_symbol_value (new_alias), Qnil, SET_INTERNAL_BIND); + else if (!NILP (Fboundp (new_alias)) + && !EQ (find_symbol_value (new_alias), + find_symbol_value (base_variable))) + call2 (intern ("display-warning"), + list3 (intern ("defvaralias"), intern ("losing-value"), new_alias), + CALLN (Fformat_message, + build_string + ("Overwriting value of `%s' by aliasing to `%s'"), + new_alias, base_variable)); + { union specbinding *p; diff --git a/test/src/eval-tests.el b/test/src/eval-tests.el index 319dd91c86..281d959b53 100644 --- a/test/src/eval-tests.el +++ b/test/src/eval-tests.el @@ -26,6 +26,7 @@ ;;; Code: (require 'ert) +(eval-when-compile (require 'cl-lib)) (ert-deftest eval-tests--bug24673 () "Check that Bug#24673 has been fixed." @@ -117,4 +118,25 @@ eval-tests--exceed-specbind-limit "Check that Bug#31072 is fixed." (should-error (eval '(defvar 1) t) :type 'wrong-type-argument)) +(ert-deftest defvaralias-overwrite-warning () + "Test for Bug#5950." + (defvar eval-tests--foo) + (setq eval-tests--foo 2) + (defvar eval-tests--foo-alias) + (setq eval-tests--foo-alias 1) + (cl-letf (((symbol-function 'display-warning) + (lambda (type &rest _) + (throw 'got-warning type)))) + ;; Warn if we lose a value through aliasing. + (should (equal + '(defvaralias losing-value eval-tests--foo-alias) + (catch 'got-warning + (defvaralias 'eval-tests--foo-alias 'eval-tests--foo)))) + ;; Don't warn if we don't. + (makunbound 'eval-tests--foo-alias) + (should (eq 'no-warning + (catch 'got-warning + (defvaralias 'eval-tests--foo-alias 'eval-tests--foo) + 'no-warning))))) + ;;; eval-tests.el ends here -- 2.11.0