-
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
Fix variable type inference precedence #18691
Changes from all commits
e6f31b0
989134d
c91e29f
6ba92b5
1289c4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1367,6 +1367,24 @@ Describe "Type inference Tests" -tags "CI" { | |
$res.Name -join ' ' | Should -Be "System.IO.FileInfo System.IO.DirectoryInfo" | ||
} | ||
|
||
It 'Infers closest variable type' { | ||
$res = [AstTypeInference]::InferTypeOf( { [string]$TestVar = "";[hashtable]$TestVar = @{};$TestVar }.Ast) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should there be some sort of order checking here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. There shouldn't even be multiple inferred types but because assignments are included when inferring the output from a scriptblock I need that |
||
$res.Name | Select-Object -Last 1 | Should -Be "System.Collections.Hashtable" | ||
} | ||
|
||
It 'Infers closest variable type and ignores unrelated param blocks' { | ||
$res = [AstTypeInference]::InferTypeOf( { | ||
[hashtable]$ParameterName=@{} | ||
function Verb-Noun { | ||
param | ||
( | ||
[string]$ParameterName | ||
) | ||
} | ||
$ParameterName }.Ast) | ||
$res.Name | Should -Be "System.Collections.Hashtable" | ||
} | ||
|
||
It 'Infers type of $null after variable assignment' { | ||
$res = [AstTypeInference]::InferTypeOf( { $null = "Hello";$null }.Ast) | ||
$res.Count | Should -Be 0 | ||
|
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.
@MartinGC94 This change came from the commit c91e29f, which was reverted by 1289c4e. However, this change was left behind. Did you mean to revert this change as well?
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.
No, that was a fix for this: #18691 (comment) I was just lazy and put 2 changes in 1 commit which was clearly a bad idea 😄