Subject: bug#5950: defvaralias after defvar should be warned in runtime

From: Glenn Morris <[email-masked]>
Date: Wed, 15 Sep 2010 16:16:03 -0400

IRIE Shinsuke wrote:

> We assume an user wrote .emacs file as:
>
>  (setq old-foo 123)
>  (load "bar")
>
> Here, bar.el includes the following:
>
>  (defvar foo 456)
>  (define-obsolete-variable-alias 'old-foo 'foo)
>
> By this .emacs setting, the user probably hopes `old-foo' remains 123
> after loading bar.el, but actually it will be changed to 456.
[...]
> I think the programs like bar.el should be warned when running (and
> byte-compiling, if possible).

You can never have too many compilation warnings...

Here is an attempt at this. Comments:

1) It gives a spurious warning in cases where the
define-obsolete-variable-alias statement is autoloaded. One could
simply reposition such statements anyway.

2) I never know when to use normal-call or keep-pending.

It does reveal a couple of bugs in the current trunk.

*** lisp/emacs-lisp/bytecomp.el	2010-09-15 15:30:43 +0000
--- lisp/emacs-lisp/bytecomp.el	2010-09-15 18:59:50 +0000
***************
*** 3874,3879 ****
--- 3874,3892 ----
    (byte-compile-set-symbol-position 'lambda)
    (error "`lambda' used as function name is invalid"))
  
+ (put 'define-obsolete-variable-alias 'byte-hunk-handler
+      'byte-compile-define-obsolete-variable-alias)
+ ;; Gives a spurious warning if the d-o-v-a is autoloaded.
+ (defun byte-compile-define-obsolete-variable-alias (form)
+   (and (byte-compile-warning-enabled-p 'suspicious)
+        (eq (car-safe (nth 2 form)) 'quote)
+        (memq (car-safe (cdr (nth 2 form))) byte-compile-bound-variables)
+        (not (memq (cadr (nth 2 form)) byte-compile-const-variables))
+        (byte-compile-warn "variable `%s' aliased after definition"
+ 			  (cadr (nth 2 form))))
+   (byte-compile-keep-pending form)
+   nil)
+ 
  ;; Compile normally, but deal with warnings for the function being defined.
  (put 'defalias 'byte-hunk-handler 'byte-compile-file-form-defalias)
  (defun byte-compile-file-form-defalias (form)