@@ -988,7 +988,7 @@ private readonly Dictionary<Clause, IReadOnlyList<ExecutionRegionSyntax>>
988988 private long _locationStateMutationCount ;
989989 private long _childScopeEscapeRiskCount ;
990990 private bool _pipelineStageMayReceiveInput ;
991- private bool _pipelineCallbacksMayInterleave ;
991+ private bool _pipelineStageEffectsMayReachRegionBodies ;
992992
993993 private PwshForEachValueAnalyzer (
994994 PwshParserOptions options ,
@@ -1220,6 +1220,15 @@ flow.OnFailure is AnalysisContext failure
12201220 flow ) ;
12211221 }
12221222
1223+ if ( binding . ParameterSet == PwshExecutionRegionParameterSet . InvokeInProcess )
1224+ {
1225+ return AnalyzeInProcessInvokeCommand (
1226+ binding ,
1227+ regions [ 0 ] ,
1228+ regionInput ,
1229+ flow ) ;
1230+ }
1231+
12231232 var executionRegionEffectCount = _executionRegionEffectCount ;
12241233 var nonRegionStateMutationCount = _nonRegionStateMutationCount ;
12251234 var bodyFlow = AnalyzeExecutionRegionBody ( regions [ 0 ] . Body , regionInput ) ;
@@ -1230,6 +1239,48 @@ flow.OnFailure is AnalysisContext failure
12301239 : flow ;
12311240 }
12321241
1242+ private PwshFlowResult AnalyzeInProcessInvokeCommand (
1243+ PwshExecutionRegionBindingResult binding ,
1244+ ExecutionRegionSyntax region ,
1245+ AnalysisContext input ,
1246+ PwshFlowResult fallback )
1247+ {
1248+ var executionRegionEffectCount = _executionRegionEffectCount ;
1249+ var nonRegionStateMutationCount = _nonRegionStateMutationCount ;
1250+ var locationStateMutationCount = _locationStateMutationCount ;
1251+ var childScopeEscapeRiskCount = _childScopeEscapeRiskCount ;
1252+ var body = AnalyzeExecutionRegionBody ( region . Body , input ) ;
1253+ var locationMutated = _locationStateMutationCount > locationStateMutationCount ;
1254+ var childScopeMayEscape =
1255+ _childScopeEscapeRiskCount > childScopeEscapeRiskCount ;
1256+ _executionRegionEffectCount = executionRegionEffectCount + 1 ;
1257+ _nonRegionStateMutationCount = nonRegionStateMutationCount ;
1258+
1259+ if ( binding . HasNoNewScope )
1260+ {
1261+ return body . JoinedState is AnalysisContext bodyExit
1262+ ? PwshFlowResult . Both ( bodyExit )
1263+ : fallback ;
1264+ }
1265+
1266+ _childScopeEscapeRiskCount = childScopeEscapeRiskCount +
1267+ ( childScopeMayEscape ? 1 : 0 ) ;
1268+ var restoredExit = AnalysisContext . JoinNullable (
1269+ RestoreChildScopeExit (
1270+ body . OnSuccess ,
1271+ input ,
1272+ locationMutated ,
1273+ childScopeMayEscape ) ,
1274+ RestoreChildScopeExit (
1275+ body . OnFailure ,
1276+ input ,
1277+ locationMutated ,
1278+ childScopeMayEscape ) ) ;
1279+ return restoredExit is AnalysisContext joined
1280+ ? PwshFlowResult . Both ( joined )
1281+ : fallback ;
1282+ }
1283+
12331284 private PwshFlowResult AnalyzePipelineCallbackRegions (
12341285 PwshExecutionRegionBindingResult binding ,
12351286 IReadOnlyList < ExecutionRegionSyntax > regions ,
@@ -1362,7 +1413,7 @@ private PwshFlowResult AnalyzeExecutionRegionBody(
13621413 {
13631414 var enclosingStageMayReceiveInput = _pipelineStageMayReceiveInput ;
13641415 _pipelineStageMayReceiveInput = false ;
1365- var bodyInput = _pipelineCallbacksMayInterleave
1416+ var bodyInput = _pipelineStageEffectsMayReachRegionBodies
13661417 ? input . Invalidate ( unknownCwd : true )
13671418 : input ;
13681419 var flow = AnalyzeBlock ( body , bodyInput ) ;
@@ -1445,6 +1496,7 @@ private static bool IsSupportedSynchronousReceiver(
14451496 binding . Status == PwshExecutionRegionBindingStatus . ProvedExecution &&
14461497 ( binding . ParameterSet is PwshExecutionRegionParameterSet . MeasureExpression or
14471498 PwshExecutionRegionParameterSet . TraceExpression or
1499+ PwshExecutionRegionParameterSet . InvokeInProcess or
14481500 PwshExecutionRegionParameterSet . ForEachScriptBlock or
14491501 PwshExecutionRegionParameterSet . WhereScriptBlock ) &&
14501502 AllBindingsAreCompleteAndSynchronous ( binding . Bindings ) ;
@@ -1606,8 +1658,10 @@ private PwshFlowResult AnalyzeList(CommandListSyntax list, AnalysisContext input
16061658 private PwshFlowResult AnalyzePipeline ( PipelineSyntax pipeline , AnalysisContext input )
16071659 {
16081660 var stageInput = input ;
1609- var enclosingCallbacksMayInterleave = _pipelineCallbacksMayInterleave ;
1610- _pipelineCallbacksMayInterleave |= PipelineCallbacksMayInterleave ( pipeline ) ;
1661+ var enclosingStageEffectsMayReachBodies =
1662+ _pipelineStageEffectsMayReachRegionBodies ;
1663+ _pipelineStageEffectsMayReachRegionBodies |=
1664+ PipelineStageEffectsMayReachRegionBodies ( pipeline ) ;
16111665 try
16121666 {
16131667 for ( var stageIndex = 0 ; stageIndex < pipeline . Stages . Count ; stageIndex ++ )
@@ -1644,35 +1698,43 @@ private PwshFlowResult AnalyzePipeline(PipelineSyntax pipeline, AnalysisContext
16441698 }
16451699 finally
16461700 {
1647- _pipelineCallbacksMayInterleave = enclosingCallbacksMayInterleave ;
1701+ _pipelineStageEffectsMayReachRegionBodies =
1702+ enclosingStageEffectsMayReachBodies ;
16481703 }
16491704 }
16501705
1651- private static bool PipelineCallbacksMayInterleave ( PipelineSyntax pipeline )
1706+ private static bool PipelineStageEffectsMayReachRegionBodies ( PipelineSyntax pipeline )
16521707 {
1653- var hasCallback = false ;
1708+ var hasPipelineSensitiveRegion = false ;
16541709 var statefulStageCount = 0 ;
16551710 for ( var index = 0 ; index < pipeline . Stages . Count ; index ++ )
16561711 {
16571712 var stage = pipeline . Stages [ index ] ;
1658- hasCallback |= ContainsPipelineCallback ( stage ) ;
1713+ hasPipelineSensitiveRegion |= ContainsPipelineSensitiveExecutionRegion ( stage ) ;
16591714 if ( MayMutatePipelineState ( stage ) )
16601715 {
16611716 statefulStageCount ++ ;
16621717 }
16631718 }
16641719
1665- return hasCallback && statefulStageCount > 1 ;
1720+ return hasPipelineSensitiveRegion && statefulStageCount > 1 ;
16661721 }
16671722
1668- private static bool ContainsPipelineCallback ( ShellSyntaxNode node ) =>
1723+ private static bool ContainsPipelineSensitiveExecutionRegion ( ShellSyntaxNode node ) =>
16691724 node switch
16701725 {
1671- SimpleCommandSyntax simple => IsPipelineCallback ( simple ) ,
1672- ShellBlockSyntax block => BlockContains ( block , ContainsPipelineCallback ) ,
1673- GroupSyntax group => ContainsPipelineCallback ( group . Body ) ,
1674- CommandListSyntax list => ListContains ( list , ContainsPipelineCallback ) ,
1675- PipelineSyntax pipeline => PipelineContains ( pipeline , ContainsPipelineCallback ) ,
1726+ SimpleCommandSyntax simple => IsPipelineSensitiveExecutionRegionHost ( simple ) ,
1727+ ShellBlockSyntax block => BlockContains (
1728+ block ,
1729+ ContainsPipelineSensitiveExecutionRegion ) ,
1730+ GroupSyntax group => ContainsPipelineSensitiveExecutionRegion ( group . Body ) ,
1731+ CommandListSyntax list => ListContains (
1732+ list ,
1733+ ContainsPipelineSensitiveExecutionRegion ) ,
1734+ PipelineSyntax pipeline => PipelineContains (
1735+ pipeline ,
1736+ ContainsPipelineSensitiveExecutionRegion ) ,
1737+ ExecutionRegionSyntax => true ,
16761738 _ => false ,
16771739 } ;
16781740
@@ -1690,14 +1752,17 @@ private static bool MayMutatePipelineState(ShellSyntaxNode node) =>
16901752 _ => false ,
16911753 } ;
16921754
1693- private static bool IsPipelineCallback ( SimpleCommandSyntax simple )
1755+ private static bool IsPipelineSensitiveExecutionRegionHost ( SimpleCommandSyntax simple )
16941756 {
16951757 var binding = PwshExecutionRegionBindingCatalog . Bind (
16961758 simple . Clause ,
16971759 commandIdentityProven : true ) ;
16981760 return binding . Status == PwshExecutionRegionBindingStatus . ProvedExecution &&
16991761 binding . ParameterSet is PwshExecutionRegionParameterSet . ForEachScriptBlock or
1700- PwshExecutionRegionParameterSet . WhereScriptBlock ;
1762+ PwshExecutionRegionParameterSet . WhereScriptBlock or
1763+ PwshExecutionRegionParameterSet . InvokeInProcess or
1764+ PwshExecutionRegionParameterSet . MeasureExpression or
1765+ PwshExecutionRegionParameterSet . TraceExpression ;
17011766 }
17021767
17031768 private static bool SimpleMayMutatePipelineState ( SimpleCommandSyntax simple )
@@ -1818,19 +1883,19 @@ private PwshFlowResult AnalyzeExecutionRegion(
18181883 }
18191884
18201885 return new PwshFlowResult (
1821- RestoreDirectCallExit (
1886+ RestoreChildScopeExit (
18221887 body . OnSuccess ,
18231888 input ,
18241889 locationMutated ,
18251890 childScopeMayEscape ) ,
1826- RestoreDirectCallExit (
1891+ RestoreChildScopeExit (
18271892 body . OnFailure ,
18281893 input ,
18291894 locationMutated ,
18301895 childScopeMayEscape ) ) ;
18311896 }
18321897
1833- private static AnalysisContext ? RestoreDirectCallExit (
1898+ private static AnalysisContext ? RestoreChildScopeExit (
18341899 AnalysisContext ? bodyExit ,
18351900 AnalysisContext input ,
18361901 bool locationMutated ,
0 commit comments