Skip to content

Commit 6e59cd8

Browse files
Fix for block within async when last expression is not an await
1 parent 1750620 commit 6e59cd8

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/Hyperbee.Expressions/CompilerServices/LoweringVisitor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,19 @@ protected override Expression VisitBlock( BlockExpression node )
181181
StateNode firstGoto = null;
182182
StateNode previousTail = null;
183183

184-
foreach ( var expression in node.Expressions )
184+
for ( var index = 0; index < node.Expressions.Count; index++ )
185185
{
186+
var expression = node.Expressions[index];
187+
186188
if ( RequiresLowering( expression ) )
187189
{
188-
var updated = VisitBranch( expression, joinState, resultVariable ); // Warning: visitation mutates the tail state.
190+
var updated =
191+
VisitBranch( expression, joinState, resultVariable ); // Warning: visitation mutates the tail state.
192+
193+
// handle last expression in the block
194+
if ( index == node.Expressions.Count -1 )
195+
previousVariable = updated.Result.Variable;
189196

190-
previousVariable = updated.Result.Variable;
191197
joinState.Result.Variable = previousVariable;
192198

193199
// Fix tail linked list of Transitions.

src/Hyperbee.Expressions/CompilerServices/StateMachineBuilder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Linq.Expressions;
1+
using System.Linq.Expressions;
22
using System.Reflection;
33
using System.Reflection.Emit;
44
using System.Runtime.CompilerServices;
@@ -53,7 +53,6 @@ public Expression CreateStateMachine( LoweringTransformer loweringTransformer, i
5353
//
5454
// stateMachine.__builder<> = new AsyncInterpreterTaskBuilder<TResult>();
5555
// stateMachine.__state<> = -1;
56-
// stateMachine.<extern_fields> = <extern_fields>;
5756
//
5857
// stateMachine.__moveNextDelegate<> = (ref StateMachine stateMachine) => { ... }
5958
// stateMachine._builder.Start<StateMachineType>( ref stateMachine );

test/Hyperbee.Expressions.Tests/BlockAsyncBasicTests.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,13 @@ public async Task BlockAsync_ShouldAwaitSuccessfully_WithBlockConditional( Compl
627627
Block(
628628
Constant( 5 ),
629629
Condition( Constant( true ),
630-
Await( AsyncHelper.Completer(
631-
Constant( completer ),
632-
Constant( 1 )
633-
) ),
630+
Block(
631+
Await( AsyncHelper.Completer(
632+
Constant( completer ),
633+
Constant( 1 )
634+
) ),
635+
Constant( 2 )
636+
),
634637
Constant( 0 )
635638
)
636639
)
@@ -642,7 +645,7 @@ public async Task BlockAsync_ShouldAwaitSuccessfully_WithBlockConditional( Compl
642645
var result = await compiledLambda();
643646

644647
// Assert
645-
Assert.AreEqual( 1, result );
648+
Assert.AreEqual( 2, result );
646649
}
647650

648651
public class TestContext

0 commit comments

Comments
 (0)