Skip to content

Commit

Permalink
Fix listing of auto field mutators.
Browse files Browse the repository at this point in the history
Fix structs incorrectly including non-auto field mutators as auto fields.
  • Loading branch information
kurinoku committed Nov 16, 2020
1 parent 5656073 commit c6fe634
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions racket/collects/racket/private/define-struct.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"stx.rkt" "stxcase-scheme.rkt" "qq-and-or.rkt" "cond.rkt"
"define-et-al.rkt"
"stxloc.rkt" "qqstx.rkt"
"struct-info.rkt"
"struct-util.rkt"))
"struct-info.rkt"))

(#%provide define-struct*
define-struct/derived
Expand Down Expand Up @@ -593,15 +592,19 @@
0
fld-size))

(define-values (sets field-to-mutator-directives)
(let loop ([fields fields])
(define-values (sets field-to-mutator-directives sets-auto-count)
(let loop ([fields fields]
[count 0])
(cond
[(null? fields) (values null null)]
[(null? fields) (values null null count)]
[(not (or mutable? (field-mutable? (car fields))))
(loop (cdr fields))]
(loop (cdr fields) count)]
[else
(define-values (other-sets other-directives)
(loop (cdr fields)))
(define count* (if (field-auto? (car fields))
(+ count 1)
count))
(define-values (other-sets other-directives count**)
(loop (cdr fields) count*))
(define this-set
(build-name id ; (field-id (car fields))
"set-"
Expand All @@ -613,7 +616,8 @@
(cons (field-to-selector/mutator-directive (car fields)
this-set
#f)
other-directives))])))
other-directives)
count**)])))

(define all-directives
(append
Expand Down Expand Up @@ -746,7 +750,7 @@
(map protect (car super-autos))
null))
(list #,@(map protect
(list-tail sets (max 0 (- (length sets) auto-count))))
(list-tail sets (max 0 (- (length sets) sets-auto-count))))
#,@(if super-autos
(map protect (cadr super-autos))
null))))
Expand Down Expand Up @@ -900,6 +904,16 @@
[(null? xs) xs]
[else (cons (car xs) (take (cdr xs) (sub1 n)))]))

;; modified from racket/collects/racket/contract/private/provide.rkt
(define-for-syntax (predicate->struct-name orig-stx stx)
(cond
[(regexp-match #rx"^(.*)[?]$" (format "~a" (syntax-e stx))) => cadr]
[else
(raise-syntax-error
#f
"unable to cope with a struct type whose predicate doesn't end with `?'"
orig-stx)]))

(define-for-syntax (find-accessor/no-field-info the-struct-info fld stx)
(define accessors (list-ref the-struct-info 3))
(define parent (list-ref the-struct-info 5))
Expand All @@ -911,7 +925,7 @@
0))
(define num-own-fields (- num-fields num-super-fields))
(define own-accessors (take accessors num-own-fields))
(define struct-name (predicate->struct-name #f stx (list-ref the-struct-info 2)))
(define struct-name (predicate->struct-name stx (list-ref the-struct-info 2)))
(define accessor-name (string->symbol (format "~a-~a" struct-name (syntax-e fld))))
(or (findf (λ (a) (eq? accessor-name (syntax-e a))) own-accessors)
(raise-syntax-error
Expand Down

0 comments on commit c6fe634

Please sign in to comment.