From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] Prevent 'error' from being tail-called, for better diagnostics Date: Mon, 30 Jan 2012 01:39:40 -0500 Message-ID: <87pqe1ithv.fsf@netris.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: dough.gmane.org 1327905666 6008 80.91.229.3 (30 Jan 2012 06:41:06 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 30 Jan 2012 06:41:06 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Mon Jan 30 07:41:06 2012 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1RrkvB-0002DP-R4 for guile-devel@m.gmane.org; Mon, 30 Jan 2012 07:40:57 +0100 Original-Received: from localhost ([::1]:42781 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RrkvB-0003Qg-Cj for guile-devel@m.gmane.org; Mon, 30 Jan 2012 01:40:57 -0500 Original-Received: from eggs.gnu.org ([140.186.70.92]:50066) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rrkv8-0003Qa-Fz for guile-devel@gnu.org; Mon, 30 Jan 2012 01:40:55 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Rrkv7-0007Me-8J for guile-devel@gnu.org; Mon, 30 Jan 2012 01:40:54 -0500 Original-Received: from world.peace.net ([96.39.62.75]:56736) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rrkv7-0007MX-3j for guile-devel@gnu.org; Mon, 30 Jan 2012 01:40:53 -0500 Original-Received: from 209-6-91-212.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com ([209.6.91.212] helo=yeeloong) by world.peace.net with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.69) (envelope-from ) id 1Rrkv1-0006bf-7f; Mon, 30 Jan 2012 01:40:47 -0500 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 96.39.62.75 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:13740 Archived-At: --=-=-= Content-Type: text/plain Hello all, Paul Smith got a rather unhelpful error message from Guile when he called 'error' in tail position from one of his procedures, and asked for input on how to improve error reporting, so I've been looking into it. In this case, the error came from a procedure like this: (define (to-string-maybe x) (cond ;; Many clauses here (else (error "Unknown object:" x)))) Since 'error' is called in tail position, 'to-string-maybe' is missing from the backtrace, making the error message rather mysterious. Of course a simple fix would be to include the name of the procedure within the string passed to 'error', but this really shouldn't be necessary. I would be rather embarrassed to suggest such a thing. We should be able to do much better than this. When 'error' is called, the user should _automatically_ be told the exact source location where it was called, and the procedure that called it should be present in the backtrace. Therefore, we should somehow prevent it from being tail-called. Any ideas on how best to accomplish this? One idea is to change 'error' into a macro, and use tricks similar to what 'load' does, to cleverly makes it look like a procedure. This macro would, in the common case, force the call to the "real" error procedure to be done in push context instead of tail context. Maybe something along the lines of the attached patch. What do you think? Mark --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Prevent-error-from-being-tail-called-for-better-diag.patch Content-Description: [PATCH] Prevent 'error' from being tail-called, for better diagnostics >From 7914328a91d15639399f7c2926da4ae2fd793e84 Mon Sep 17 00:00:00 2001 From: Mark H Weaver Date: Mon, 30 Jan 2012 01:33:08 -0500 Subject: [PATCH] Prevent 'error' from being tail-called, for better diagnostics * module/ice-9/boot-9.scm (error): Rename 'error' to '%%error'. 'error' is now a variable transformer that calls '%%error' in non-tail-position when 'error' is found in operator position. When 'error' is found elsewhere, it simply expands to '%%error'. --- module/ice-9/boot-9.scm | 14 +++++++++++++- 1 files changed, 13 insertions(+), 1 deletions(-) diff --git a/module/ice-9/boot-9.scm b/module/ice-9/boot-9.scm index c8a56e0..2d3a254 100644 --- a/module/ice-9/boot-9.scm +++ b/module/ice-9/boot-9.scm @@ -1187,7 +1187,7 @@ VALUE." ;;; {Error Handling} ;;; -(define error +(define %%error (case-lambda (() (scm-error 'misc-error #f "?" #f #f)) @@ -1195,6 +1195,18 @@ VALUE." (let ((msg (string-join (cons "~A" (make-list (length args) "~S"))))) (scm-error 'misc-error #f msg (cons message args) #f))))) +(define-syntax error + (make-variable-transformer + (lambda (x) + (syntax-case x () + ((_ arg ...) + #'(call-with-values + (lambda () (%%error arg ...)) + values)) + (id + (identifier? #'id) + #'%%error))))) + ;;; {Time Structures} -- 1.7.5.4 --=-=-=--