Skip to content
This repository was archived by the owner on Sep 13, 2019. It is now read-only.

Commit a1b0366

Browse files
committed
more permissive tracking of unbound references at phase > 0
The old expander was too permissive, losing track of unbound variables that are part of an expansion. The new expander was too strict, maintaining a complaint even if the bad reference doesn't appear in the expansion. Look for a happy medium.
1 parent 4172507 commit a1b0366

File tree

5 files changed

+3361
-3269
lines changed

5 files changed

+3361
-3269
lines changed

pkgs/racket-test-core/tests/racket/module.rktl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,41 @@ case of module-leve bindings; it doesn't cover local bindings.
21622162

21632163
(define another 'x))
21642164

2165+
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2166+
;; Allow a reference to a never-defined variable in a `local-expand`
2167+
;; or `syntax-local-bind-syntaxes` on the grounds that the result is
2168+
;; not necessarily in the module's expansion. But keep track of
2169+
;; missing variables encountered during
2170+
;; `syntax-local-expand-expression`, since the opqaue result can be
2171+
;; included without further inspection.
2172+
2173+
(module im-ok-and-your-ok-local-expand racket/base
2174+
(require (for-syntax racket/base)
2175+
(for-meta 2 racket/base))
2176+
(begin-for-syntax
2177+
(define-syntax (m stx)
2178+
(local-expand #'(lambda () nonesuch) 'expression '())
2179+
#''ok)
2180+
(m)))
2181+
2182+
(module im-ok-and-your-ok-syntax-local-bind-syntaxes racket/base
2183+
(require (for-syntax racket/base))
2184+
(define-syntax (m stx)
2185+
(syntax-local-bind-syntaxes (list #'x)
2186+
#'(lambda () nonesuch)
2187+
(syntax-local-make-definition-context))
2188+
#''ok)
2189+
(m))
2190+
2191+
(syntax-test #'(module im-ok-and-your-ok-local-expand racket/base
2192+
(require (for-syntax racket/base)
2193+
(for-meta 2 racket/base))
2194+
(begin-for-syntax
2195+
(define-syntax (m stx)
2196+
(syntax-local-expand-expression #'(lambda () nonesuch))
2197+
#''ok)
2198+
(m))))
2199+
21652200
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21662201

21672202
(report-errs)

racket/src/expander/expand/definition-context.rkt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@
195195
#:phase [phase (expand-context-phase ctx)]
196196
#:intdefs intdefs
197197
#:stop-ids [stop-ids #f]
198-
#:to-parsed-ok? [to-parsed-ok? #f])
198+
#:to-parsed-ok? [to-parsed-ok? #f]
199+
#:track-to-be-defined? [track-to-be-defined? #f])
199200
(define same-kind? (or (eq? context
200201
(expand-context-context ctx))
201202
(and (list? context)
@@ -248,7 +249,18 @@
248249
[just-once? #f]
249250
[in-local-expand? #t]
250251
[stops (free-id-set phase (or all-stop-ids null))]
251-
[current-introduction-scopes null]))
252+
[current-introduction-scopes null]
253+
[need-eventually-defined (let ([ht (expand-context-need-eventually-defined ctx)])
254+
(cond
255+
[track-to-be-defined?
256+
;; maintain status quo and propagate tracking
257+
ht]
258+
[ht
259+
;; keep allowing unbound references, but don't track them
260+
(make-hasheqv)]
261+
[else
262+
;; keep disallowing unbound references
263+
#f]))]))
252264

253265
;; ----------------------------------------
254266

racket/src/expander/expand/local-expand.rkt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
(define (syntax-local-expand-expression s [opaque-only? #f])
4343
(define exp-s (do-local-expand 'syntax-local-expand-expression s 'expression null #f
4444
#:to-parsed-ok? opaque-only?
45-
#:skip-log-exit? #t))
45+
#:skip-log-exit? #t
46+
#:track-to-be-defined? #t))
4647
(define ctx (get-current-expand-context))
4748
;; Move introduction scope from the already-expanded syntax object to
4849
;; its wrapper. The expander will later check that the wrapper ends up
@@ -68,6 +69,7 @@
6869
#:lift-key [lift-key (and (or capture-lifts?
6970
as-transformer?)
7071
(generate-lift-key))]
72+
#:track-to-be-defined? [track-to-be-defined? #f]
7173
#:skip-log-exit? [skip-log-exit? #f])
7274
(performance-region
7375
['expand 'local-expand]
@@ -102,7 +104,8 @@
102104
#:phase phase
103105
#:intdefs intdefs
104106
#:stop-ids stop-ids
105-
#:to-parsed-ok? to-parsed-ok?))
107+
#:to-parsed-ok? to-parsed-ok?
108+
#:track-to-be-defined? track-to-be-defined?))
106109

107110
(log-expand local-ctx 'enter-local s)
108111
(define input-s (add-intdef-scopes (flip-introduction-scopes s ctx) intdefs))

racket/src/expander/expand/module.rkt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"def-id.rkt"
3030
"prepare.rkt"
3131
"log.rkt"
32+
"syntax-id-error.rkt"
3233
"../compile/main.rkt"
3334
"../eval/top.rkt"
3435
"../eval/module.rkt"
@@ -366,7 +367,7 @@
366367
#:mpis-to-reset mpis-to-reset)))
367368

368369
;; Check that any tentatively allowed reference at phase >= 1 is ok
369-
(check-defined-by-now need-eventually-defined self)
370+
(check-defined-by-now need-eventually-defined self ctx)
370371

371372
;; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
372373
;; Pass 3: resolve provides at all phases
@@ -1009,7 +1010,7 @@
10091010
(cons exp-body
10101011
(loop tail? rest-bodys)))])))
10111012

1012-
(define (check-defined-by-now need-eventually-defined self)
1013+
(define (check-defined-by-now need-eventually-defined self ctx)
10131014
;; If `need-eventually-defined` is not empty, report an error
10141015
(for ([(phase l) (in-hash need-eventually-defined)])
10151016
(for ([id (in-list l)])
@@ -1019,7 +1020,9 @@
10191020
(module-binding? b)
10201021
(eq? (module-binding-sym b) (syntax-e id))
10211022
(eq? (module-binding-module b) self))
1022-
(raise-syntax-error #f "reference to an unbound identifier" id)))))
1023+
(raise-syntax-error #f "reference to an unbound identifier"
1024+
id #f null
1025+
(syntax-debug-info-string id ctx))))))
10231026

10241027
;; ----------------------------------------
10251028

0 commit comments

Comments
 (0)