Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Improve type inference for $_#17716

Merged
daxian-dbw merged 8 commits into
PowerShell:masterPowerShell/PowerShell:masterfrom
MartinGC94:FixTypeInferenceInLongPipelineMartinGC94/PowerShell:FixTypeInferenceInLongPipelineCopy head branch name to clipboard
Jul 25, 2022
Merged

Improve type inference for $_#17716
daxian-dbw merged 8 commits into
PowerShell:masterPowerShell/PowerShell:masterfrom
MartinGC94:FixTypeInferenceInLongPipelineMartinGC94/PowerShell:FixTypeInferenceInLongPipelineCopy head branch name to clipboard

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

@MartinGC94 MartinGC94 requested a review from daxian-dbw as a code owner July 19, 2022 21:48
@ghost ghost assigned daxian-dbw Jul 19, 2022
@iSazonov 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 requested a review from iSazonov July 20, 2022 22:14
@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 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 iSazonov self-requested a review July 22, 2022 16:19

@daxian-dbw 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!

Comment thread src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs Outdated
Comment thread src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs Outdated
Co-authored-by: Ilya <darpa@yandex.ru>
@daxian-dbw daxian-dbw merged commit 3d4e294 into PowerShell:master Jul 25, 2022
@ghost

ghost commented Aug 12, 2022

Copy link
Copy Markdown

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

Handy links:

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.