Skip to content

PowerShell: branch counts [switch] types, -not and ForEach-Object (precision 0.86); closures counts every { } block (precision 0.18) #8903

Closed

Description

@squid-protocol

What is wrong

Two PowerShell families count things that are not what the contract names. Found by the keyword-family validity tool (#8864) with a PowerShell oracle, on the PowerShell repository's test/powershell (676 functions matched):

  1. branch fires where there is no decision (recall 1.00, precision 0.86; all 31 false-positive functions contain one of these):
    • the word switch inside a parameter type, [switch]$s (read as a switch statement);
    • -not, a unary negation, which is not a choice between paths (-and/-or/-xor are binary short-circuit-like operators and are a separate question, see the boolean-operator issue);
    • ForEach-Object, matched on its ForEach (an iteration cmdlet in a pipeline, which the contract files under comprehensions: "a map/filter/fold/for-each call", not under branch).
  2. closures counts every { } block, so every function body is a "closure" (precision 0.18). The rule (powershell.py, "closures": re.compile(r"\{\s{0,20}(?:param\s{0,10}\([^)]{0,300}\))?[^}]{0,500}\}", re.I)) matches any braced block, not a scriptblock literal used as a value (the contract: "the syntax that opens an anonymous callable ... at the point it is defined").

Reproduce (scanned with the pinned scanner; counts read from function_data)

function ps_switch_type { param([switch]$s) 1 }            # struct_branch == 1, should be 0
function ps_not { param($x) return -not $x }               # struct_branch == 1, should be 0
function ps_foreach_obj { 1 | ForEach-Object { $_ } }      # struct_branch == 1 (a loop cmdlet: comprehensions, not branch)
function ps_write_output { Write-Output 1 }                # struct_closures == 1, should be 0 (no scriptblock literal anywhere)

Expected

branch counts switch as a statement, not as a type in [switch]; does not count -not; and either counts ForEach-Object and Where-Object consistently (as loops) or leaves them to comprehensions. closures counts a scriptblock { ... } only where it is a value (an argument, an assignment or a pipeline stage), not the body of function name { }.

Note: the branch contract (docs/branch_rule_contract.md) records that break and continue still sit in PowerShell's branch rule (a deferred relocation, #2832); this issue is about the cases above only.

Part of #8863. Time the change (CPU seconds on keyword-rosetta and language-crucible); rosetta:rebless-owed if counts move; add each case above as a test.

Activity

  1. added
    bug
    core-engine
    on Nov 21, 2026
  2. squid-protocol commented on Nov 22, 2026

    @squid-protocol
    OwnerAuthor

    Claimed by promoter-bot host melek, lease renewed 2026-11-23T12:11Z. It lapses after 6 h without a renewal. To take it back, assign someone or remove the claim:melek label.

  3. squid-protocol commented on Nov 23, 2026

    @squid-protocol
    OwnerAuthor

    Fixed by #9111 (merged as fcc440b), which carried this issue's PR (rebuilt with eight others to re-bless the golden snapshots once).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugclaim:melekcore-enginepriority: medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions