GNU bug report logs - #6376
23.2; byte compile add-to-list report free variable

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Severity: wishlist; Reported by: Kevin Ryde <user42@HIDDEN>; dated Tue, 8 Jun 2010 01:56:01 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.

Message received at 6376 <at> debbugs.gnu.org:


Received: (at 6376) by debbugs.gnu.org; 19 Nov 2020 04:05:28 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Wed Nov 18 23:05:28 2020
Received: from localhost ([127.0.0.1]:37457 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1kfbCF-00018u-AA
	for submit <at> debbugs.gnu.org; Wed, 18 Nov 2020 23:05:28 -0500
Received: from mail-ed1-f45.google.com ([209.85.208.45]:42971)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <stefankangas@HIDDEN>) id 1kfbC8-00018K-7r
 for 6376 <at> debbugs.gnu.org; Wed, 18 Nov 2020 23:05:21 -0500
Received: by mail-ed1-f45.google.com with SMTP id v22so4347616edt.9
 for <6376 <at> debbugs.gnu.org>; Wed, 18 Nov 2020 20:05:16 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:from:in-reply-to:references:user-agent
 :mime-version:date:message-id:subject:to:cc;
 bh=6Ee/miZga9ZgQ4+irEP7qiu7RAw75dyjXdUXPqBjbYs=;
 b=Mccu04/K6dT2k+GkkZVtgqktCiI82tuVHJ9JEY8VBXdh74qlCLIf5+WSC7GSoiwFuq
 ZROitASttIQ0+GHPT2xaM8OpwjS2oofrhosbMQcXzK2J13MgzUSHiMG5KlnD9xjcJStG
 1nJFhtHzCXOFgaZhR3+agFZOOJMrXuvb9IlYqMuhoFd4xuCV9GTDrHeUwTP5zXvLh7+3
 WfyZ+8TuzXeBNG25xg6LkWIx4zxnvf4dZ1EULSw+fT2ppkuq9vhcGH+ZuLBnb343XEyJ
 VzBtGFStME/1OxBgvrnS3jC0Sq1TK8aud5egDeHehWai1Qjb2qk6rCwxJsAJh2j/t+lR
 J4HQ==
X-Gm-Message-State: AOAM53187VddWoSK59u9UC7znugrS9R8L+PjWn3AE3A31f8LPaGkcA0C
 uJ+zu8V1eXNirBEyDUgHbZL+rwxTZcaA5dWv40KRX/l8EZo=
X-Google-Smtp-Source: ABdhPJzmmusz9EIumB6JZpXfLoZxHb5U8uAfoe9M+Qcv5p54zqDqtlpTMAaQHQr/UK3dBqMGjF/Eurvecc0N5HK479M=
X-Received: by 2002:a50:9f61:: with SMTP id b88mr30184243edf.282.1605758710681; 
 Wed, 18 Nov 2020 20:05:10 -0800 (PST)
Received: from 753933720722 named unknown by gmailapi.google.com with
 HTTPREST; Wed, 18 Nov 2020 20:05:10 -0800
From: Stefan Kangas <stefan@HIDDEN>
In-Reply-To: <87zkwzs3tk.fsf@HIDDEN> (Kevin Ryde's message of "Sat, 07 Aug
 2010 09:45:43 +1000")
References: <87y6eqia4d.fsf@HIDDEN> <jwvzkz56lrq.fsf-monnier+emacs@HIDDEN>
 <87zkwzs3tk.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)
MIME-Version: 1.0
Date: Wed, 18 Nov 2020 20:05:10 -0800
Message-ID: <CADwFkm=yxm93+TQARoGkp87zQxk5wU0K61q7qg92CmJ8=uZr0Q@HIDDEN>
Subject: Re: bug#6376: 23.2; byte compile add-to-list report free variable
To: Kevin Ryde <user42@HIDDEN>
Content-Type: text/plain; charset="UTF-8"
X-Spam-Score: 0.5 (/)
X-Debbugs-Envelope-To: 6376
Cc: 6376 <at> debbugs.gnu.org, Stefan Monnier <monnier@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.5 (/)

Kevin Ryde <user42@HIDDEN> writes:

