typed-racket
typed-racket copied to clipboard
Requiring a cstruct within a typed racket module fails with "identifier `struct:id' not included in nested require spec"
$ racket --version
Welcome to Racket v7.0.
;;; c.rkt
#lang racket
(provide (struct-out color))
(require ffi/unsafe)
(define-cstruct _color ([r _uint8]))
;;; use_c.rkt
#lang typed/racket
(require/typed "c.rkt"
[#:struct color ([r : Real])])
(define a-color (color 9))
;;; Error
use_c.rkt:3:15: only-in: identifier `struct:color' not included in nested require spec
at: "c.rkt"
in: (only-in "c.rkt" struct:color (color color2))
location...:
use_c.rkt:3:15
context...:
raise-syntax-error
/usr/share/racket/collects/racket/private/reqprov.rkt:439:5
/usr/share/racket/collects/racket/require-transform.rkt:266:2: expand-import
/usr/share/racket/collects/racket/private/reqprov.rkt:266:21: try-next
/usr/share/racket/collects/racket/private/reqprov.rkt:243:2: require
apply-transformer-in-context
apply-transformer52
dispatch-transformer41
loop
pass-1-and-2-loop
module-begin-k
do-local-expand50
/usr/share/racket/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:94:0: tc-module/full
/usr/share/racket/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:23:4: module-begin
apply-transformer-in-context
apply-transformer52
...
The potential cause of this:
lexi-lambda: The define-cstruct form creates a structure type transformer binding with #f for the descriptor id, but TR appears to ignore that and forge one by prepending
struct:to the name, which is wrong in more general ways.
At the moment this can be worked around by doing the following:
;;; c.rkt
#lang racket
(provide color? make-color)
(require ffi/unsafe)
(define-cstruct _color ([r _uint8]))
;;; use_c.rkt
#lang typed/racket
(require/typed "c.rkt"
[#:opaque color color?]
[make-color (-> Real color)])
(struct object ([c : color]) #:transparent)
(define o (object (make-color 5)))
(display o)
;;;
$ racket use_c.rkt
#(struct:object #<cpointer:color>)
I just ran into this issue using Racket 8.4 when trying to wrap cstrucst in the racket-raylib library
This issue has been mentioned on Racket Discussions. There might be relevant details there:
https://racket.discourse.group/t/how-do-i-add-types-to-these-wrapped-function-in-typed-racket/783/1