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 @@ -2942,13 +2942,16 @@ private void SetLastAssignment(Ast ast, bool enumerate = false, bool redirection
}
}

private void SetLastAssignmentType(PSTypeName typeName, int assignmentOffset)
private void SetLastAssignmentType(PSTypeName typeName, IScriptExtent assignmentExtent)
{
if (LastAssignmentOffset < assignmentOffset)
if (LastAssignmentOffset < assignmentExtent.StartOffset && !VariableTarget.Extent.IsWithin(assignmentExtent))
{
// If the variable we are inferring the value of is inside this assignment then the assignment is invalid
// For example: $x = 1..10; Get-Random 2>variable:x -InputObject ($x.<Tab>) here the variable should be inferred based on the initial 1..10 assignment
// and not the error redirected variable.
ClearAssignmentData();
LastAssignmentType = typeName;
LastAssignmentOffset = assignmentOffset;
LastAssignmentOffset = assignmentExtent.StartOffset;
}
}

Expand Down Expand Up @@ -3109,17 +3112,17 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
{
case "ErrorVariable":
case "ev":
SetLastAssignmentType(new PSTypeName(typeof(List<ErrorRecord>)), commandAst.Extent.StartOffset);
SetLastAssignmentType(new PSTypeName(typeof(List<ErrorRecord>)), commandAst.Extent);
break;

case "WarningVariable":
case "wv":
SetLastAssignmentType(new PSTypeName(typeof(List<WarningRecord>)), commandAst.Extent.StartOffset);
SetLastAssignmentType(new PSTypeName(typeof(List<WarningRecord>)), commandAst.Extent);
break;

case "InformationVariable":
case "iv":
SetLastAssignmentType(new PSTypeName(typeof(List<InformationalRecord>)), commandAst.Extent.StartOffset);
SetLastAssignmentType(new PSTypeName(typeof(List<InformationalRecord>)), commandAst.Extent);
break;

case "OutVariable":
Expand Down Expand Up @@ -3166,23 +3169,23 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
switch (fileRedirection.FromStream)
{
case RedirectionStream.Error:
SetLastAssignmentType(new PSTypeName(typeof(ErrorRecord)), commandAst.Extent.StartOffset);
SetLastAssignmentType(new PSTypeName(typeof(ErrorRecord)), commandAst.Extent);
break;

case RedirectionStream.Warning:
SetLastAssignmentType(new PSTypeName(typeof(WarningRecord)), commandAst.Extent.StartOffset);
SetLastAssignmentType(new PSTypeName(typeof(WarningRecord)), commandAst.Extent);
break;

case RedirectionStream.Verbose:
SetLastAssignmentType(new PSTypeName(typeof(VerboseRecord)), commandAst.Extent.StartOffset);
SetLastAssignmentType(new PSTypeName(typeof(VerboseRecord)), commandAst.Extent);
break;

case RedirectionStream.Debug:
SetLastAssignmentType(new PSTypeName(typeof(DebugRecord)), commandAst.Extent.StartOffset);
SetLastAssignmentType(new PSTypeName(typeof(DebugRecord)), commandAst.Extent);
break;

case RedirectionStream.Information:
SetLastAssignmentType(new PSTypeName(typeof(InformationRecord)), commandAst.Extent.StartOffset);
SetLastAssignmentType(new PSTypeName(typeof(InformationRecord)), commandAst.Extent);
break;

default:
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 assignment when a variable is declared and used within the same commandAst' {
$variableAst = { Get-Random 2>variable:RandomError1 -InputObject ($RandomError1) }.Ast.FindAll({ param($a) $a -is [Language.VariableExpressionAst] }, $true) | select -Last 1
$res = [AstTypeInference]::InferTypeOf($variableAst)
$res.Count | Should -Be 0
}

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)
Expand Down
Loading