> I got a bit further to these few lines.  Not quite ready yet, but
> getting closer.  The byte-compile-check-var is more or less a break-out
> of the checks in byte-compile-variable-ref.
>
> add-hook and remove-hook end up with checks against non-variables, but
> they don't demand a bound variable.  I suppose they could helpfully also
> notice undefined functions in their second arg too.  You'd be tempted to
> have some sort of general arg-type description for the builtin funcs
> rather than doing checks plus "normal-call" for each.

(That was 10 years ago.)

This change looks useful to me.  Any chance you could fix it up and send
it as a patch?

> (require 'bytecomp)
>
> (defun byte-compile-check-var (symbol base-op)
>   ;; SYMBOL is the name of a variable.  BASE-OP is one of the following
>   ;; according to what's being done to the variable.
>   ;;    'byte-varref
>   ;;    'byte-varset
>   ;;    'byte-varbind
>   ;;    'varset-def    -- set, defining if not already (eg. add-hook)
>   ;; Emit a warning if SYMBOL is unbound, or if it's a constant being set or
>   ;; bound.
>   ;;
>   (cond ((and (not (symbolp symbol))
>               ;; `constants' warning includes all non-variables
>               (byte-compile-warning-enabled-p 'constants))
>          (byte-compile-warn "%s non-variable `%S'"
>                             (assoc-default base-op
>                                            '((byte-varref . "reference to")
>                                              (byte-varset . "assignment to")
>                                              (varset-def  . "assignment to")
>                                              (byte-varbind . "let-bind of"))
>                                            nil base-op)
>                             symbol))
>
>         ((and (not (eq base-op 'byte-varref))
>               (byte-compile-warning-enabled-p 'constants)
>               (byte-compile-const-symbol-p symbol t))
>          (byte-compile-warn "%s constant `%S'"
>                             (assoc-default
>                              base-op
>                              '((byte-varset . "variable assignment to")
>                                (varset-def  . "variable assignment to")
>                                (byte-varbind . "let-bind of"))
>                              nil base-op)
>                             symbol))
>
>         ((and (not (memq base-op '(varset-def byte-varbind)))
>               (byte-compile-warning-enabled-p 'free-vars)
>               (not (memq symbol byte-compile-bound-variables))
>               (not (memq symbol byte-compile-free-assignments)))
>          (byte-compile-warn "%s to free variable `%S'"
>                             (assoc-default base-op
>                                            '((byte-varref . "reference to")
>                                              (byte-varset . "assignment to"))
>                                            nil base-op)
>                             symbol)
>          (push symbol byte-compile-free-assignments))))
>
> (defun byte-compile-check-argvar (arg base-op)
>   ;; ARG is a function argument form which is supposed to evaluate to a
>   ;; symbol naming a variable.  If ARG is (quote FOO) or :foo then check
>   ;; that it's a bound variable per bytecomp-check-var.  If ARG is
>   ;; self-evaluating like nil, t, strings, etc then pass to the check too,
>   ;; to possibly report assignment to a constant.  Code like (quote nil) or
>   ;; (quote "foo") reaches this point as plain nil or t.
>   ;;
>   (cond ((eq 'quote (car-safe arg))
>          (byte-compile-check-var (car-safe (cdr arg)) base-op))
>         ((or (memq arg '(nil t))
>              ;; anything except a symbol or list is self-evaluating
>              (not (or (symbolp arg)
>                       (consp arg))))
>          (byte-compile-check-var arg base-op))))
>
> (defun byte-compile-addtolist (form)
>   ;; first arg is the name of a variable being changed, eg. (foo 'var ...)
>   (if (>= (safe-length form) 2)
>       (byte-compile-check-argvar (cadr form) 'byte-varset))
>   (byte-compile-normal-call form))
> (byte-defop-compiler-1 add-to-list         byte-compile-addtolist)
> (byte-defop-compiler-1 add-to-ordered-list byte-compile-addtolist)
>
> (defun byte-compile-addremhook (form)
>   ;; first arg is the name of a variable being changed, eg. (foo 'var ...)
>   ;; only
>   (if (>= (safe-length form) 2)
>       (byte-compile-check-argvar (cadr form) 'varset-def))
>   (byte-compile-normal-call form))
> (byte-defop-compiler-1 add-hook    byte-compile-addremhook)
> (byte-defop-compiler-1 remove-hook byte-compile-addremhook)




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#6376; Package emacs. Full text available.

Message received at 6376 <at> debbugs.gnu.org:


Received: (at 6376) by debbugs.gnu.org; 21 Sep 2010 01:32:57 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Sep 20 21:32:57 2010
Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1Oxrj7-00022i-4d
	for submit <at> debbugs.gnu.org; Mon, 20 Sep 2010 21:32:57 -0400
Received: from mailout2-8.pacific.net.au ([61.8.2.231]
	helo=mailout2.pacific.net.au)
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <gg@HIDDEN>) id 1Oxrj4-00022d-0p
	for 6376 <at> debbugs.gnu.org; Mon, 20 Sep 2010 21:32:55 -0400
Received: from mailproxy1.pacific.net.au (mailproxy1.pacific.net.au
	[61.8.2.162])
	by mailout2.pacific.net.au (Postfix) with ESMTP id 3AE2729C357;
	Tue, 21 Sep 2010 11:35:23 +1000 (EST)
Received: from blah.blah (ppp204C.dyn.pacific.net.au [61.8.32.76])
	by mailproxy1.pacific.net.au (Postfix) with ESMTP id 466EB8C12;
	Tue, 21 Sep 2010 11:35:19 +1000 (EST)
Received: from gg by blah.blah with local (Exim 4.72)
	(envelope-from <gg@HIDDEN>)
	id 1Oxrl1-0000H0-8k; Tue, 21 Sep 2010 11:34:55 +1000
From: Kevin Ryde <user42@HIDDEN>
To: Stefan Monnier <monnier@HIDDEN>
Subject: Re: bug#6376: 23.2; byte compile add-to-list report free variable
References: <87y6eqia4d.fsf@HIDDEN> <jwvzkz56lrq.fsf-monnier+emacs@HIDDEN>
	<87zkwzs3tk.fsf@HIDDEN> <jwvvd7ikf3c.fsf-monnier+emacs@HIDDEN>
Date: Tue, 21 Sep 2010 11:34:55 +1000
In-Reply-To: <jwvvd7ikf3c.fsf-monnier+emacs@HIDDEN> (Stefan Monnier's message
	of "Tue, 10 Aug 2010 11:12:17 +0200")
Message-ID: <87bp7r6fr4.fsf@HIDDEN>
User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.2 (gnu/linux)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Spam-Score: -3.5 (---)
X-Debbugs-Envelope-To: 6376
Cc: 6376 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.11
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/pipermail/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -3.5 (---)

Stefan Monnier <monnier@HIDDEN> writes:
>
> Could you send it as a patch, please.

I suppose I should get my cvs head checkout sorted so as to be able to
test on the current code.  That's likely to take a while :-(

> Which part(s) so you think still need work?

I thought to make room for checking the func arg to add-hook and
friends.  But checking the variables might be enough to start with.




Information forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs@HIDDEN:
bug#6376; Package emacs. Full text available.

Message received at 6376 <at> debbugs.gnu.org:


Received: (at 6376) by debbugs.gnu.org; 10 Aug 2010 09:11:36 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Tue Aug 10 05:11:36 2010
Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1Oikrw-00077v-IE
	for submit <at> debbugs.gnu.org; Tue, 10 Aug 2010 05:11:36 -0400
Received: from impaqm3.telefonica.net ([213.4.138.3])
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <monnier@HIDDEN>) id 1Oikrv-00077q-Ai
	for 6376 <at> debbugs.gnu.org; Tue, 10 Aug 2010 05:11:35 -0400
Received: from IMPmailhost5.adm.correo ([10.20.102.126])
	by IMPaqm3.telefonica.net with bizsmtp
	id sXBk1e00m2jdgqJ3PZCJuQ; Tue, 10 Aug 2010 11:12:18 +0200
Received: from ceviche.home ([83.40.4.203])
	by IMPmailhost5.adm.correo with BIZ IMP
	id sZCH1e00P4NokFF1lZCJJe; Tue, 10 Aug 2010 11:12:18 +0200
X-Brightmail-Tracker: AAAAAA==
X-TE-authinfo: authemail="monnier$movistar.es"
	|auth_email="monnier@HIDDEN"
X-TE-AcuTerraCos: auth_cuTerraCos="cosuitnetc01"
Received: by ceviche.home (Postfix, from userid 20848)
	id 8910766138; Tue, 10 Aug 2010 11:12:17 +0200 (CEST)
From: Stefan Monnier <monnier@HIDDEN>
To: Kevin Ryde <user42@HIDDEN>
Subject: Re: bug#6376: 23.2; byte compile add-to-list report free variable
Message-ID: <jwvvd7ikf3c.fsf-monnier+emacs@HIDDEN>
References: <87y6eqia4d.fsf@HIDDEN> <jwvzkz56lrq.fsf-monnier+emacs@HIDDEN>
	<87zkwzs3tk.fsf@HIDDEN>
Date: Tue, 10 Aug 2010 11:12:17 +0200
In-Reply-To: <87zkwzs3tk.fsf@HIDDEN> (Kevin Ryde's message of "Sat, 07 Aug
	2010 09:45:43 +1000")
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Spam-Score: -2.2 (--)
X-Debbugs-Envelope-To: 6376
Cc: 6376 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.11
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/pipermail/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -2.2 (--)

> I got a bit further to these few lines.  Not quite ready yet, but
> getting closer.  The byte-compile-check-var is more or less a break-out
> of the checks in byte-compile-variable-ref.

Could you send it as a patch, please.
Which part(s) so you think still need work?


        Stefan




Information forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs@HIDDEN:
bug#6376; Package emacs. Full text available.

Message received at 6376 <at> debbugs.gnu.org:


Received: (at 6376) by debbugs.gnu.org; 6 Aug 2010 23:45:20 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Aug 06 19:45:20 2010
Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1OhWbI-0001MD-8h
	for submit <at> debbugs.gnu.org; Fri, 06 Aug 2010 19:45:20 -0400
Received: from mailout1-7.pacific.net.au ([61.8.2.214]
	helo=mailout1.pacific.net.au)
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <gg@HIDDEN>) id 1OhWbE-0001M7-Te
	for 6376 <at> debbugs.gnu.org; Fri, 06 Aug 2010 19:45:18 -0400
Received: from mailproxy2.pacific.net.au (mailproxy2.pacific.net.au
	[61.8.2.163])
	by mailout1.pacific.net.au (Postfix) with ESMTP id CDC915D077D;
	Sat,  7 Aug 2010 09:45:49 +1000 (EST)
Received: from blah.blah (ppp2335.dyn.pacific.net.au [61.8.35.53])
	by mailproxy2.pacific.net.au (Postfix) with ESMTP id C08FC27406;
	Sat,  7 Aug 2010 09:45:48 +1000 (EST)
Received: from gg by blah.blah with local (Exim 4.72)
	(envelope-from <gg@HIDDEN>)
	id 1OhWbf-000204-Ld; Sat, 07 Aug 2010 09:45:43 +1000
From: Kevin Ryde <user42@HIDDEN>
To: 6376 <at> debbugs.gnu.org
Subject: Re: bug#6376: 23.2; byte compile add-to-list report free variable
References: <87y6eqia4d.fsf@HIDDEN> <jwvzkz56lrq.fsf-monnier+emacs@HIDDEN>
Date: Sat, 07 Aug 2010 09:45:43 +1000
In-Reply-To: <jwvzkz56lrq.fsf-monnier+emacs@HIDDEN> (Stefan Monnier's message
	of "Tue, 08 Jun 2010 21:29:20 -0400")
Message-ID: <87zkwzs3tk.fsf@HIDDEN>
User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.2 (gnu/linux)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-Spam-Score: -3.6 (---)
X-Debbugs-Envelope-To: 6376
Cc: Stefan Monnier <monnier@HIDDEN>
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.11
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/pipermail/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -3.5 (---)

--=-=-=

I got a bit further to these few lines.  Not quite ready yet, but
getting closer.  The byte-compile-check-var is more or less a break-out
of the checks in byte-compile-variable-ref.

add-hook and remove-hook end up with checks against non-variables, but
they don't demand a bound variable.  I suppose they could helpfully also
notice undefined functions in their second arg too.  You'd be tempted to
have some sort of general arg-type description for the builtin funcs
rather than doing checks plus "normal-call" for each.


--=-=-=
Content-Type: application/emacs-lisp
Content-Disposition: inline; filename=bytecomp-add-to-list.el
Content-Transfer-Encoding: quoted-printable

(require 'bytecomp)

(defun byte-compile-check-var (symbol base-op)
  ;; SYMBOL is the name of a variable.  BASE-OP is one of the following
  ;; according to what's being done to the variable.
  ;;    'byte-varref
  ;;    'byte-varset
  ;;    'byte-varbind
  ;;    'varset-def    -- set, defining if not already (eg. add-hook)
  ;; Emit a warning if SYMBOL is unbound, or if it's a constant being set or
  ;; bound.
  ;;
  (cond ((and (not (symbolp symbol))
              ;; `constants' warning includes all non-variables
              (byte-compile-warning-enabled-p 'constants))
         (byte-compile-warn "%s non-variable `%S'"
                            (assoc-default base-op
                                           '((byte-varref . "reference to")
                                             (byte-varset . "assignment to")
                                             (varset-def  . "assignment to")
                                             (byte-varbind . "let-bind of"))
                                           nil base-op)
                            symbol))

        ((and (not (eq base-op 'byte-varref))
              (byte-compile-warning-enabled-p 'constants)
              (byte-compile-const-symbol-p symbol t))
         (byte-compile-warn "%s constant `%S'"
                            (assoc-default
                             base-op
                             '((byte-varset . "variable assignment to")
                               (varset-def  . "variable assignment to")
                               (byte-varbind . "let-bind of"))
                             nil base-op)
                            symbol))

        ((and (not (memq base-op '(varset-def byte-varbind)))
              (byte-compile-warning-enabled-p 'free-vars)
              (not (memq symbol byte-compile-bound-variables))
              (not (memq symbol byte-compile-free-assignments)))
         (byte-compile-warn "%s to free variable `%S'"
                            (assoc-default base-op
                                           '((byte-varref . "reference to")
                                             (byte-varset . "assignment to"=
))
                                           nil base-op)
                            symbol)
         (push symbol byte-compile-free-assignments))))

(defun byte-compile-check-argvar (arg base-op)
  ;; ARG is a function argument form which is supposed to evaluate to a
  ;; symbol naming a variable.  If ARG is (quote FOO) or :foo then check
  ;; that it's a bound variable per bytecomp-check-var.  If ARG is
  ;; self-evaluating like nil, t, strings, etc then pass to the check too,
  ;; to possibly report assignment to a constant.  Code like (quote nil) or
  ;; (quote "foo") reaches this point as plain nil or t.
  ;;
  (cond ((eq 'quote (car-safe arg))
         (byte-compile-check-var (car-safe (cdr arg)) base-op))
        ((or (memq arg '(nil t))
             ;; anything except a symbol or list is self-evaluating
             (not (or (symbolp arg)
                      (consp arg))))
         (byte-compile-check-var arg base-op))))

(defun byte-compile-addtolist (form)
  ;; first arg is the name of a variable being changed, eg. (foo 'var ...)
  (if (>=3D (safe-length form) 2)
      (byte-compile-check-argvar (cadr form) 'byte-varset))
  (byte-compile-normal-call form))
(byte-defop-compiler-1 add-to-list         byte-compile-addtolist)
(byte-defop-compiler-1 add-to-ordered-list byte-compile-addtolist)

(defun byte-compile-addremhook (form)
  ;; first arg is the name of a variable being changed, eg. (foo 'var ...)
  ;; only=20
  (if (>=3D (safe-length form) 2)
      (byte-compile-check-argvar (cadr form) 'varset-def))
  (byte-compile-normal-call form))
(byte-defop-compiler-1 add-hook    byte-compile-addremhook)
(byte-defop-compiler-1 remove-hook byte-compile-addremhook)

--=-=-=--




Information forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs@HIDDEN:
bug#6376; Package emacs. Full text available.

Message received at 6376 <at> debbugs.gnu.org:


Received: (at 6376) by debbugs.gnu.org; 12 Jun 2010 23:12:13 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Jun 12 19:12:13 2010
Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1ONZs4-0006SG-JQ
	for submit <at> debbugs.gnu.org; Sat, 12 Jun 2010 19:12:12 -0400
Received: from mailout2-15.pacific.net.au ([125.255.80.142]
	helo=mailout2.pacific.net.au)
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <gg@HIDDEN>) id 1ONZs0-0006Ry-T4
	for 6376 <at> debbugs.gnu.org; Sat, 12 Jun 2010 19:12:10 -0400
Received: from mailproxy1.pacific.net.au (mailproxy1.pacific.net.au
	[61.8.2.162])
	by mailout2.pacific.net.au (Postfix) with ESMTP id 089761EA8B6;
	Sun, 13 Jun 2010 09:12:03 +1000 (EST)
Received: from blah.blah (ppp2DB1.dyn.pacific.net.au [61.8.45.177])
	by mailproxy1.pacific.net.au (Postfix) with ESMTP id 1F7558C03;
	Sun, 13 Jun 2010 09:12:01 +1000 (EST)
Received: from gg by blah.blah with local (Exim 4.71)
	(envelope-from <gg@HIDDEN>)
	id 1ONZrX-00055F-Qm; Sun, 13 Jun 2010 09:11:39 +1000
From: Kevin Ryde <user42@HIDDEN>
To: Stefan Monnier <monnier@HIDDEN>
Subject: Re: bug#6376: 23.2; byte compile add-to-list report free variable
References: <87y6eqia4d.fsf@HIDDEN> <jwvzkz56lrq.fsf-monnier+emacs@HIDDEN>
Date: Sun, 13 Jun 2010 09:11:39 +1000
In-Reply-To: <jwvzkz56lrq.fsf-monnier+emacs@HIDDEN> (Stefan Monnier's message
	of "Tue, 08 Jun 2010 21:29:20 -0400")
Message-ID: <871vcbluc4.fsf@HIDDEN>
User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.2 (gnu/linux)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Spam-Score: -2.7 (--)
X-Debbugs-Envelope-To: 6376
Cc: 6376 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.11
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/pipermail/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -3.6 (---)

Stefan Monnier <monnier@HIDDEN> writes:
>
> add-hook

I was thinking of excusing add-hook and remove-hook because they can
work on an unbound variable, and might quite often do so if tying in to
something external.

> add-to-list is not only a "varset" but also a "varref"

I thought to show the message about setting in that case.  For the
unboundness check the two only differ in the wording I think.




Information forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs@HIDDEN:
bug#6376; Package emacs. Full text available.

Message received at 6376 <at> debbugs.gnu.org:


Received: (at 6376) by debbugs.gnu.org; 9 Jun 2010 01:29:26 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Tue Jun 08 21:29:26 2010
Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1OMA6g-0001ga-6Q
	for submit <at> debbugs.gnu.org; Tue, 08 Jun 2010 21:29:26 -0400
Received: from ironport2-out.teksavvy.com ([206.248.154.183]
	helo=ironport2-out.pppoe.ca)
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <monnier@HIDDEN>) id 1OMA6f-0001gV-6O
	for 6376 <at> debbugs.gnu.org; Tue, 08 Jun 2010 21:29:25 -0400
X-IronPort-Anti-Spam-Filtered: true
X-IronPort-Anti-Spam-Result: AvsEAMOKDkxFxIwr/2dsb2JhbACeQ3K/b4UWBIxs
X-IronPort-AV: E=Sophos;i="4.53,387,1272859200"; d="scan'208";a="67640100"
Received: from 69-196-140-43.dsl.teksavvy.com (HELO pastel.home)
	([69.196.140.43])
	by ironport2-out.pppoe.ca with ESMTP; 08 Jun 2010 21:29:21 -0400
Received: by pastel.home (Postfix, from userid 20848)
	id EF2598175; Tue,  8 Jun 2010 21:29:20 -0400 (EDT)
From: Stefan Monnier <monnier@HIDDEN>
To: Kevin Ryde <user42@HIDDEN>
Subject: Re: bug#6376: 23.2; byte compile add-to-list report free variable
Message-ID: <jwvzkz56lrq.fsf-monnier+emacs@HIDDEN>
References: <87y6eqia4d.fsf@HIDDEN>
Date: Tue, 08 Jun 2010 21:29:20 -0400
In-Reply-To: <87y6eqia4d.fsf@HIDDEN> (Kevin Ryde's message of "Tue, 08 Jun
	2010 11:30:42 +1000")
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Spam-Score: -1.9 (-)
X-Debbugs-Envelope-To: 6376
Cc: 6376 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.11
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/pipermail/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -1.9 (-)

> I made a mistake with a variable name to add-to-list and wondered if the
> byte compiler might report

>     (add-to-list 'nosuchvariable ...)

> as a free variable, similar to the report for

I use the code below for add-hook:

   (byte-defop-compiler-1 remove-hook byte-compile-add-hook)
   (defun byte-compile-add-hook (form)
     (let ((sym (car-safe (cdr-safe form))))
       (when (and (eq 'quote (car-safe sym))
                  (setq sym (car-safe (cdr sym)))
                  (symbolp sym))
         ;; Gross hack: We want to do the sanity checks just as we would for
         ;; a setq so we first do a setq and then pop the byte-code that was
         ;; just pushed by byte-compile-variable-ref.
         (let ((byte-compile-output byte-compile-output)
               (byte-compile-maxdepth byte-compile-maxdepth)
               (byte-compile-depth byte-compile-depth))
           (byte-compile-variable-ref 'byte-varset sym)))
       (byte-compile-normal-call form)))

so I'm in favor of such a change, but it would need to be cleaned up such
that your byte-compile-check-assign is also used for
byte-compile-variable-ref (i.e. the code should be hoisted out of
byte-compile-variable-ref).  Also, note that add-to-list is not only
a "varset" but also a "varref" (it reads the var before assigning to it).


        Stefan




Information forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs@HIDDEN:
bug#6376; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 8 Jun 2010 01:55:15 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Jun 07 21:55:15 2010
Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1OLo27-0007UZ-4H
	for submit <at> debbugs.gnu.org; Mon, 07 Jun 2010 21:55:15 -0400
Received: from mx10.gnu.org ([199.232.76.166])
	by debbugs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <gg@HIDDEN>) id 1OLo25-0007UU-Tr
	for submit <at> debbugs.gnu.org; Mon, 07 Jun 2010 21:55:14 -0400
Received: from lists.gnu.org ([199.232.76.165]:36445)
	by monty-python.gnu.org with esmtps
	(TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60)
	(envelope-from <gg@HIDDEN>) id 1OLo22-0003Ah-9Z
	for submit <at> debbugs.gnu.org; Mon, 07 Jun 2010 21:55:10 -0400
Received: from [140.186.70.92] (port=52574 helo=eggs.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.43) id 1OLo20-0008PJ-Bm
	for bug-gnu-emacs@HIDDEN; Mon, 07 Jun 2010 21:55:09 -0400
X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on eggs.gnu.org
X-Spam-Level: 
X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE
	autolearn=unavailable version=3.3.1
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69)
	(envelope-from <gg@HIDDEN>) id 1OLo1y-0008Lj-PM
	for bug-gnu-emacs@HIDDEN; Mon, 07 Jun 2010 21:55:08 -0400
Received: from mailout1-3.pacific.net.au ([61.8.2.210]:60410
	helo=mailout1.pacific.net.au) by eggs.gnu.org with esmtp (Exim 4.69)
	(envelope-from <gg@HIDDEN>) id 1OLo1y-0008LQ-1q
	for bug-gnu-emacs@HIDDEN; Mon, 07 Jun 2010 21:55:06 -0400
Received: from mailproxy2.pacific.net.au (mailproxy2.pacific.net.au
	[61.8.2.163])
	by mailout1.pacific.net.au (Postfix) with ESMTP id 83DE15A8649
	for <bug-gnu-emacs@HIDDEN>; Tue,  8 Jun 2010 11:30:56 +1000 (EST)
Received: from blah.blah (ppp287C.dyn.pacific.net.au [61.8.40.124])
	by mailproxy2.pacific.net.au (Postfix) with ESMTP id 879E127410
	for <bug-gnu-emacs@HIDDEN>; Tue,  8 Jun 2010 11:30:55 +1000 (EST)
Received: from gg by blah.blah with local (Exim 4.71)
	(envelope-from <gg@HIDDEN>) id 1OLneM-0002dO-Sf
	for bug-gnu-emacs@HIDDEN; Tue, 08 Jun 2010 11:30:42 +1000
From: Kevin Ryde <user42@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: 23.2; byte compile add-to-list report free variable
Date: Tue, 08 Jun 2010 11:30:42 +1000
Message-ID: <87y6eqia4d.fsf@HIDDEN>
User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.2 (gnu/linux)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older,
	4)
X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6,
	seldom 2.4 (older, 4)
X-Spam-Score: -5.6 (-----)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.11
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <http://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <http://debbugs.gnu.org/pipermail/debbugs-submit>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <http://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>,
	<mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Sender: debbugs-submit-bounces <at> debbugs.gnu.org
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
X-Spam-Score: -5.6 (-----)

I made a mistake with a variable name to add-to-list and wondered if the
byte compiler might report

    (add-to-list 'nosuchvariable ...)

as a free variable, similar to the report for

    (set 'nosuchvariable ...)

The few lines below get the desired effect, but might be a bit rough.
The separate check-assign is with a view to sharing among set, setq,
set-default and maybe others.

Something specific for add-to-list would be vaguely reasonable since
it's a builtin, but it could be worth trying a general way to flag an
arg number as the name of a variable either referenced or assigned.
Something like that might also allow "function not known to be defined"
for quoted symbol args to mapcar, sort, etc.

I suppose this sort of thing might be analysed by elint instead or as
well, but the byte compiler has good information about bindings
available after macro expansion etc and it's much more often run than
elint.



(defun byte-compile-check-assign (symbol)
  (cond ((and (byte-compile-warning-enabled-p 'constants)
              (byte-compile-const-symbol-p symbol t))
         (byte-compile-warn "variable assignment to constant `%s'" symbol))

        ((and (byte-compile-warning-enabled-p 'free-vars)
              (not (memq symbol byte-compile-bound-variables))
              (not (memq symbol byte-compile-free-assignments)))
         (byte-compile-warn "assignment to free variable `%s'" symbol)
         (push symbol byte-compile-free-assignments))))

(defun byte-compile-varsym1 (form)
  ;; arg 1 is a symbol which is the name of a variable which must be found
  ;; eg. (foo 'var ...)
  (cond ((eq 'quote (car-safe (nth 1 form)))
         (let ((var (car-safe (cdr (nth 1 form)))))
           (if (symbolp var)
               (byte-compile-check-assign var))))
        ;; (add-to-list 'nil ...) reaches here as (add-to-list nil ...),
        ;; call the check so as to report nil as a constant
        ((null (nth 1 form))
         (byte-compile-check-assign nil)))
  (byte-compile-normal-call form))

(byte-defop-compiler-1 add-to-list         byte-compile-varsym1)
(byte-defop-compiler-1 add-to-ordered-list byte-compile-varsym1)



In GNU Emacs 23.2.1 (i486-pc-linux-gnu, GTK+ Version 2.20.0)
 of 2010-05-16 on raven, modified by Debian
configured using `configure  '--build' 'i486-linux-gnu' '--build' 'i486-linux-gnu' '--prefix=/usr' '--sharedstatedir=/var/lib' '--libexecdir=/usr/lib' '--localstatedir=/var/lib' '--infodir=/usr/share/info' '--mandir=/usr/share/man' '--with-pop=yes' '--enable-locallisppath=/etc/emacs23:/etc/emacs:/usr/local/share/emacs/23.2/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/23.2/site-lisp:/usr/share/emacs/site-lisp:/usr/share/emacs/23.2/leim' '--with-x=yes' '--with-x-toolkit=gtk' '--with-toolkit-scroll-bars' 'build_alias=i486-linux-gnu' 'CFLAGS=-DDEBIAN -g -O2' 'LDFLAGS=-g' 'CPPFLAGS=''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: en_AU
  value of $XMODIFIERS: nil
  locale-coding-system: iso-latin-1-unix
  default enable-multibyte-characters: t




Acknowledgement sent to Kevin Ryde <user42@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs@HIDDEN:
bug#6376; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Thu, 19 Nov 2020 04:15:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.