Top-level define-record-type errors
When using define-record-type
, the code calling the constructor must be in a separate file or wrapped in a let
or lambda
form.
Here is an example. Place the following code into a file, eg bug.scm
, and compile it with guild compile-wasm
:
(define-record-type <samp>
(make-samp arg1)
samp?
(arg1 samp-arg1))
(make-samp 'arg)
This produces the following error:
Backtrace:
In ice-9/boot-9.scm:
1755:12 17 (with-exception-handler _ _ #:unwind? _ #:unwind-for-type _)
In unknown file:
16 (apply-smob/0 #<thunk 7fb76e2be300>)
In ice-9/boot-9.scm:
724:2 15 (call-with-prompt _ _ #<procedure default-prompt-handler (k proc)>)
In ice-9/eval.scm:
619:8 14 (_ #(#(#<directory (guile-user) 7fb76e2c1c80>)))
In /gnu/store/hv336kak62jzghiydfr8brl376nbl43f-profile/bin/guild:
72:17 13 (main _)
In scripts/compile-wasm.scm:
252:10 12 (compile-wasm . _)
In ice-9/ports.scm:
433:17 11 (call-with-input-file _ _ #:binary _ #:encoding _ #:guess-encoding _)
In hoot/compile.scm:
313:18 10 (_ _)
229:4 9 (compile _ #:imports _ #:import-abi? _ #:export-abi? _ #:include-file _ # _ # _ …)
In hoot/library-group.scm:
713:7 8 (expand-library-group #<<library-group> src: #f libraries: (#<<library> src: …> …)
568:4 7 (tree-il->reversed-bindings #<tree-il (seq (define make-samp (lambda ((name .…> …)
534:19 6 (foldts #<tree-il (seq (define make-samp (lambda ((name . make-samp)) (lambda-…>)
534:19 5 (foldts #<tree-il (define make-samp (lambda ((name . make-samp)) (lambda-case …>)
534:19 4 (foldts #<tree-il (lambda ((name . make-samp)) (lambda-case (((arg1) #f #f #f …>)
534:19 3 (foldts #<tree-il (lambda-case (((arg1) #f #f #f () (arg1-adc2de3630231c2-6)) …>)
534:19 2 (foldts #<tree-il (call (@@ (%library-group89 hoot records) %make-struct) (top…>)
534:19 1 (fold-values #<procedure foldts (tree)> (#<tree-il (toplevel <samp>)> #<tree…>))
571:27 0 (_ _)
hoot/library-group.scm:571:27: unbound top-level #("bug.scm" 0 20) <samp>
The same occurs if the code is wrapped in a begin
. If it is instead wrapped in a let
or lambda
, it works fine. Similarly, it works fine if the constructor is exported from the file where the record is defined, and then imported and used in another file.
The same error can be reproduced in compile-value
if the code is wrapped in a begin
, but gives a slightly different error:
scheme@(guile-user)> ,use (hoot reflect)
scheme@(guile-user)> (compile-value '(begin
(define-record-type <samp>
(make-samp arg1)
samp?
(arg1 samp-arg1))
(make-samp 'arg)))
ice-9/boot-9.scm:1676:22: In procedure raise-exception:
unbound top-level #f <samp>