all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: "João Távora" <joaotavora@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: Attaching context info to an error
Date: Thu, 28 Dec 2023 11:03:43 -0500	[thread overview]
Message-ID: <jwvbkaafiyi.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <CALDnm51gurztb0dr-XF+dX6dZUX83H0oz3S4kMvPL+fBe3fUEA@mail.gmail.com> ("João Távora"'s message of "Thu, 28 Dec 2023 14:12:09 +0000")

> It's not just initialize-instance, of course, it's CLOS or
> an equivalent.

You wrote:

    [...] And then, CLOS is good :-) you get access to lots of existing
    useful protocols.  initialize-instance, print-object, etc for free.

Which implies that `initialize-instance` is a useful protocol.
I'd just be happy to see a concrete example of something useful you can
do with it that's easier than what we do with "old style Lisp".

> Top of my head, CLOS gives you hierarchies easily, with generic
> functions doing the right thing by default when called with an
> instance of the subclass.

You do realize that I wrote `cl-generic.el`, made changes to
`cl-defstruct` to better integrate with EIEIO, ...

> That's the "standard method combination" [1],

... and this as well, right?

> *** Welcome to IELM ***  Type (describe-mode) or press C-h m for help.
> ELISP> (defclass foo () ((bar :initarg :bar) (baz :initarg :baz)))
> foo
> ELISP> (make-instance 'foo :bar 42 :baz 42) ; so far so good
> #<foo foo-156aec4e27b2>
> ELISP> (defclass foo () ((bar :initarg :bar))) ; redef
> foo

Redefinition of a class is definitely not currently supported (with no
plan to support it in the foreseeable future).

But in the `jsonrpc.el` there is no redefinition, so it seems to be
another problem.  Can you open a bug with a small recipe that
reproduces the problem that you encountered while writing `jsonrpc.el`?

> In summary, I think the "context" you are thinking about can just
> be good old dynamic context, i.e. let-bindings of special
> variables, with the new HANDLER-BIND-powered twist that these
> variables can remain private to their users _and_ be augmented
> after they are initially established.
>
> Right?  Wrong?

That describes where the context is placed until the error is signaled.
The question is how/where to preserve (part of) this context information
when some code wants to use it *after* unwinding the stack.

E.g. in `bytecomp.el` on the handler-bind branch I solved it as follows:

	      (let ((form-stack nil))
		(condition-case error-info
		    (handler-bind
		        ((error (lambda (_err)
		                  (setq form-stack byte-compile-form-stack))))
		      (funcall body-fn))
		  (error (let ((byte-compile-form-stack form-stack))
		           (byte-compile-report-error error-info))))))))

where `byte-compile-form-stack` is basically your *FOO-DATA*, and
I preserve/propagate this info by stashing it in the
`form-stack` variable.

Maybe we could have gotten away with calling `byte-compile-report-error`
directly from the `handler-bind` handler, but that could be affected by
all kinds of dynamic let-bindings and whatnot.

Another example is in the C code: when we call `signal`, the C code
walks the backtrace to find the name of the inner function, stores it in
the global `Vsignaling_function` and then uses it in
`cmd_error(_internal)` (i.e. after unwinding the stack) to include it in
the error message.  The us of a global var here is not just ugly but
it's also wrong because other ELisp code can be executed between the
moment we store the info in the var and the moment we use it, so it can
be polluted by the info from some other error.
[ And yes, we could rewrite that code to do something like what I did
  in `bytecomp.el` above.  ]

>> The upside doesn't seem worth the cost for now.
> It'll be harder 40 years from now.

I guess we'll see :-)


        Stefan




  reply	other threads:[~2023-12-28 16:03 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-21 22:30 Attaching context info to an error Stefan Monnier
2023-12-22  6:50 ` Gerd Möllmann
2023-12-22  8:37   ` Gerd Möllmann
2023-12-22 15:58   ` Stefan Monnier
2023-12-28  6:57     ` Gerd Möllmann
2023-12-22 20:56 ` Jens Schmidt
2023-12-22 22:37   ` Stefan Monnier
2023-12-23  3:02 ` João Távora
2023-12-23  3:28   ` João Távora
2023-12-26 20:12     ` Stefan Monnier
2023-12-26 20:47   ` Stefan Monnier
2023-12-26 22:43     ` João Távora
2023-12-27  6:50       ` Gerd Möllmann
2023-12-27 10:29         ` João Távora
2023-12-27 10:35           ` Gerd Möllmann
2023-12-27 17:50       ` Stefan Monnier
2023-12-27 18:08         ` João Távora
2023-12-27 18:28           ` João Távora
2023-12-27 19:08           ` Stefan Monnier
2023-12-27 19:27             ` João Távora
2023-12-27 20:27               ` Stefan Monnier
2023-12-27 23:08                 ` João Távora
2023-12-28  7:05                   ` Stefan Monnier
2023-12-28 14:12                     ` João Távora
2023-12-28 16:03                       ` Stefan Monnier [this message]
2023-12-28 17:15                         ` João Távora
2023-12-28 19:22                           ` Stefan Monnier
2023-12-28 23:53                             ` João Távora
2023-12-29  2:54                               ` Stefan Monnier
2023-12-29  3:43                                 ` João Távora
2023-12-29 16:54                                   ` Stefan Monnier
2023-12-29 17:29                                     ` João Távora
2023-12-29 17:39                                       ` João Távora
2023-12-30  4:29                                       ` Stefan Monnier
2023-12-30 16:45                                         ` João Távora
2023-12-29 17:19                                   ` Alan Mackenzie
2023-12-29 17:24                                     ` João Távora
2023-12-29 17:43                                       ` Alan Mackenzie
2023-12-29 17:54                                         ` João Távora
2023-12-29 18:08                                           ` Alan Mackenzie
2023-12-29 18:45                                             ` João Távora
2023-12-29 18:35                                         ` Stefan Monnier
2023-12-29 18:48                                           ` João Távora

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvbkaafiyi.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    --cc=joaotavora@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.