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

Easier way for rules to check identifier usages #400

Open
jackfirth opened this issue Nov 18, 2024 · 0 comments
Open

Easier way for rules to check identifier usages #400

jackfirth opened this issue Nov 18, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@jackfirth
Copy link
Owner

Consider the hash-ref-set!-to-hash-ref! rule:

(define-refactoring-rule hash-ref-set!-to-hash-ref!
  #:description "This expression can be replaced with a simpler, equivalent `hash-ref!` expression."
  #:literals (hash-ref hash-set! define)
  (hash-ref
   h1:id
   k1:pure-expression
   (_:lambda-by-any-name
    ()
    (define v1:id initializer:value-initializer)
    (hash-set! h2:id k2:pure-expression v2:id)
    v3:id))
  #:when (free-identifier=? #'h1 #'h2)
  #:when (syntax-free-identifier=? #'k1 #'k2)
  #:when (free-identifier=? #'v1 #'v2)
  #:when (free-identifier=? #'v1 #'v3)
  (hash-ref! h1 k1 initializer.failure-result-form))

It needs to do quite a bit of work to check that some identifiers are usages of others. Those free-identifier=? checks ought to be easier to write. It might be useful to have a (~usage id) syntax pattern that matches any identifier that's free-identifier=? to #'id. That would let me write the above rule like this:

(define-refactoring-rule hash-ref-set!-to-hash-ref!
  #:description "This expression can be replaced with a simpler, equivalent `hash-ref!` expression."
  #:literals (hash-ref hash-set! define)
  (hash-ref
   h:id
   k1:pure-expression
   (_:lambda-by-any-name
    ()
    (define v:id initializer:value-initializer)
    (hash-set! (~usage h) k2:pure-expression (~usage v))
    (~usage v)))
  #:when (syntax-free-identifier=? #'k1 #'k2)
  (hash-ref! h k1 initializer.failure-result-form))

Might be worthwhile to make it match any syntax object that's syntax-free-identifier=? too. This could also help with #379, as ~usage could default to (resyntax-local-phase-level) (or whatever equivalent I come up with) instead of (syntax-local-phase-level) like free-identifier=? does.

@jackfirth jackfirth added the enhancement New feature or request label Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant