Skip to content

Commit e2ff6fb

Browse files
Copilotjackfirth
andcommitted
Add syntax-contains-path? and filter invalid properties
Co-authored-by: jackfirth <[email protected]>
1 parent 614ae99 commit e2ff6fb

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

private/analysis.rkt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,32 @@
139139
(mapping-values (λ (exp-paths) (present-value (sorted-set-least-element exp-paths))))
140140
#:into (into-sorted-map syntax-path<=>)))
141141

142-
(define expansion-analyzer-props
142+
(define expansion-analyzer-props-raw
143143
(transduce analyzers
144144
(append-mapping
145145
(λ (analyzer)
146146
(syntax-property-bundle-entries
147147
(expansion-analyze analyzer expanded))))
148148
#:into into-syntax-property-bundle))
149149

150+
;; Filter out properties with invalid paths and log warnings
151+
(define expansion-analyzer-props
152+
(transduce (syntax-property-bundle-entries expansion-analyzer-props-raw)
153+
(filtering
154+
(λ (prop-entry)
155+
(match-define (syntax-property-entry path key _value) prop-entry)
156+
(define valid? (syntax-contains-path? expanded path))
157+
(unless valid?
158+
(log-resyntax-warning
159+
(string-append
160+
"ignoring property with out-of-syntax path returned by analyzer\n"
161+
" path: ~a\n"
162+
" property key: ~a")
163+
path
164+
key))
165+
valid?))
166+
#:into into-syntax-property-bundle))
167+
150168
(define expansion-analyzer-props-adjusted-for-visits
151169
(transduce property-selection-table
152170
(mapping-values

private/syntax-path.rkt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
[syntax-path->string (-> syntax-path? immutable-string?)]
2424
[string->syntax-path (-> string? syntax-path?)]
2525
[syntax-ref (-> syntax? syntax-path? syntax?)]
26+
[syntax-contains-path? (-> syntax? syntax-path? boolean?)]
2627
[syntax-set (-> syntax? syntax-path? syntax? syntax?)]
2728
[syntax-remove-splice
2829
(-> syntax? (and/c proper-syntax-path? nonempty-syntax-path?) exact-nonnegative-integer? syntax?)]
@@ -564,6 +565,66 @@
564565
(check-regexp-match #rx"path is inconsistent" (exn-message thrown)))))
565566

566567

568+
(define (syntax-contains-path? init-stx path)
569+
(with-handlers ([exn:fail:contract? (λ (_) #false)])
570+
(syntax-ref init-stx path)
571+
#true))
572+
573+
574+
(module+ test
575+
(test-case "syntax-contains-path?"
576+
577+
(test-case "empty path on any syntax"
578+
(define stx #'a)
579+
(check-true (syntax-contains-path? stx empty-syntax-path)))
580+
581+
(test-case "valid path in list"
582+
(define stx #'(a b c))
583+
(check-true (syntax-contains-path? stx (syntax-path (list 0))))
584+
(check-true (syntax-contains-path? stx (syntax-path (list 1))))
585+
(check-true (syntax-contains-path? stx (syntax-path (list 2)))))
586+
587+
(test-case "invalid path in list - out of bounds"
588+
(define stx #'(a b c))
589+
(check-false (syntax-contains-path? stx (syntax-path (list 3))))
590+
(check-false (syntax-contains-path? stx (syntax-path (list 10)))))
591+
592+
(test-case "valid nested path"
593+
(define stx #'(a (b c) d))
594+
(check-true (syntax-contains-path? stx (syntax-path (list 1 0))))
595+
(check-true (syntax-contains-path? stx (syntax-path (list 1 1)))))
596+
597+
(test-case "invalid nested path"
598+
(define stx #'(a (b c) d))
599+
(check-false (syntax-contains-path? stx (syntax-path (list 1 2))))
600+
(check-false (syntax-contains-path? stx (syntax-path (list 3 0)))))
601+
602+
(test-case "valid path in vector"
603+
(define stx #'#[a b c])
604+
(check-true (syntax-contains-path? stx (syntax-path (list 0))))
605+
(check-true (syntax-contains-path? stx (syntax-path (list 1)))))
606+
607+
(test-case "invalid path in vector"
608+
(define stx #'#[a b c])
609+
(check-false (syntax-contains-path? stx (syntax-path (list 5)))))
610+
611+
(test-case "valid path in box"
612+
(define stx #'#&a)
613+
(check-true (syntax-contains-path? stx (syntax-path (list 0)))))
614+
615+
(test-case "invalid path in box"
616+
(define stx #'#&a)
617+
(check-false (syntax-contains-path? stx (syntax-path (list 1)))))
618+
619+
(test-case "path on non-compound datum"
620+
(define stx #'atom)
621+
(check-false (syntax-contains-path? stx (syntax-path (list 0)))))
622+
623+
(test-case "path referring to non-syntax component"
624+
; This test ensures we catch the error when syntax-ref finds a non-syntax component
625+
(define stx #'(a b c))
626+
; If we try to drill down into 'a', we should fail because atoms don't have children
627+
(check-false (syntax-contains-path? stx (syntax-path (list 0 0)))))))
567628

568629

569630

0 commit comments

Comments
 (0)