racket / racket/racket

assignment to identifiers bound by for-like forms can break invariants

Open
#3,378 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Racket
Stars
5.2k
Forks
698
Avg merge
18h 34m
Merged PRs (30d)
5

Description

Identifiers bound by for- and for/fold-like forms—for both accumulators and sequence elements—can be set!. Assigning to these variables can break invariants or expose implementation details of a for-like form or for-clause transformer (as in define-sequence-syntax).

Some examples:

  1. #lang racket
    
    (for/lists (acc)
               ([v (in-range 2)])
       (unless (zero? v)
         (set! acc 'bad))
       v)
    
    ->
    racket/private/for.rkt:1963:23: reverse: contract violation
      expected: list?
      given: '(1 . bad)
    
  2. #lang racket
    
    (require (for-syntax syntax/parse))
    
    (define-sequence-syntax in-my-naturals
      (λ () #'in-naturals)
      (syntax-parser
        [[(n) (_)]
         #'[(n) (:do-in ([(n) 0])
                        #t
                        ([n n])
                        #t
                        ([(n) n])
                        #t
                        #t
                        ((add1 n)))]]))
    
    (for/list ([i (in-my-naturals)]
               #:when (< i 3))
      (set! i 'bad))
    
    ->
    add1: contract violation
      expected: number?
      given: 'bad
    

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing both examples and inspect racket/private/for.rkt around the reported line, along with the for-like and for-clause transformer behavior shown in the issue. Determine the intended handling of set! on accumulator and sequence-element bindings, then add coverage demonstrating that these assignments cannot break the form's invariants or expose implementation details.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.