From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#73725: Master: Wrong position for byte compiler warning message. Date: Fri, 11 Oct 2024 19:45:18 -0400 Message-ID: References: Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6835"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= , 73725@debbugs.gnu.org To: Alan Mackenzie Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Oct 12 01:46:23 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1szPKx-0001et-08 for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 12 Oct 2024 01:46:23 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1szPKc-0000F4-0u; Fri, 11 Oct 2024 19:46:02 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1szPKQ-0000Ej-Qa for bug-gnu-emacs@gnu.org; Fri, 11 Oct 2024 19:45:51 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1szPKP-0004fE-Cx for bug-gnu-emacs@gnu.org; Fri, 11 Oct 2024 19:45:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:In-Reply-To:From:To:Subject; bh=luh7RFb4rBzkZWPMnGy837jdSvStTG6joNH8up1PLnY=; b=YLVsr0+qLNUlVYm9bHDdGyNlV5C05jc4xqpvSZjXwagxx4G0e8hx4iz48HL2fLsIQ3GK5+BdR8CAfOi6ZRWbYL5grUMaIPgOABqPPqvyo2N8WHizImGzLL0t+Rq6aGQmwcyFqGZf3istBHaLanO6aga6h0P1tS5xuYYXN12zW/WEuqpkaWGysLblt83mWTxrhJLZAnnrZXLMFDsyjHF3IzO2135KzSPBdenjHp4XcOJ0YQv/zoU6So/oxvultehW30hufnzNEdlfRUKPBZtbGXWunPxW1zUN/P9kX8Go/BrjFQW16q/Ss2tW7fVJroqKFEBVm++mSG3nxIbiC4oG4g==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1szPKc-0002wB-G5 for bug-gnu-emacs@gnu.org; Fri, 11 Oct 2024 19:46:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 11 Oct 2024 23:46:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 73725 X-GNU-PR-Package: emacs Original-Received: via spool by 73725-submit@debbugs.gnu.org id=B73725.172869034211258 (code B ref 73725); Fri, 11 Oct 2024 23:46:02 +0000 Original-Received: (at 73725) by debbugs.gnu.org; 11 Oct 2024 23:45:42 +0000 Original-Received: from localhost ([127.0.0.1]:35690 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1szPKI-0002vV-8X for submit@debbugs.gnu.org; Fri, 11 Oct 2024 19:45:42 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:32493) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1szPKG-0002vH-2d for 73725@debbugs.gnu.org; Fri, 11 Oct 2024 19:45:40 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 1E4C1100055; Fri, 11 Oct 2024 19:45:21 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1728690319; bh=hLoiia8p+YNSqk4s6KM2kAYL+DBkOQJZxzkTXSIIi2o=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=Dz7zfEvlsd5Ib5rb2LW8PKl1arMrZ3tBwI6e0fonlrggy8R0xACqz/K57HYcDFz0z 8iuNU7latLTQKdc1FuoN3Em18HcdhPl092iC0D00XMK6bkdhccynd9CUeMBz1athjC 1SdZS7KvgFGzlj7q9DwHRjcySM5bHrDdS0+eL7M1w/+9uz+p968krZUrojRNEq5VlB vrP6i3Vjk/8P/tXGM3Rk0WipsvBt5saJlLSiaSOx/q6aVTJDVlb8XDVFcOAhU9kYK2 TeSpfhzeOg6bAgHUtc/Mxo5G3toeG1XZlJt//U0qkEzVVCYOesHntmzaHf07fzfalj +UVP13co2gOhw== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 2AF15100042; Fri, 11 Oct 2024 19:45:19 -0400 (EDT) Original-Received: from pastel (104-195-209-82.cpe.teksavvy.com [104.195.209.82]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id F187B120849; Fri, 11 Oct 2024 19:45:18 -0400 (EDT) In-Reply-To: (Alan Mackenzie's message of "Thu, 10 Oct 2024 10:22:49 +0000") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:293391 Archived-At: > (i) Create the following file, bad-error-position2.el: > > ######################################################################### > ;; -*- lexical-binding:t -*- > (eval-and-compile > (defmacro increase () > `(let ((foo (point-max))) > (cond > ((consp foo) > (message "consp %s" foo) > foo) > ((numberp foo) > (1+ fooo)) ; Note the misspelling. > (t (message "Something else: %s" foo)))))) > > (defun call-increase (bar) > (cond > ((not (or (consp bar) > (numberp bar))) > bar) > (t (increase)))) > ######################################################################### > > Note the misspelling of `foo' as `fooo' on line 10. > > (ii) emacs -Q > (iii) M-x byte-compile-file /path/to/bad-error-position2.el. > (iv) This gives the warning message: > > bad-error-position2.el:14:4: Warning: reference to free variable `fooo' > > (v) The position 14:4 is wrong. That is the position of the `cond' > symbol in `call-increase'. > (vi) The correct message should be: > > bad-error-position2.el:18:8: Warning: reference to free variable `fooo' > > .. 18:8 is the position of `increase' on the last line of the file. Nitpick: one could also argue that the "correct" message should point to line 8 where is the `fooo` typo. > +(defun sub-macroexp--posify-form (form call-pos depth) > + "Try to apply the transformation of `macroexp--posify-form' to FORM. > +FORM and CALL-POS are as in that function. DEPTH is a small integer, > +decremented at each recursive call, to prevent infinite recursion. > +Return the changed form, or nil if no change happened." > + (let (new-form > + ) > + (cond > + ((zerop depth) nil) > + ((and (consp form) > + (symbolp (car form)) > + (car form)) > + (setcar form (position-symbol (car form) call-pos)) > + form) > + ((consp form) > + (or (when (setq new-form (sub-macroexp--posify-form > + (car form) call-pos (1- depth))) > + (setcar form new-form) > + form) > + (when (setq new-form (sub-macroexp--posify-form > + (cdr form) call-pos (1- depth))) > + (setcdr form new-form) > + form))) > + ((symbolp form) > + (if form ; Don't position nil! > + (position-symbol form call-pos))) > + ((and (or (vectorp form) (recordp form))) > + (let ((len (length form)) > + (i 0) > + ) > + (while (and (< i len) > + (not (setq new-form (sub-macroexp--posify-form > + (aref form i) call-pos (1- depth))))) > + (setq i (1+ i))) > + (when (< i len) > + (aset form i new-form) > + form)))))) That sounds potentially costly Have you tried to measure the performance impact in some "typical" cases? While reading your description I was naively thinking: we can probably fix it with a trivial patch like: diff --git a/lisp/emacs-lisp/macroexp.el b/lisp/emacs-lisp/macroexp.el index 19daa57b207..5cc471e32f6 100644 --- a/lisp/emacs-lisp/macroexp.el +++ b/lisp/emacs-lisp/macroexp.el @@ -246,6 +246,7 @@ macroexp-macroexpand (let* ((macroexpand-all-environment env) new-form) (while (not (eq form (setq new-form (macroexpand-1 form env)))) + (push form byte-compile-form-stack) (let ((fun (car-safe form))) (setq form (if (and fun (symbolp fun) Have you tried something like this? If it doesn't work, do you happen to know why? Stefan