Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8235,6 +8235,17 @@ internal static bool IsBlockLikeOrConditional(this ExpressionType nodeType) =>
nodeType == ExpressionType.Conditional | nodeType == ExpressionType.Coalesce ||
IsBracedBlockLike(nodeType);

// Returns true when a non-void expression used as a statement needs a `_ = ` discard prefix
// to suppress CS0201 / produce valid, unambiguous C# output.
[MethodImpl((MethodImplOptions)256)]
internal static bool NeedsDiscardWhenUsedAsStatement(this Expression expr)
{
var nodeType = expr.NodeType;
return expr.Type != typeof(void)
&& (nodeType == ExpressionType.Call | nodeType == ExpressionType.Invoke
| nodeType == ExpressionType.Conditional | nodeType == ExpressionType.Coalesce);
}

[MethodImpl((MethodImplOptions)256)]
internal static bool IsReturnable(this Expression expr)
{
Expand Down Expand Up @@ -10927,6 +10938,9 @@ private static StringBuilder ToCSharpBlock(this Expression expr, StringBuilder s
else
{
sb.NewLineIndent(lineIndent + indentSpaces);
var needsDiscard = expr != null && expr.NeedsDiscardWhenUsedAsStatement();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Does it need the local var to store bool check used in a single condition?
Does it needs the comment almost repearing the check method nane?
The same for other check - plesse eliminate

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 4a54b45 — inlined the single-use needsDiscard variable in ToCSharpBlock and removed the redundant comments at both call sites. The returningCondOrCoalesceOrCall variable in BlockToCSharpString is kept since it's referenced twice (in the if and in the ternary on the next block).

if (needsDiscard) // it requires some assignment target to avoid error or warning
sb.Append("_ = ");
sb = expr?.ToCSharpString(sb, EnclosedIn.ParensByDefault, ref ctx,
lineIndent + indentSpaces, stripNamespace, printType, indentSpaces, notRecognizedToCode) ?? sb.Append("null");
sb.AppendSemicolonOnce(expr);
Expand Down Expand Up @@ -11149,9 +11163,8 @@ private static StringBuilder BlockToCSharpString(this BlockExpression b, StringB
{
sb.NewLineIndent(lineIndent);
var nodeType = expr.NodeType;
var returningCondOrCoalesce = expr.Type != typeof(void)
&& nodeType == ExpressionType.Conditional | nodeType == ExpressionType.Coalesce;
if (returningCondOrCoalesce) // it requires some assignment target to avoid error or warning
var returningCondOrCoalesceOrCall = expr.NeedsDiscardWhenUsedAsStatement();
if (returningCondOrCoalesceOrCall) // it requires some assignment target to avoid error or warning
sb.Append("_ = ");

expr.ToCSharpString(sb, EnclosedIn.Block, ref ctx,
Expand All @@ -11160,7 +11173,7 @@ private static StringBuilder BlockToCSharpString(this BlockExpression b, StringB
// Preventing the `};` kind of situation and separating the conditional block with empty line
if (nodeType.IsBlockLikeOrConditional())
{
sb = returningCondOrCoalesce ? sb.AppendSemicolonOnce() : sb;
sb = returningCondOrCoalesceOrCall ? sb.AppendSemicolonOnce() : sb;
sb.NewLineIndent(lineIndent);
}
else if (nodeType != ExpressionType.Label & nodeType != ExpressionType.Default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public void Equal_in_void_Handler_should_work()
var expr = Lambda<Handler>(callIfNotNull, parameterExpr);

expr.PrintCSharp();
Asserts.Contains("_ = ((int?)null).ToString();", expr.ToCSharpString());

var fs = expr.CompileSys();
fs.PrintIL();

Expand Down
Loading