Skip to content

Commit 8f3e3b4

Browse files
authored
Attach analyzed modules to namespace (#471)
This allows rules to extract compile-time info from imported bindings in analyzed code.
1 parent cd4eb66 commit 8f3e3b4

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

main.rkt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@
162162
(with-handlers ([exn:fail:syntax? skip]
163163
[exn:fail:filesystem:missing-module? skip]
164164
[exn:fail:contract:variable? skip])
165-
(define analysis
166-
(parameterize ([current-namespace (make-base-namespace)])
167-
(source-analyze source #:lines lines)))
165+
(define analysis (source-analyze source #:lines lines))
168166
(refactor-visited-forms #:analysis analysis #:suite suite #:comments comments #:lines lines)))
169167

170168
(refactoring-result-set #:base-source source #:results results))
@@ -279,7 +277,8 @@
279277
absent)])
280278
(guarded-block
281279
(guard-match (present replacement)
282-
(refactoring-rule-refactor rule syntax (source-code-analysis-code analysis))
280+
(parameterize ([current-namespace (source-code-analysis-namespace analysis)])
281+
(refactoring-rule-refactor rule syntax (source-code-analysis-code analysis)))
283282
#:else absent)
284283
(guard (syntax-replacement-introduces-incorrect-bindings? replacement) #:else
285284
(log-resyntax-warning

private/source.rkt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
[source-code-analysis-code (-> source-code-analysis? source?)]
2929
[source-code-analysis-visited-forms (-> source-code-analysis? (listof syntax?))]
3030
[source-code-analysis-expansion-time-output (-> source-code-analysis? immutable-string?)]
31+
[source-code-analysis-namespace (-> source-code-analysis? namespace?)]
3132
[syntax-source-location (-> syntax? source-location?)]
3233
[with-input-from-source (-> source? (-> any) any)]))
3334

@@ -79,7 +80,7 @@
7980
#:guard (λ (original contents _) (values original (string->immutable-string contents))))
8081

8182

82-
(define-record-type source-code-analysis (code visited-forms expansion-time-output))
83+
(define-record-type source-code-analysis (code visited-forms expansion-time-output namespace))
8384
(define-record-type source-location (source line column position span))
8485

8586

@@ -127,7 +128,9 @@
127128

128129

129130
(define (source-analyze code #:lines [lines (range-set (unbounded-range #:comparator natural<=>))])
130-
(parameterize ([current-directory (or (source-directory code) (current-directory))])
131+
(define ns (make-base-namespace))
132+
(parameterize ([current-directory (or (source-directory code) (current-directory))]
133+
[current-namespace ns])
131134
(define code-linemap (string-linemap (source->string code)))
132135
(define program-stx (source-read-syntax code))
133136
(define program-source-name (syntax-source program-stx))
@@ -184,6 +187,12 @@
184187
[current-output-port output-port])
185188
(expand program-stx)))
186189

190+
;; We evaluate the module in order to ensure it's declared in the namespace, then we attach it at
191+
;; expansion time to ensure the module is visited (but not instantiated). This allows refactoring
192+
;; rules to access expansion-time values reflectively via the analysis namespace.
193+
(eval expanded)
194+
(namespace-require/expansion-time (extract-module-require-spec expanded))
195+
187196
(define output (get-output-string output-port))
188197
(define binding-table (fully-expanded-syntax-binding-table expanded))
189198
(define original-binding-table-by-position
@@ -226,7 +235,10 @@
226235
(sorting syntax-source-location<=> #:key syntax-source-location)
227236
#:into into-list))
228237

229-
(source-code-analysis #:code code #:visited-forms visited #:expansion-time-output output)))
238+
(source-code-analysis #:code code
239+
#:visited-forms visited
240+
#:expansion-time-output output
241+
#:namespace ns)))
230242

231243

232244
(define (syntax-source-location stx)
@@ -241,3 +253,8 @@
241253
(define syntax-source-location<=>
242254
(comparator-chain (comparator-map real<=> source-location-position)
243255
(comparator-map (comparator-reverse real<=>) source-location-span)))
256+
257+
258+
(define (extract-module-require-spec mod-stx)
259+
(syntax-parse mod-stx
260+
[(_ name _ . _) `',(syntax-e #'name)]))

0 commit comments

Comments
 (0)