FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add type inference for $_ in -replace scriptblock by MartinGC94 · Pull Request #28329 · PowerShell/PowerShell · GitHub

Repository navigation

Add type inference for $_ in -replace scriptblock - #28329

Open
MartinGC94 wants to merge 2 commits into
PowerShell:masterfrom
MartinGC94:inferReplaceVar
Open

MartinGC94 wants to merge 2 commits into
PowerShell:masterfrom
MartinGC94:inferReplaceVar

Conversation

MartinGC94 commented Nov 4, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor

PR Summary

This PR adds type inference for $_ when used inside a scriptblock for the -replace operator.
For example when it's used like this: "123" -replace '2', {[int]$_.Value + 2}

PR Context

Yesterday I found out that you could use a scriptblock for -replace but there was no type inference for $_ in this particular case, so I decided to add it.

PR Checklist

Copilot AI balanced review requested due to automatic review settings November 4, 2026 14:10

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

🟡 Changes recommended

Replacement callback detection incorrectly assigns Match to eagerly invoked scriptblocks and misses array-literal callbacks, producing incorrect or missing type inference for $_.

2 open issues What changed in this PR

Adds type inference for $_ inside -replace scriptblocks, enabling PowerShell’s inference engine to recognize it as System.Text.RegularExpressions.Match.

Changes:

  • Detects replacement scriptblock contexts for case-sensitive and case-insensitive replacement operators.
  • Adds a regression test for the inferred Match type.
File Description
test/​powershell/​engine/​Api/​TypeInference.Tests.ps1 Tests $_ type inference inside a replacement callback.
src/​System.Management.Automation/​engine/​parser/​TypeInferenceVisitor.cs Adds replacement-context detection and Match inference.

Review effort: Balanced

microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Nov 11, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL