Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve let-to-define self-shadowing detection #283

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions default-recommendations/let-binding-suggestions-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ test: "self-shadowing let*-values binding clause isn't refactorable"
------------------------------


test: "let binding that only appears self shadowing before expansion (issue #230)"
------------------------------
(define (f b)
(let ([x (let ([x 1]) (+ x 1))])
(+ x 1)))
------------------------------
------------------------------
(define (f b)
(define x (let ([x 1]) (+ x 1)))
(+ x 1))
------------------------------


test: "let* with later right-hand-sides referring to earlier bindings is refactorable"
------------------------------
(define (f a)
Expand Down
15 changes: 11 additions & 4 deletions default-recommendations/private/let-binding.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@

(pattern [all-ids:id-list rhs:expr]
#:do [(log-resyntax-debug
"refactorable-let-expression: checking binding-clause not self shadowing: ~a"
"refactorable-let-expression: checking binding-clause not self-shadowing: ~a"
this-syntax)]
#:when (for*/and ([rhs-free-id (in-free-id-set (syntax-free-identifiers (attribute rhs)))]
[id (in-list (attribute all-ids.id))])
(log-resyntax-debug "refactorable-let-expression: checking identifier not shadowed: ~a"
rhs-free-id)
(identifier-binding-unchanged-in-context? rhs-free-id id))
(log-resyntax-debug
"refactorable-let-expression: checking identifier ~a not shadowed by ~a"
rhs-free-id id)
(identifier-would-self-shadow? id rhs-free-id #:full-right-hand-side (attribute rhs)))
#:cut
#:with (id ...) (attribute all-ids.id)
#:with definition
Expand Down Expand Up @@ -107,6 +108,12 @@
#:original #,this-syntax)))



(define (identifier-would-self-shadow? id rhs-id #:full-right-hand-side rhs)
(or (identifier-binding-unchanged-in-context? rhs-id id)
(not (free-identifier=? rhs-id (identifier-in-context rhs-id rhs)))))


(define (identifier-binding-unchanged-in-context? id context)
(define add-context (make-syntax-delta-introducer context #false))
(free-identifier=? id (add-context id)))
Expand Down
Loading