Skip to content

Improve type inference for $_ - #17716

Merged
Dongbo Wang (daxian-dbw) merged 8 commits into
PowerShell:masterfrom
MartinGC94:FixTypeInferenceInLongPipeline
Jul 25, 2022
Merged

Dongbo Wang (daxian-dbw) merged 8 commits into
PowerShell:masterfrom
MartinGC94:FixTypeInferenceInLongPipeline

Conversation

@MartinGC94

@MartinGC94 MartinGC94 commented Jul 19, 2022 •

Copy link
Copy Markdown
Contributor

PR Summary

Fixes the type inference for $_ in pipelines with more than 1 command by making it infer the type of the previous command of the expression rather than the first.

-Edit: Decided to make a full fix for $_ and also fix it when the scriptblock is inside an array of arguments like for Select-Object or Foreach-Object ls | ForEach-Object -Process {$_.<Tab>},{$_.<Tab>}
Also fixes a scenario where a catch block is given invalid types so there's no completion for $_ inside the catch block.
Fixes: #17613
Edit2: The test failures helped me discover a different issue, strings are enumerated as chars by the type inference. This has now been fixed.

Full list of scenarios where $_ is now properly being inferred:

  • Member invocation scriptblocks like $Collection.Where({$_})
  • Scriptblocks used as command arguments like: ls | where {$_}
  • Switch statements like: switch ("hello") {'Hello'{$_}}
  • Catch statements like: try{} catch {$_}
  • Trap statements like: trap {$_}

PR Context

PR Checklist

@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jul 20, 2022
@MartinGC94 MartinGC94 changed the title Fix type inference for $_ in pipelines with more than 1 command Improve type inference for $_ Jul 20, 2022
Co-authored-by: Ilya <darpa@yandex.ru>

@daxian-dbw Dongbo Wang (daxian-dbw) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, but it would be great if more comments can be added.

@MartinGC94

Copy link
Copy Markdown
Contributor Author

Sorry for pushing new updates after the review, I forgot that there were other instances where $_ is set. Hopefully I have them all now.

@iSazonov
Ilya (iSazonov) self-requested a review July 22, 2022 16:19

@daxian-dbw Dongbo Wang (daxian-dbw) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great! Thank you, MartinGC94!

Co-authored-by: Ilya <darpa@yandex.ru>
@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 3d4e294 into PowerShell:master Jul 25, 2022
@ghost

Copy link
Copy Markdown

🎉v7.3.0-preview.7 has been released which incorporates this pull request.:tada:

Handy links:

@MartinGC94 MartinGC94 mentioned this pull request Mar 23, 2023
8 of 22 tasks
@MartinGC94
MartinGC94 deleted the FixTypeInferenceInLongPipeline branch March 23, 2023 20:49
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tab completion should find members for $_ in a typed catch block

3 participants