Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@SeeminglyScience
Copy link
Collaborator

@SeeminglyScience SeeminglyScience commented Apr 22, 2022

Fixes #17081

PR Summary

Adds an explicit call to UpdatePosition when a return statement does not include a pipeline.

Codegen before change:

// Generated with:
// { return } | Get-ScriptBlockDisassembly -IgnoreStartupAndTeardown
// ScriptBlock.EndBlock
funcContext._currentSequencePointIndex = 0;
try
{
    return ;
}
catch (FlowControlException)
{
    throw;
}
catch (Exception exception)
{
    ExceptionHandlingOps.CheckActionPreference(funcContext, exception);
}
funcContext._currentSequencePointIndex = 1;

if (context._debuggingMode > 0)
{
    context._debugger.OnSequencePointHit(funcContext);
}

Codegen after change:

// Generated with:
// { return } | Get-ScriptBlockDisassembly -IgnoreStartupAndTeardown
// ScriptBlock.EndBlock
funcContext._currentSequencePointIndex = 0;
try
{
    funcContext._currentSequencePointIndex = 1;

    if (context._debuggingMode > 0)
    {
        context._debugger.OnSequencePointHit(funcContext);
    }
    return ;
}
catch (FlowControlException)
{
    throw;
}
catch (Exception exception)
{
    ExceptionHandlingOps.CheckActionPreference(funcContext, exception);
}
funcContext._currentSequencePointIndex = 2;

if (context._debuggingMode > 0)
{
    context._debugger.OnSequencePointHit(funcContext);
}

Trap compilation (click to expand)

Turns out there wasn't an easy way to view disassembly of a trap with ScriptBlockDisassembler. So that took a minute to figure out how to do easily:

Code to generate (click to expand)

#requires -Modules ImpliedReflection, ScriptBlockDisassembler

Enable-ImpliedReflection -YesIKnowIShouldNotDoThis

$sb = {
    trap { return }
    throw
}

$trap = $sb.Ast.EndBlock.Traps[0]
$main = [System.Management.Automation.Language.Compiler]::new()
$main.Compile($sb.ScriptBlockData, $true)
$secondary = [System.Management.Automation.Language.Compiler]::new(
    $main._sequencePoints,
    $main._sequencePointIndexMap)

$secondary._compilingTrap = $true
$funcName = $main._currentFunctionName + '<trap>'
if ($null -ne $trap.TrapType) {
    $funcName += '<' + $trap.TrapType.TypeName.Name + '>'
}

$tuple = [Management.Automation.Language.VariableAnalysis]::AnalyzeTrap($trap)
$secondary.LocalVariablesTupleType = $tuple.Item1
$secondary.LocalVariablesParameter = [Linq.Expressions.Expression]::Variable(
    $secondary.LocalVariablesTupleType,
    'locals')

$lambda = $secondary.CompileSingleLambda(
    $trap.Body.Statements,
    $trap.Body.Traps,
    $funcName,
    <# entryExtent: #> $null,
    <# exitExtent: #> $null,
    <# rootForDefiningTypesAndUsings: #> $null)

$lambda | Format-ExpressionTree | bat -l cs

Codegen before:

(FunctionContext funcContext) =>
{
    try
    {
        ExecutionContext context = funcContext._executionContext;
        MutableTuple<object, object[], object, object, PSScriptCmdlet, PSBoundParametersDictionary, InvocationInfo, string, string, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null> locals = (MutableTuple<object, object[], object, object, PSScriptCmdlet, PSBoundParametersDictionary, InvocationInfo, string, string, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null>)funcContext._localsTuple;
        funcContext._functionName = "<ScriptBlock><trap>";
        context._debugger.EnterScriptFunction(funcContext);
        try
        {
            throw new ReturnException(AutomationNull.Value);
        }
        catch (FlowControlException)
        {
            throw;
        }
        catch (Exception exception)
        {
            ExceptionHandlingOps.CheckActionPreference(funcContext, exception);
        }
    }
    finally
    {
        context._debugger.ExitScriptFunction();
    }
}

Codegen after:

(FunctionContext funcContext) =>
{
    try
    {
        ExecutionContext context = funcContext._executionContext;
        MutableTuple<object, object[], object, object, PSScriptCmdlet, PSBoundParametersDictionary, InvocationInfo, string, string, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null> locals = (MutableTuple<object, object[], object, object, PSScriptCmdlet, PSBoundParametersDictionary, InvocationInfo, string, string, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null, LanguagePrimitives.Null>)funcContext._localsTuple;
        funcContext._functionName = "<ScriptBlock><trap>";
        context._debugger.EnterScriptFunction(funcContext);
        try
        {
            funcContext._currentSequencePointIndex = 1;

            if (context._debuggingMode > 0)
            {
                context._debugger.OnSequencePointHit(funcContext);
            }
            throw new ReturnException(AutomationNull.Value);
        }
        catch (FlowControlException)
        {
            throw;
        }
        catch (Exception exception)
        {
            ExceptionHandlingOps.CheckActionPreference(funcContext, exception);
        }
    }
    finally
    {
        context._debugger.ExitScriptFunction();
    }
}

PR Context

PR Checklist

Fixes PowerShell#17081

Adds an explicit call to UpdatePosition when a return statement does
not include a pipeline.
Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a comment

'@
$return_script_2 = @'
return 10
'@
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add one more test case to cover return statement in a trap block?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Also added disassembly for traps in the description.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should have waited for the tests to pass before saying that. Looking into the failure

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay it really does hit twice. Apparently when a trap is present that hits a breakpoint when it's registered. So even with this:

'something'

trap {
    'something else'
    return 'testing'
}

throw

If you put a breakpoint on every line and run it, line 4 hits a breakpoint first, then line 1, then 8, then 5. That seems like a bug but for another time. I've added another line before the return in the trap test so that doesn't interfere with the result.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the additional fix. Will take a look today.

@pull-request-quantifier-deprecated

This PR has 35 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +34 -1
Percentile : 14%

Total files changed: 2

Change summary by file extension:
.cs : +3 -1
.ps1 : +31 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detetcted.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@daxian-dbw daxian-dbw merged commit 99f3bb7 into PowerShell:master May 4, 2022
@SeeminglyScience SeeminglyScience deleted the SeeminglyScience/issue17081 branch May 4, 2022 20:54
@ghost
Copy link

ghost commented May 23, 2022

🎉v7.3.0-preview.4 has been released which incorporates this pull request.:tada:

Handy links:

@ghost ghost mentioned this pull request May 23, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Breakpoint set on return doesn't work properly

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.