Skip to content

Commit f5fca70

Browse files
authored
fix subtyping between struct descriptor types & has-struct-property (#1047)
a struct descriptor associcated with a property name should be a valid argument to its property accessor
1 parent 374efcc commit f5fca70

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

typed-racket-lib/typed-racket/types/subtype.rkt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@
590590
(cons portable-fixnum? -NonNegFixnum)
591591
(cons values -Nat)))
592592

593+
(define (valid-prop-name? name properties)
594+
(and (free-id-set-member? properties name) (Struct-Property? (lookup-id-type/lexical name))))
593595

594596
(define-rep-switch (subtype-cases A (#:switch t1) t2 obj)
595597
;; NOTE: keep these in alphabetical order
@@ -1201,12 +1203,8 @@
12011203
[(StructTop: (Struct: nm2 _ _ _ _ _ _))
12021204
#:when (free-identifier=? nm1 nm2)
12031205
A]
1204-
[(Has-Struct-Property: prop-name)
1205-
(cond
1206-
[(free-id-set-member? properties prop-name)
1207-
(match (lookup-id-type/lexical prop-name)
1208-
[(? Struct-Property?) A])]
1209-
[else #f])]
1206+
[(Has-Struct-Property: prop-name) #:when (valid-prop-name? prop-name properties)
1207+
A]
12101208
[(Val-able: (? (negate struct?) _)) #f]
12111209
;; subtyping on structs follows the declared hierarchy
12121210
[_ (cond
@@ -1223,6 +1221,11 @@
12231221
[(case: StructType (StructType: t1*))
12241222
(match t2
12251223
[(StructTypeTop:) A]
1224+
[(Has-Struct-Property: prop-name)
1225+
(match t1*
1226+
[(Struct: _ _ _ _ _ _ properties) #:when (valid-prop-name? prop-name properties)
1227+
A]
1228+
[else #f])]
12261229
[_ (continue<: A t1 t2 obj)])]
12271230
[(case: Syntax (Syntax: elem1))
12281231
(match t2

typed-racket-test/succeed/structs-has-subtype.rkt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
(: prop-ins-to-num-ref (Some (X) (-> (Has-Struct-Property prop-ins-to-num) (-> X Number) : X) ))
77
(define-values (prop-ins-to-num prop-ins-to-num? prop-ins-to-num-ref) (make-struct-type-property 'prop-ins-to-num))
88

9-
;; (: bar? : Any -> Boolean : (Has-Struct-Property prop-ins-to-num))
10-
;; (define bar? prop-ins-to-num?)
119

12-
13-
; (struct (X Y) helloworld ([x : Y] [y : Y]) #:property prop-ins-to-num (cons 20 40))
14-
15-
(struct posn ([x : Integer] [y : Integer]) #:property prop-ins-to-num (λ ([self : posn])
16-
20))
10+
(struct posn ([x : Integer] [y : Integer])
11+
#:property prop-ins-to-num
12+
(λ ([self : posn])
13+
20))
1714

1815
(: p1 posn)
1916
(define p1 (posn 100 200))
2017
(posn-x p1)
18+
19+
(prop-ins-to-num-ref struct:posn)
2120
(: val Number)
2221
(define val ((prop-ins-to-num-ref p1) p1))

0 commit comments

Comments
 (0)