From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Noam Postavsky Newsgroups: gmane.emacs.bugs Subject: bug#5950: defvaralias after defvar should be warned in runtime Date: Sat, 26 May 2018 12:52:48 -0400 Message-ID: <874liufjfj.fsf@gmail.com> References: <4BC6A096.8030801@yahoo.co.jp> <87r5gu34ag.wl%irieshinsuke@yahoo.co.jp> <3keicu1h3v.fsf@fencepost.gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1527353529 3428 195.159.176.226 (26 May 2018 16:52:09 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 26 May 2018 16:52:09 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: 5950@debbugs.gnu.org, IRIE Shinsuke To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat May 26 18:52:05 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fMcQG-0000lm-TW for geb-bug-gnu-emacs@m.gmane.org; Sat, 26 May 2018 18:52:05 +0200 Original-Received: from localhost ([::1]:49632 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fMcSN-0008Pq-Ro for geb-bug-gnu-emacs@m.gmane.org; Sat, 26 May 2018 12:54:15 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49733) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fMcSD-0008OJ-Qw for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 12:54:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fMcSA-0000o2-M6 for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 12:54:05 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:41957) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fMcSA-0000nt-Go for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 12:54:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1fMcSA-0005M1-3e for bug-gnu-emacs@gnu.org; Sat, 26 May 2018 12:54:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Noam Postavsky Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 26 May 2018 16:54:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 5950 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 5950-submit@debbugs.gnu.org id=B5950.152735358520497 (code B ref 5950); Sat, 26 May 2018 16:54:02 +0000 Original-Received: (at 5950) by debbugs.gnu.org; 26 May 2018 16:53:05 +0000 Original-Received: from localhost ([127.0.0.1]:49854 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fMcRF-0005KX-5o for submit@debbugs.gnu.org; Sat, 26 May 2018 12:53:05 -0400 Original-Received: from mail-it0-f45.google.com ([209.85.214.45]:35121) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fMcR6-0005Jc-Np; Sat, 26 May 2018 12:52:57 -0400 Original-Received: by mail-it0-f45.google.com with SMTP id q72-v6so10404957itc.0; Sat, 26 May 2018 09:52:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=n7WH6n5kCUBo3p1RvNcsYMTIHbqGgF8CmT9vx4iZfOA=; b=J5RJtMNCHkQcA/F4WOL+GRimvV+MOREntrNOf4e1Nu7rkkeUn/5a7hV6JN9NbfjWpd OOUCOEJEUAWJyS4cAcZtZH7kcI+fqt2EWWrxbRitFE7xAe+qwDGl7Err3OLf5hjL34Cm 1gvsPEYaXVOtK+ccZ4/3vIxGyARQY3hlZfsFu39KxTFV98GQ5XQuDZU++/klarvO3NUH jY5aUW9V03I92uKdb2m/6oC26sRBG3VuAaBT/hBAbjG6mChiF82OSpBIhfq9XJmMo3ho vs7SNlN844SEZvgLhuWcgUP67LuR9j2hUo6rAZLm0Q9Thq2wGk+LoboyIWRU9vTNbiZn eNaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=n7WH6n5kCUBo3p1RvNcsYMTIHbqGgF8CmT9vx4iZfOA=; b=ijhVsFx1V6gQbkPp0tmIKMYU5xjG6NgehUVL4AjPw1pP0nkJ1I4SSDzb9J8RqP/GQU GT1ddTO547yy4fvA6yBRkeEtvtrYzHElJTklFwRII+cjGtJzE/rMngFFGauTnVAweNAO ZGifu8z5eAzQOMoaFCcj14ZNg3LTaawibHVaOPIW+cQv5xXd9P9rxhx8mfvpjQyUHhPl 0/vtq0VfiIaFIuqRmRhZSSt2Shp64OaxHmcezeJVjbpAVuxZr/daXM1cnltWft+hA6++ TVtYIWmO15G5o8bieNXB1qPw/h2iYj1JO0dfcSqUDDuRWunfx65/QZ8aL+BD26Qhqin1 tfOg== X-Gm-Message-State: ALKqPwfS8JwtHFunrYKYwQP2ZMh72cVKyvYXT4WI5X3KzFDdJiYbBKY+ rL8eidIyGqmV00TfuxoqpQ1GGg== X-Google-Smtp-Source: ADUXVKLr795IvXawabTKL0gMp1E1dD+NmMwNTmh5hlzZfx4OD6mk7dwb4TUEva/vCTRVbtd18Cn7SQ== X-Received: by 2002:a24:4a4c:: with SMTP id k73-v6mr6180582itb.126.1527353571103; Sat, 26 May 2018 09:52:51 -0700 (PDT) Original-Received: from zebian (cbl-45-2-119-34.yyz.frontiernetworks.ca. [45.2.119.34]) by smtp.googlemail.com with ESMTPSA id e139-v6sm8232047ite.1.2018.05.26.09.52.49 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 26 May 2018 09:52:50 -0700 (PDT) In-Reply-To: (Stefan Monnier's message of "Thu, 16 Sep 2010 11:27:57 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:146573 Archived-At: --=-=-= Content-Type: text/plain tags 5950 + patch quit Stefan Monnier writes: > Changing defvaralias to try and be more clever would definitely > be wrong. But changing it to output a warning about the problematic > situation would be OK and changing the byte-compiler to output a warning > in cases that make such a situation more likely is also perfectly good. I see the byte-compiler side of this has been taken care of [1..4], but I still managed to make this mistake when I made a cross-file alias (Bug#31603). Here's a patch for the runtime warning. While writing it, I found that invoking `display-warning' failed during bootstrap, so the first commit works around that. In the end it's not strictly necessary, because I added a check to suppress the warning when the two variables have `eq' values, so no warnings are generated during bootstrap anyway. The bootstrap warnings were about cl-custom-print-functions, mail-dont-reply-to-names, and several flymake-proc-* variables all of which correctly have the alias before the definition. This makes me think I might be doing something wrong, but it seems to do the right thing in my tests. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=v1-0001-Let-display-warning-work-during-bootstrap.patch Content-Description: patch >From e039d38c653059716b68f64fdc7b395bf7593834 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Fri, 25 May 2018 21:37:17 -0400 Subject: [PATCH v1 1/2] Let display-warning work during bootstrap * lisp/emacs-lisp/warnings.el (display-warning): Only call `special-mode' and `newline' if they are `fbound'. --- lisp/emacs-lisp/warnings.el | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lisp/emacs-lisp/warnings.el b/lisp/emacs-lisp/warnings.el index 665733181c..c4d97ceab0 100644 --- a/lisp/emacs-lisp/warnings.el +++ b/lisp/emacs-lisp/warnings.el @@ -241,11 +241,15 @@ display-warning (old (get-buffer buffer-name)) (buffer (or old (get-buffer-create buffer-name))) (level-info (assq level warning-levels)) + ;; `newline' may be unbound during bootstrap. + (newline (if (fboundp 'newline) #'newline + (lambda () (insert "\n")))) start end) (with-current-buffer buffer ;; If we created the buffer, disable undo. (unless old - (special-mode) + (when (fboundp 'special-mode) ; Undefined during bootstrap. + (special-mode)) (setq buffer-read-only t) (setq buffer-undo-list t)) (goto-char (point-max)) @@ -256,7 +260,7 @@ display-warning (funcall warning-series))))) (let ((inhibit-read-only t)) (unless (bolp) - (newline)) + (funcall newline)) (setq start (point)) (if warning-prefix-function (setq level-info (funcall warning-prefix-function @@ -264,7 +268,7 @@ display-warning (insert (format (nth 1 level-info) (format warning-type-format typename)) message) - (newline) + (funcall newline) (when (and warning-fill-prefix (not (string-match "\n" message))) (let ((fill-prefix warning-fill-prefix) (fill-column 78)) -- 2.11.0 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=v1-0002-Give-warning-if-losing-value-to-defvaralias-Bug-5.patch Content-Description: patch >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 --=-=-= Content-Type: text/plain [1: 495963cfaf]: 2018-04-20 17:22:47 -0400 * lisp/emacs-lisp/bytecomp.el (byte-compile-file-form-defvar-function): https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=495963cfaf535646350051f47c085b84319572f0 [2: 9c3eeba4db]: 2018-04-20 18:34:39 -0400 The tedious game of whack-a-mole with compiler warnings continues https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=9c3eeba4db26ddaeead100beea7a96f9fa640918 [3: 18de2ada24]: 2018-04-20 18:55:04 -0400 More alias-related tedium https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=18de2ada243653ece98b18044233e5d29eee5903 [4: 94e794c8d8]: 2018-04-20 19:02:16 -0400 Tweak recent bytecomp defvaralias change https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=94e794c8d8b93a1d6813742da12135f2746ef80b --=-=-=--