Skip to content

Commit

Permalink
Fixes to for with #:break and #:when together.
Browse files Browse the repository at this point in the history
The original implementation could never work. Also fix `for/vector`.
  • Loading branch information
samth committed Jan 14, 2020
1 parent 7facce4 commit 1b15186
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 0 additions & 1 deletion typed-racket-lib/typed-racket/base-env/for-clauses.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#:with (expand* ...) (list (quasisyntax/loc #'c
((v.ann-name ...) seq-expr))
#'#:when #''#t))
;; Note: #:break and #:final clauses don't ever typecheck
(pattern (~seq (~and kw (~or #:when #:unless #:break #:final)) guard:expr)
#:with (expand ...) (list #'kw #'guard)
#:with (expand* ...) #'(expand ...)))
Expand Down
17 changes: 11 additions & 6 deletions typed-racket-lib/typed-racket/base-env/prims.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -320,18 +320,21 @@ the typed racket language.
;; Note: these don't ever typecheck
(pattern (~seq (~and kw (~or #:break #:final)) guard-expr:expr)
#:with (expand ...) #'(kw guard-expr)))
(define-splicing-syntax-class break-clause
(pattern (~seq (~and kw (~or #:break #:final)) guard-expr:expr)
#:with (expand ...) #'(kw guard-expr)))
(define-syntax-class for-kw
(pattern #:when
#:with replace-with #'when)
(pattern #:unless
#:with replace-with #'unless))
(syntax-parse clauses
[(head:for-clause next:for-clause ... kw:for-kw rest ...)
[(head:for-clause next:for-clause ... kw:for-kw guard b:break-clause ... rest ...)
(add-ann
(quasisyntax/loc stx
(for
(head.expand ... next.expand ... ...)
#,(loop #'(kw rest ...))))
(head.expand ... next.expand ... ... kw guard b.expand ... ...)
#,(loop #'(rest ...))))
#'Void)]
[(head:for-clause ...) ; we reached the end
(add-ann
Expand Down Expand Up @@ -806,8 +809,9 @@ the typed racket language.
(define n n-expr)
(define: vs : T (make-vector n fill-expr))
(define i 0)
(for (clauses ... #:break (i . unsafe-fx= . n))
(unsafe-vector-set! vs i body-expr))
(for (clauses ... #:break (i . >= . n))
(unsafe-vector-set! vs i body-expr)
(set! i (+ i 1)))
vs)
T))]
[(name for ann T K #:length n-expr (clauses ...) body-expr)
Expand All @@ -819,7 +823,8 @@ the typed racket language.
(define i 0)
(unless (and (fixnum? n) (exact-nonnegative-integer? n))
(raise-argument-error 'name '"exact-nonnegative-integer?" n))
(for (clauses ... #:break (unsafe-fx= i n))

(for (clauses ... #:break (>= i n))
(define v body-expr)
;; can't use `unsafe-fx=` here
;; if `n` is larger than a fixnum, this is unsafe, and we
Expand Down

0 comments on commit 1b15186

Please sign in to comment.