-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add Type Inference for $_ / $PSItem in catch{ } blocks #8020
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
Add Type Inference for $_ / $PSItem in catch{ } blocks #8020
Conversation
|
Perhaps we need a test that |
lzybkr
left a comment
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.
It would be cool to also add special handling for:
try {}
catch [SomeSpecificException] { $_.Exception.<TAB> }I realize this could be a bit messy though and I'm not sure it's commonly needed, so maybe it's not worth it.
|
Can you expand upon that? As-is, the currently available logic handles this as you'd expect, completing the members for a generic exception object. Are there some exceptions with additional members we would want to try to detect from the exception type handed to the catch block itself? I'm not aware of any, but I'm sure there are probably some or could have some definitely in the case of custom exception types as well. @lzybkr I don't think this would go quite in the same place on this code (would it?), so if you could point out where it would need to be added, I'll give it a whirl. |
|
@vexx32 Please add test to demo that both cases work. |
9c1c9c3 to
586aae3
Compare
|
Added tests. With @SeeminglyScience's help, also added in some type inference for typed catch blocks. He came up with the idea of a dummy generic Pretty happy with it! 😄 |
0e24c59 to
dd61d52
Compare
|
Clever but simple, perfect. 👍👍👍 |
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.
$res | Should -HaveCount 1
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.
Could you please address the comment?
Line 941 too.
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.
All done. There are a few tests for other things that should probably use this as well, but I think that might be better left to another PR specifically for test revisions.
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.
Please add more catch-es.
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.
Reformatted this for additional Test Cases with a variety of exception types.
Would you like me to add additional tests verifying that constructs like catch [type1], [type2] {} and subsequent catch blocks all work as expected?
Upon having this thought, I just went ahead and did it; figured it better be as robust as we can make it!
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.
I meant:
"try {} catch [System.OverflowException] catch [System.TimeoutException] { $_.Exception }"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.
See additional below tests. I suppose I only test catch [type] {$_.Exception} catch {$_.Exception} but if you want me to go through a couple stacks I'm game. :)
Add dummy generic errorrecord class for type inference Add logic for inferring errorrecord/exception type Add tests for typed catch block inference Tidy Exception<E> class
Update generic type argument Update tests. (thanks @SeeminglyScience!)
d09fb29 to
b9eaea2
Compare
|
Appveyor caught something funky, don't think it was related. Rebased to restart it. Other than that... everything looks like it works as it ought. 😄 |
PR Summary
Fix #7828
$_/$PSItemwhen it is used to refer to theErrorRecordin a catch block.catch [ExceptionType] { $_ }blocks such that$_is inferred as a dummyErrorRecord<TException>which is identical toErrorRecordbut with aTException-typedExceptionmember.$_.Exception.<tab>Example:
(Compare current behaviour, where tab completion is missing due to a lack of type inference on the
$_/$PSItemvariable.)PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests