Skip to content

Commit 66ba341

Browse files
committed
fix a bug in generating struct-related bindings
closes #1050
1 parent 1f179e4 commit 66ba341

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

typed-racket-lib/typed-racket/typecheck/def-binding.rkt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
(define-struct binding (name) #:transparent)
88
(define-struct (def-binding binding) (ty) #:transparent)
99
(define-struct (def-stx-binding binding) () #:transparent)
10-
(define-struct (def-struct-stx-binding def-stx-binding) (static-info constructor-type) #:transparent)
10+
(define-struct (def-struct-stx-binding def-stx-binding) (static-info constructor-type extra-ctor-name) #:transparent)
1111

1212
(provide/cond-contract
1313
(struct binding ([name identifier?]))
1414
(struct (def-binding binding) ([name identifier?] [ty any/c]))
1515
(struct (def-stx-binding binding) ([name identifier?]))
1616
(struct (def-struct-stx-binding binding) ([name identifier?]
1717
[static-info (or/c #f struct-info?)]
18-
[constructor-type any/c])))
18+
[constructor-type any/c]
19+
[extra-ctor-name (or/c #f identifier?)])))

typed-racket-lib/typed-racket/typecheck/provide-handling.rkt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(types struct-table)
1111
(utils tc-utils)
1212
(env env-utils)
13+
(base-env type-name-error)
1314
(for-syntax racket/base)
1415
(for-template racket/base))
1516

@@ -85,24 +86,25 @@
8586
(match-lambda
8687
[(def-binding _ ty)
8788
(mk-value-quad internal-id new-id ty)]
88-
[(def-struct-stx-binding _ (? struct-info? si) constr-type)
89-
(mk-struct-syntax-quad internal-id new-id si constr-type)]
89+
[(def-struct-stx-binding _ (? struct-info? si) constr-type extra-ctor-name)
90+
(mk-struct-syntax-quad internal-id new-id si constr-type extra-ctor-name)]
9091
[(def-stx-binding _)
9192
(mk-syntax-quad internal-id new-id)])]
9293
;; otherwise, not defined in this module, not our problem
9394
[else (mk-ignored-quad internal-id)]))
9495

9596
;; mk-struct-syntax-quad : identifier? identifier? struct-info? Type? -> quad/c
9697
;; This handles `(provide s)` where `s` was defined with `(struct s ...)`.
97-
(define (mk-struct-syntax-quad internal-id new-id si constr-type)
98-
(define type-is-constructor? #t) ;Conservative estimate (provide/contract does the same)
98+
(define (mk-struct-syntax-quad internal-id new-id si constr-type extra-ctor-name)
9999
(match-define (list type-desc constr pred (list accs ...) muts super) (extract-struct-info si))
100100
(define-values (defns export-defns new-ids aliases)
101101
(for/lists (defns export-defns new-ids aliases)
102102
([e (in-list (list* type-desc pred super accs))])
103103
(if (identifier? e)
104104
(mk e)
105105
(mk-ignored-quad e))))
106+
107+
(define type-is-constructor? (or (free-identifier=? new-id constr) extra-ctor-name))
106108
;; Here, we recursively handle all of the identifiers referenced
107109
;; in this static struct info.
108110
(define-values (constr-defn constr-export-defn constr-new-id constr-aliases)
@@ -143,7 +145,8 @@
143145
(list #,@(map (lambda (x) #'#f) accs)) super*)))
144146
#,(if type-is-constructor?
145147
#'(make-struct-info-self-ctor constr* info)
146-
#'info)))
148+
#'(lambda (stx)
149+
(type-name-error stx)))))
147150
(define-syntax export-id
148151
(make-rename-transformer #'protected-id)))
149152
#'export-id

typed-racket-lib/typed-racket/typecheck/tc-structs.rkt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@
300300
(cons
301301
(make-def-struct-stx-binding (struct-names-type-name names)
302302
si
303-
(def-binding-ty constructor-binding))
303+
(def-binding-ty constructor-binding)
304+
extra-constructor)
304305
bindings)))
305306

306307
(define/cond-contract (register-non-prefab-bindings! sty names desc si)
@@ -378,7 +379,8 @@
378379
(values (cons constructor-binding extra-constructor-bindings)
379380
(append (cons (make-def-struct-stx-binding (struct-names-type-name names)
380381
si
381-
constructor-type)
382+
constructor-type
383+
extra-constructor)
382384
extra-constructor-bindings)
383385
bindings))))
384386

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#;
2+
(exn-pred exn:fail:syntax? #rx"type-check: type name used out of context.*? type: Exp")
3+
#lang typed/racket/base
4+
5+
(module typed1 typed/racket/base
6+
(provide (except-out (all-defined-out) make-exp))
7+
(struct Exp () #:constructor-name make-exp))
8+
9+
(require 'typed1)
10+
(Exp)

0 commit comments

Comments
 (0)