Improve type inference of array literals and foreach statement variables#8100
Conversation
Fix an issue where the assignment of a foreach statement would not be detected during type inference. This issue was caused by a check that the end offset of the foreach statement was before the start offset of the variable expression. This was never true inside the foreach statement body, which caused the assignement never to be included.
Added the ability to infer an explicit array or array literal expression as a strongly typed array based on the inferred type of it's elements.
Inferring the type of the current enumerated value variable now has better detection of how the foreach condition would enumerate.
|
This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days. |
|
@BrucePay @TravisEz13 (when you guys get back from the holiday break) was there something pending on this PR? 🙂 |
|
This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days. |
|
I got caught up in other projects. I'm planning to address feedback this weekend. |
|
@SeeminglyScience I think this is complete, but I'm going to hold this PR until 6.3. We are really close to starting lock down for release and I want to make sure a change like this has plenty of time to get feedback before the release. My primary reasoning is that there maybe script that make assumptions about our type inferences and break because of this change. Although this change shouldn't be a breaking change, I think we should be a little cautious. |
|
This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days. |
|
@TravisEz13 @iSazonov Is it possible to disable stale bot since this is waiting on 6.2 GA? |
|
Nevermind. Any comment removes the mark. |
|
We have already forked for GA. |
PR Summary
Improve type inference for
foreachstatement variables by:Inferring strongly typed arrays from explicit array and array literal expressions when elements are of the same inferred type
Fix detection of
foreachvariable declaration. The previous logic was to check if the variable expression's start offset was after the end offset of theforeachstatement, which will never be true in the bodyImprove inference of what type the "Condition" of a
foreachstatement will enumerate asHere's some examples of scenarios that now work correctly:
Note: I specifically avoided testing forFound it, I used PSEnumerableBinder for reference.IEnumerable<>directly because some types likestringare not enumerated by PowerShell. I know there's more types thanstring, if someone could point me to a full list of types that implementIEnumerablebut PowerShell doesn't enumerate, I'll add in testing for that as well.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