Skip to content

Fix pin operator incorrectly applied to variables in pattern match body#59

Closed
ofedoren wants to merge 1 commit into
seattlerb:masterfrom
ofedoren:safemode-fixes
Closed

Fix pin operator incorrectly applied to variables in pattern match body#59
ofedoren wants to merge 1 commit into
seattlerb:masterfrom
ofedoren:safemode-fixes

Conversation

@ofedoren
Copy link
Copy Markdown

@ofedoren ofedoren commented May 12, 2026

Bug: process_in processes both the pattern and the body while the SexpProcessor context stack contains :in. The __var method checks context[1] for :in or /_pat$/ and prepends ^ (pin operator) to all lvar references. This causes local variables in the body (after "then") to be incorrectly rendered with ^, producing invalid Ruby.

For example, case x; in [a, b]; a; end was rendered as:

  case x
  in [a, b] then
    ^a        # <-- wrong: ^ should only appear in patterns
  end

Fix: wrap body processing in in_context :in_body so that __var sees :in_body instead of :in on the context stack and skips the pin prefix. Pattern variables inside the pattern itself continue to be pinned correctly since the pattern is still processed in :in context.

Found during theforeman/safemode#60. Seemed good enough to make a patch suggestion here, so please tell what do you think :)

Bug: process_in processes both the pattern and the body while the
SexpProcessor context stack contains :in. The __var method checks
context[1] for :in or /_pat$/ and prepends ^ (pin operator) to all
lvar references. This causes local variables in the body (after "then")
to be incorrectly rendered with ^, producing invalid Ruby.

For example, `case x; in [a, b]; a; end` was rendered as:
  case x
  in [a, b] then
    ^a        # <-- wrong: ^ should only appear in patterns
  end

Fix: wrap body processing in `in_context :in_body` so that __var sees
:in_body instead of :in on the context stack and skips the pin prefix.
Pattern variables inside the pattern itself continue to be pinned
correctly since the pattern is still processed in :in context.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@zenspider
Copy link
Copy Markdown
Member

Good find! Done... this will be released soonish.

@zenspider zenspider closed this May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants