From 99f3bb7a6abc3143c832efe22fbfc79c2222b874 Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Wed, 4 May 2022 10:56:55 -0700 Subject: [PATCH] Fix line breakpoints for `return` statements without a value (#17179) --- .../engine/parser/Compiler.cs | 6 ++- .../Scripting/Debugging/Debugging.Tests.ps1 | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/parser/Compiler.cs b/src/System.Management.Automation/engine/parser/Compiler.cs index 7550a81d45..6d325aa3e8 100644 --- a/src/System.Management.Automation/engine/parser/Compiler.cs +++ b/src/System.Management.Automation/engine/parser/Compiler.cs @@ -642,7 +642,7 @@ namespace System.Management.Automation.Language internal static readonly MethodInfo ArgumentTransformationAttribute_Transform = typeof(ArgumentTransformationAttribute).GetMethod(nameof(ArgumentTransformationAttribute.Transform), InstancePublicFlags); - + internal static readonly MethodInfo MemberInvocationLoggingOps_LogMemberInvocation = typeof(MemberInvocationLoggingOps).GetMethod(nameof(MemberInvocationLoggingOps.LogMemberInvocation), StaticFlags); } @@ -5617,7 +5617,9 @@ namespace System.Management.Automation.Language return Expression.Block(returnValue, returnExpr); } - return returnExpr; + return Expression.Block( + UpdatePosition(returnStatementAst), + returnExpr); } public object VisitExitStatement(ExitStatementAst exitStatementAst) diff --git a/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 b/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 index c7c1b6a110..29e279a199 100644 --- a/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 +++ b/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 @@ -331,6 +331,50 @@ elseif (Test-Path $PSCommandPath) $Breakpoint.HitCount | Should -Be $HitCount } } + + Context "Break point on return should hit" { + BeforeAll { + $return_script_1 = @' +return +'@ + $return_script_2 = @' +return 10 +'@ + $return_script_3 = @' +trap { + 'statement to ignore trap registration sequence point' + return +} + +throw +'@ + + $ReturnScript_1 = Setup -PassThru -File ReturnScript_1.ps1 -Content $return_script_1 + $bp_1 = Set-PSBreakpoint -Script $ReturnScript_1 -Line 1 -Action { continue } + + $ReturnScript_2 = Setup -PassThru -File ReturnScript_2.ps1 -Content $return_script_2 + $bp_2 = Set-PSBreakpoint -Script $ReturnScript_2 -Line 1 -Action { continue } + + $ReturnScript_3 = Setup -PassThru -File ReturnScript_3.ps1 -Content $return_script_3 + $bp_3 = Set-PSBreakpoint -Script $ReturnScript_3 -Line 3 -Action { continue } + + $testCases = @( + @{ Name = "return without pipeline should be hit once"; Path = $ReturnScript_1; Breakpoint = $bp_1; HitCount = 1 } + @{ Name = "return with pipeline should be hit once"; Path = $ReturnScript_2; Breakpoint = $bp_2; HitCount = 1 } + @{ Name = "return from trap should be hit once"; Path = $ReturnScript_3; Breakpoint = $bp_3; HitCount = 1 } + ) + } + + AfterAll { + Get-PSBreakpoint -Script $ReturnScript_1, $ReturnScript_2, $ReturnScript_3 | Remove-PSBreakpoint + } + + It "Return statement " -TestCases $testCases { + param($Path, $Breakpoint, $HitCount) + $null = & $Path + $Breakpoint.HitCount | Should -Be $HitCount + } + } } Describe "It should be possible to reset runspace debugging" -Tag "Feature" {