-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve type inference for $_ #17716
Improve type inference for $_ #17716
Conversation
Co-authored-by: Ilya <darpa@yandex.ru>
There was a problem hiding this comment.
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.
Sorry for pushing new updates after the review, I forgot that there were other instances where |
There was a problem hiding this comment.
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!
src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Ilya <darpa@yandex.ru>
🎉 Handy links: |
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:
$Collection.Where({$_})
ls | where {$_}
switch ("hello") {'Hello'{$_}}
try{} catch {$_}
trap {$_}
PR Context
PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).