Skip to content

Commit 99f3bb7

Browse files
Fix line breakpoints for return statements without a value (PowerShell#17179)
1 parent 2550a3a commit 99f3bb7

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ internal static class CachedReflectionInfo
642642

643643
internal static readonly MethodInfo ArgumentTransformationAttribute_Transform =
644644
typeof(ArgumentTransformationAttribute).GetMethod(nameof(ArgumentTransformationAttribute.Transform), InstancePublicFlags);
645-
645+
646646
internal static readonly MethodInfo MemberInvocationLoggingOps_LogMemberInvocation =
647647
typeof(MemberInvocationLoggingOps).GetMethod(nameof(MemberInvocationLoggingOps.LogMemberInvocation), StaticFlags);
648648
}
@@ -5617,7 +5617,9 @@ public object VisitReturnStatement(ReturnStatementAst returnStatementAst)
56175617
return Expression.Block(returnValue, returnExpr);
56185618
}
56195619

5620-
return returnExpr;
5620+
return Expression.Block(
5621+
UpdatePosition(returnStatementAst),
5622+
returnExpr);
56215623
}
56225624

56235625
public object VisitExitStatement(ExitStatementAst exitStatementAst)

test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,50 @@ elseif (Test-Path $PSCommandPath)
331331
$Breakpoint.HitCount | Should -Be $HitCount
332332
}
333333
}
334+
335+
Context "Break point on return should hit" {
336+
BeforeAll {
337+
$return_script_1 = @'
338+
return
339+
'@
340+
$return_script_2 = @'
341+
return 10
342+
'@
343+
$return_script_3 = @'
344+
trap {
345+
'statement to ignore trap registration sequence point'
346+
return
347+
}
348+
349+
throw
350+
'@
351+
352+
$ReturnScript_1 = Setup -PassThru -File ReturnScript_1.ps1 -Content $return_script_1
353+
$bp_1 = Set-PSBreakpoint -Script $ReturnScript_1 -Line 1 -Action { continue }
354+
355+
$ReturnScript_2 = Setup -PassThru -File ReturnScript_2.ps1 -Content $return_script_2
356+
$bp_2 = Set-PSBreakpoint -Script $ReturnScript_2 -Line 1 -Action { continue }
357+
358+
$ReturnScript_3 = Setup -PassThru -File ReturnScript_3.ps1 -Content $return_script_3
359+
$bp_3 = Set-PSBreakpoint -Script $ReturnScript_3 -Line 3 -Action { continue }
360+
361+
$testCases = @(
362+
@{ Name = "return without pipeline should be hit once"; Path = $ReturnScript_1; Breakpoint = $bp_1; HitCount = 1 }
363+
@{ Name = "return with pipeline should be hit once"; Path = $ReturnScript_2; Breakpoint = $bp_2; HitCount = 1 }
364+
@{ Name = "return from trap should be hit once"; Path = $ReturnScript_3; Breakpoint = $bp_3; HitCount = 1 }
365+
)
366+
}
367+
368+
AfterAll {
369+
Get-PSBreakpoint -Script $ReturnScript_1, $ReturnScript_2, $ReturnScript_3 | Remove-PSBreakpoint
370+
}
371+
372+
It "Return statement <Name>" -TestCases $testCases {
373+
param($Path, $Breakpoint, $HitCount)
374+
$null = & $Path
375+
$Breakpoint.HitCount | Should -Be $HitCount
376+
}
377+
}
334378
}
335379

336380
Describe "It should be possible to reset runspace debugging" -Tag "Feature" {

0 commit comments

Comments
 (0)