Skip to content

Commit 185c8be

Browse files
Copilotjackfirth
andcommitted
Add disappeared-use support to identifier-usage-analyzer
Co-authored-by: jackfirth <[email protected]>
1 parent effc44e commit 185c8be

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

default-recommendations/analyzers/identifier-usage-test.rkt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,19 @@ analysis-test: "disappeared use of macro"
390390
@inspect - m
391391
@property usage-count
392392
@assert 1
393+
394+
395+
analysis-test: "custom macro with disappeared use"
396+
--------------------
397+
(require (for-syntax racket/base))
398+
(define x 42)
399+
(define-syntax (use-x stx)
400+
(syntax-case stx ()
401+
[(_ body)
402+
(syntax-property #'body 'disappeared-use (list #'x))]))
403+
(use-x (void))
404+
--------------------
405+
@within - (define x 42)
406+
@inspect - x
407+
@property usage-count
408+
@assert 1

default-recommendations/analyzers/identifier-usage.rkt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@
5858
(extract-ids origin))
5959

6060

61+
;; Extract identifiers from the 'disappeared-use syntax property
62+
;; The 'disappeared-use property can be either:
63+
;; - A single identifier
64+
;; - A list of identifiers
65+
;; We extract all identifiers and label them with the given phase
66+
(define (disappeared-use-property-identifiers stx phase)
67+
(define disappeared (syntax-property stx 'disappeared-use))
68+
69+
(define (extract-ids obj)
70+
(cond
71+
[(not obj) (stream)]
72+
[(identifier? obj)
73+
;; Add the phase property to the identifier so it matches correctly
74+
(stream (syntax-property obj 'phase phase))]
75+
[(list? obj)
76+
(apply stream-append (map extract-ids obj))]
77+
[else (stream)]))
78+
79+
(extract-ids disappeared))
80+
81+
6182
;; Find all identifier usage sites (not binding sites)
6283
(define (usage-site-identifiers expanded-stx)
6384
(let loop ([expanded-stx expanded-stx] [phase 0])
@@ -70,6 +91,12 @@
7091
(for/list ([stx-node (in-stream (syntax-search-everything expanded-stx))])
7192
(origin-property-identifiers stx-node phase))))
7293

94+
;; Collect identifiers from disappeared-use properties of all syntax objects
95+
(define disappeared-ids
96+
(apply stream-append
97+
(for/list ([stx-node (in-stream (syntax-search-everything expanded-stx))])
98+
(disappeared-use-property-identifiers stx-node phase))))
99+
73100
;; Collect identifiers from the expanded syntax tree
74101
(define expanded-ids
75102
(syntax-search expanded-stx
@@ -126,7 +153,7 @@
126153
#:when (identifier? this-syntax)
127154
(stream (attribute id))]))
128155

129-
(stream-append origin-ids expanded-ids)))
156+
(stream-append origin-ids disappeared-ids expanded-ids)))
130157

131158

132159
(define (fully-expanded-syntax-binding-table stx)

0 commit comments

Comments
 (0)