Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2930,8 +2930,10 @@ private sealed class VariableAssignmentVisitor : AstVisitor2

private void SetLastAssignment(Ast ast, bool enumerate = false, bool redirectionAssignment = false)
{
if (LastAssignmentOffset < ast.Extent.StartOffset)
if (LastAssignmentOffset < ast.Extent.StartOffset && !VariableTarget.Extent.IsWithin(ast.Extent))
{
// If the variable we are inferring the value of is inside this assignment then the assignment is invalid
// For example: $x = Get-Random; $x = $x.Where{$_.<Tab>} here the value should be inferred based on Get-Random and not $x = $x...
ClearAssignmentData();
LastAssignment = ast;
EnumerateAssignment = enumerate;
Expand Down
6 changes: 6 additions & 0 deletions test/powershell/engine/Api/TypeInference.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,12 @@ Describe "Type inference Tests" -tags "CI" {
$res.Name | Should -Be System.String
}

It 'Ignores the last assignment when a variable is reused' {
$variableAst = { $x = New-Guid; $x = $x.Where{$_} }.Ast.FindAll({ param($a) $a -is [Language.VariableExpressionAst] }, $true) | select -Last 1
$res = [AstTypeInference]::InferTypeOf($variableAst)
$res.Name | Should -Be System.Guid
}

$catchClauseTypes = @(
@{ Type = 'System.ArgumentException' }
@{ Type = 'System.ArgumentNullException' }
Expand Down
Loading