Skip to content

Conversation

@MathiasVP
Copy link
Collaborator

TLDR: This PR fixes a bug related to local variable resolution.

Slightly longer:
One of the very earliest things that happen in QL for most languages is local variable resolution. That is, resolving which variable a read or write affects. This is pretty straightforward, but care must be taken to limit the scope of variable as much as possible for performance reasons.

Compiled languages often have the luxury of a variable declaration which clearly defines where a variable is introduced. However, that's not the case for mots dynamic languages (such as PowerShell). Instead, the scope of a variable is identified by finding the scope in which the variable is written to, and any nested scope is then a scope in which the variable is available. So when we see something like:

$x = 42
if(...) {
  use($x)
} else { ... }

we know that $x is in scope in the outer scope (which assigns $x) and the inner scopes (i.e., the branches of the if).

Thus, it's clear that we need to identify which variable occurs on the left-hand side of an assignment. However, it turned out that we had forgotten that the left-hand side could be a conversion such as:

[int]$x = 42
use($x)

and in that case we failed to deduce the assignment to $x, and thus the scope of $x, and thus any flow involving $x was totally lost.

Commit-by-commit review recommended:

  • Commit 1: Does a totally unrelated prettification
  • Commit 2: Adds a test that demonstrates the bug
  • Commit 3: Performs a missing autoformat
  • Commit 4: Actually fixes the problem
  • Commit 5: Accepts the test changes so that we can see the bug has been fixed

@MathiasVP MathiasVP merged commit 83b8f89 into main Oct 20, 2025
4 checks passed
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.

3 participants