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 @@ -739,19 +739,28 @@ object ICustomAstVisitor.VisitSubExpression(SubExpressionAst subExpressionAst)
object ICustomAstVisitor.VisitErrorStatement(ErrorStatementAst errorStatementAst)
{
var inferredTypes = new List<PSTypeName>();
foreach (var ast in errorStatementAst.Conditions)
if (errorStatementAst.Conditions is not null)
{
inferredTypes.AddRange(InferTypes(ast));
foreach (var ast in errorStatementAst.Conditions)
{
inferredTypes.AddRange(InferTypes(ast));
}
}

foreach (var ast in errorStatementAst.Bodies)
if (errorStatementAst.Bodies is not null)
{
inferredTypes.AddRange(InferTypes(ast));
foreach (var ast in errorStatementAst.Bodies)
{
inferredTypes.AddRange(InferTypes(ast));
}
}

foreach (var ast in errorStatementAst.NestedAst)
if (errorStatementAst.NestedAst is not null)
{
inferredTypes.AddRange(InferTypes(ast));
foreach (var ast in errorStatementAst.NestedAst)
{
inferredTypes.AddRange(InferTypes(ast));
}
}

return inferredTypes;
Expand Down Expand Up @@ -1944,7 +1953,8 @@ private void InferTypeFrom(VariableExpressionAst variableExpressionAst, List<PST
{
break;
}
else if (parent is SwitchStatementAst switchStatement)
else if (parent is SwitchStatementAst switchStatement
&& switchStatement.Condition.Extent.EndOffset < variableExpressionAst.Extent.StartOffset)
{
parent = switchStatement.Condition;
break;
Expand Down
8 changes: 8 additions & 0 deletions test/powershell/engine/Api/TypeInference.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,14 @@ Describe "Type inference Tests" -tags "CI" {
$res = [AstTypeInference]::InferTypeOf( { pwsh }.Ast)
$res.Name | Should -Be 'System.String'
}

It 'Should not throw when inferring $_ in switch condition' {
$FoundAst = { switch($_){default{}} }.Ast.Find(
{param($Ast) $Ast -is [Language.VariableExpressionAst]},
$true
)
$null = [AstTypeInference]::InferTypeOf($FoundAst)
}
}

Describe "AstTypeInference tests" -Tags CI {
Expand Down