From 3ef82a91e4a79b1b179211b79afe0ce2eeab7a0a Mon Sep 17 00:00:00 2001 From: sakno Date: Sat, 17 Feb 2024 16:03:32 +0200 Subject: [PATCH] Fixed exception throw on sync completion of async lambda --- .../Linq/Expressions/AsyncResultExpression.cs | 4 ++++ .../Runtime/CompilerServices/AsyncStateMachineBuilder.cs | 4 +--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/DotNext.Metaprogramming/Linq/Expressions/AsyncResultExpression.cs b/src/DotNext.Metaprogramming/Linq/Expressions/AsyncResultExpression.cs index dbbe3eb99..0a1ffb1bb 100644 --- a/src/DotNext.Metaprogramming/Linq/Expressions/AsyncResultExpression.cs +++ b/src/DotNext.Metaprogramming/Linq/Expressions/AsyncResultExpression.cs @@ -60,6 +60,10 @@ public AsyncResultExpression(bool valueTask) /// public override Type Type => taskType; + // indicates that AsyncResult cannot throw exception so it can be inlined + internal bool IsSimpleResult + => AsyncResult is ConstantExpression or ParameterExpression or DefaultExpression; + /// /// Translates this expression into predefined set of expressions /// using Lowering technique. diff --git a/src/DotNext.Metaprogramming/Runtime/CompilerServices/AsyncStateMachineBuilder.cs b/src/DotNext.Metaprogramming/Runtime/CompilerServices/AsyncStateMachineBuilder.cs index 83dd8e1d8..c3c7510e7 100644 --- a/src/DotNext.Metaprogramming/Runtime/CompilerServices/AsyncStateMachineBuilder.cs +++ b/src/DotNext.Metaprogramming/Runtime/CompilerServices/AsyncStateMachineBuilder.cs @@ -245,9 +245,7 @@ private Expression VisitAsyncResult(AsyncResultExpression expr) var prologue = context.CurrentStatement.PrologueCodeInserter(); expr = (AsyncResultExpression)base.VisitExtension(expr); - var containsAwait = ExpressionAttributes.Get(expr.AsyncResult) is { ContainsAwait: true }; - - if (containsAwait && Task.HasResult) + if (Task.HasResult && expr.IsSimpleResult is false) { ResultVariable ??= Expression.Parameter(Task.ResultType); prologue(Expression.Assign(ResultVariable, expr.AsyncResult));