Skip to content

Commit

Permalink
Fix a bug caused by define-signature
Browse files Browse the repository at this point in the history
The `(open ...)` clause of `define-signature` _somehow_ introduces syntax objects in its expansion that are `syntax-original?`, but can be from a different file. This causes the linemap checking that Resyntax performs to break, since the line numbers from those syntax objects won't match up with the file being analyzed. This commit ensures that Resyntax only looks at original syntax that's from the same file as the one being analyzed.
  • Loading branch information
jackfirth committed Sep 1, 2024
1 parent 5f1945c commit 2944b0a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions private/source.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,22 @@
(define (source-analyze code #:lines [lines (range-set (unbounded-range #:comparator natural<=>))])
(parameterize ([current-directory (or (source-directory code) (current-directory))])
(define code-linemap (string-linemap (source->string code)))
(define stx (source-read-syntax code))
(define program-stx (source-read-syntax code))
(define program-source-name (syntax-source program-stx))
(define current-expand-observe (dynamic-require ''#%expobs 'current-expand-observe))
(define visits-by-location (make-hash))
(define others-by-location (make-hash))

(define (add-original-location! hsh stx)
(when (and (syntax? stx)
(syntax-original? stx)
;; Some macros are able to bend hygiene and syntax properties in such a way that they
;; introduce syntax objects into the program that are syntax-original?, but from a
;; different file than the one being expanded. So in addition to checking for
;; originality, we also check that they come from the same source as the main program
;; syntax object. The (open ...) clause of the define-signature macro bends hygiene
;; in this way, and is what originally motivated the addition of this check.
(equal? (syntax-source stx) program-source-name)
(range-set-encloses? lines (syntax-line-range stx #:linemap code-linemap)))
(define loc (syntax-source-location stx))
(unless (hash-has-key? hsh loc)
Expand All @@ -127,7 +135,7 @@
[(_ _) (void)])

(parameterize ([current-expand-observe observe-event!])
(expand stx))
(expand program-stx))
(define scopes-by-location
(hash-union (hash) visits-by-location others-by-location
#:combine (λ (a b) a)))
Expand Down

0 comments on commit 2944b0a

Please sign in to comment.