guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: better error messages through assertions


From: Ludovic Courtès
Subject: Re: better error messages through assertions
Date: Wed, 30 Mar 2022 11:37:10 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)

Hi Philip,

Philip McGrath <philip@philipmcgrath.com> skribis:

> I'm thinking that a reasonable place to start might be to implement a
> `contract->sanitizer` form that would allow using contracts to create 
> sanitizers, ideally with no changes to `(guix records)`.

OK.  I’d prefer if people who define record types could directly write:

  (field getter (contract integer/c))

rather than:

  (field getter (sanitizer (contract->sanitizer integer/c)))

But that’s more of a detail.

> What is the preferred mechanism for exceptions?

For Guix code, SRFI-34/35.

> Likewise, what record system should I use?

SRFI-9.

(Perhaps we should put answers to these questions in the “Coding Style”
section of the manual.)

> Also, I don't know much about how the "abi" aspect of (guix records)
> works and what types of changes there would trigger rebuilds. (Though, 
> again, I hope no changes would be needed for the proof-of-concept phase.)

I don’t think you need to worry about that.

> Another problem here seems to be the fault of (srfi srfi-9). For example:

[...]

> scheme@(guile-user)> (container-contents '())
> ice-9/boot-9.scm:1685:16: In procedure raise-exception:
> In procedure struct-vtable: Wrong type argument in position 1
> (expecting struct): ()
>
> Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
> scheme@(guile-user) [1]> ,bt
> In current input:
>       3:0  1 (_)
> In ice-9/boot-9.scm:
>   1685:16  0 (raise-exception _ #:continuable? _)
> ```
>
> It seems like `container-contents` and other field accessors ought to
> check their arguments with `container?` (or the applicable predicate) 
> and not leave error reporting to `struct-vtable`.

SRFI-9 generates the smallest amount of code for the job:

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> ,use(srfi srfi-9)
scheme@(guile-user)> (define-record-type <foo>
                       (make-foo x)
                       foo?
                       (x foo-x))
scheme@(guile-user)> ,optimize (foo-x '())
$9 = (if (eq? (struct-vtable '()) <foo>)
  (struct-ref '() 0)
  (throw 'wrong-type-arg
         'foo-x
         "Wrong type argument: ~S"
         (list '())
         (list '())))
--8<---------------cut here---------------end--------------->8---

With Guile 3, it might be that adding an extra ‘struct?’ test would have
little effect on performance; we’d need to check.

> Perhaps this could be fixed in the (guix records) layer?

Could be, yes.

Thanks for looking into this!

Ludo’.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]