Fix C# output: add _ = discard for non-void calls in if/else block bodies#504
Fix C# output: add _ = discard for non-void calls in if/else block bodies#504
_ = discard for non-void calls in if/else block bodies#504Conversation
…in if-block bodies Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/29dfe7a7-ddfe-48ea-be43-eafbc23b9e5c Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
_ = discard for non-void calls in if/else block bodies
dadhi
left a comment
There was a problem hiding this comment.
Did you run all tests in debug mode and analyzed PrintCSharp output
to find if thid fix introduced any regressions?
Yes — ran all 1655 tests via
No unexpected regressions in the output. |
| var nodeType = expr?.NodeType ?? ExpressionType.Default; | ||
| var needsDiscard = expr != null && expr.Type != typeof(void) | ||
| && (nodeType == ExpressionType.Call | nodeType == ExpressionType.Invoke | ||
| | nodeType == ExpressionType.Conditional | nodeType == ExpressionType.Coalesce); |
There was a problem hiding this comment.
Those checks for nodeTyñe are in at least 2 places, and may be more accross c# output, can we sematicalmy compress/unify them to method to simply code and rrmove creep?
There was a problem hiding this comment.
Extracted the repeated four-node-type check into NeedsDiscardWhenUsedAsStatement(this Expression expr) (line 8241), placed alongside the other ExpressionType extension helpers. Both ToCSharpBlock and BlockToCSharpString now call this single method. Commit: f973bd3, all 1655 tests still pass.
…eType checks Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/300f0816-a25b-4415-9b93-9e8044d8b0b0 Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
| else | ||
| { | ||
| sb.NewLineIndent(lineIndent + indentSpaces); | ||
| var needsDiscard = expr != null && expr.NeedsDiscardWhenUsedAsStatement(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
…ndant comments Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/efa537b8-0c0e-4172-9ba3-1401feacbf6a Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
Non-void
Call/Invokeexpressions used as statements insideIfThen/IfThenElseblocks were printed without a discard assignment, producing misleading (though technically valid) C# output.Changes
ToCSharpBlock: When the body of anif/elseblock is a non-voidCall,Invoke,Conditional, orCoalesceexpression, prefix it with_ =to explicitly discard the return value.BlockToCSharpString: Extends the existingConditional/Coalescediscard check to also coverCallandInvokenode types in block statement position.Equal_in_void_Handler_should_workto verify the output contains_ = ((int?)null).ToString();.Before / After