Skip to content

Commit ab7f206

Browse files
authored
Fix type inference error for empty return statements (PowerShell#18351)
1 parent f12a4f8 commit ab7f206

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,11 @@ object ICustomAstVisitor.VisitContinueStatement(ContinueStatementAst continueSta
936936

937937
object ICustomAstVisitor.VisitReturnStatement(ReturnStatementAst returnStatementAst)
938938
{
939+
if (returnStatementAst.Pipeline is null)
940+
{
941+
return TypeInferenceContext.EmptyPSTypeNameArray;
942+
}
943+
939944
return returnStatementAst.Pipeline.Accept(this);
940945
}
941946

test/powershell/engine/Api/TypeInference.Tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,11 @@ Describe "Type inference Tests" -tags "CI" {
796796
$res.Name | Should -Be 'System.Int32'
797797
}
798798

799+
It 'Infers type from empty Return statement' {
800+
$res = [AstTypeInference]::InferTypeOf( { return }.Ast)
801+
$res.Count | Should -Be 0
802+
}
803+
799804
It 'Infers type from New-Object statement' {
800805
$res = [AstTypeInference]::InferTypeOf( {
801806
New-Object -TypeName 'System.Diagnostics.Stopwatch'

0 commit comments

Comments
 (0)