Skip to content

Commit 36e352e

Browse files
capfredfsamth
authored andcommitted
fix the constructor expr in the exported structure's struct-info
1 parent 11eba59 commit 36e352e

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
;; avoid generating the quad for constr twice.
117117
;; skip it when the binding is for the type name
118118
[(and (free-identifier=? internal-id sname) (free-identifier=? constr internal-id))
119-
(super-mk-quad (make-def-binding constr constr-type) (generate-temporary constr) def-tbl pos-blame-id mk-redirect-id)]
119+
(super-mk-quad (make-def-binding constr constr-type) (freshen-id constr) def-tbl pos-blame-id mk-redirect-id)]
120120
[else
121121
(make-quad constr def-tbl pos-blame-id mk-redirect-id)]))
122122

@@ -127,6 +127,13 @@
127127
(with-syntax* ([id internal-id]
128128
[export-id new-id]
129129
[protected-id (freshen-id #'id)]
130+
;; when the struct name is also the constructor name, we put
131+
;; the former in the struct info, because it is the
132+
;; exporting binding. Otherwise, we put the latter in the
133+
;; struct info
134+
[constr-in-si* (if (free-identifier=? new-id constr-new-id)
135+
new-id
136+
constr-new-id)]
130137
[type-name tname])
131138
(values
132139
#`(begin
@@ -146,7 +153,7 @@
146153
;; a protected version in the submodule, since that
147154
;; wouldn't be accessible by `syntax-local-value`.
148155
(define-syntax protected-id
149-
(let ((info (list type-desc* (syntax export-id) pred* (list accs* ...)
156+
(let ((info (list type-desc* (syntax constr-in-si*) pred* (list accs* ...)
150157
(list #,@(map (lambda (x) #'#f) accs)) super*)))
151158
(make-struct-info-wrapper* constr* info (syntax type-name) #,sname-is-constructor?)))
152159
(define-syntax export-id
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#lang racket/base
2+
(module test racket/base
3+
(require (for-syntax racket/base syntax/parse racket/struct-info racket/match))
4+
5+
(define-syntax (get-constr stx)
6+
(syntax-parse stx
7+
[(_ sname:id)
8+
(define si (extract-struct-info (syntax-local-value #'sname (lambda () #f))))
9+
(match-define (list* struct-desc constr rst) si)
10+
constr]))
11+
(provide get-constr))
12+
13+
(module typed typed/racket/base
14+
(require (submod ".." test))
15+
(require typed/rackunit)
16+
(struct kiwi ())
17+
(struct apple () #:constructor-name make-apple)
18+
(struct pear () #:type-name PEAR)
19+
(struct dragon-fruit () #:type-name DragonFruit #:constructor-name make-dragon-fruit)
20+
(provide (struct-out apple)
21+
(struct-out kiwi)
22+
(struct-out PEAR)
23+
(struct-out DragonFruit))
24+
25+
(check-equal? (object-name (get-constr apple)) 'make-apple)
26+
(check-equal? (object-name (get-constr kiwi)) 'kiwi)
27+
(check-equal? (object-name (get-constr apple)) 'make-apple)
28+
(check-equal? (object-name (get-constr pear)) 'pear)
29+
(check-equal? (object-name (get-constr dragon-fruit)) 'make-dragon-fruit)
30+
(check-equal? (object-name (get-constr DragonFruit)) 'make-dragon-fruit)
31+
(check-equal? (object-name (get-constr PEAR)) 'pear))
32+
33+
(require rackunit)
34+
(require 'typed)
35+
(require 'test)
36+
(check-equal? (object-name (get-constr apple)) 'make-apple)
37+
(check-equal? (object-name (get-constr kiwi)) 'kiwi)
38+
(check-equal? (object-name (get-constr apple)) 'make-apple)
39+
(check-equal? (object-name (get-constr DragonFruit)) 'make-dragon-fruit)
40+
(check-equal? (object-name (get-constr PEAR)) 'pear)

0 commit comments

Comments
 (0)