Skip to content

Fix LightExpression compound assignment round trips without a conversion - #555

Open
dajiaohuang wants to merge 1 commit into
dadhi:masterfrom
dajiaohuang:fix/light-compound-assignment-roundtrip-20261015
Open

dajiaohuang wants to merge 1 commit into
dadhi:masterfrom
dajiaohuang:fix/light-compound-assignment-roundtrip-20261015

Conversation

@dajiaohuang

Copy link
Copy Markdown

Problem

A normal integer compound assignment can compile with CompileFast, but converting its LightExpression representation back to System.Linq.Expressions throws NullReferenceException:

using System;
using System.Linq.Expressions;
using FastExpressionCompiler.LightExpression;
using static System.Linq.Expressions.Expression;

var value = Variable(typeof(int), "value");
var source = Lambda<Func<int>>(Block(new[] { value },
    Assign(value, Constant(3)), AddAssign(value, Constant(2)), value));
var light = source.ToLightExpression();
var result = light.ToLambdaExpression().Compile()(); // expected 5; throws before this change

FromSysExpressionConverter creates an OpAssignMethodConversionBinaryExpression through MakeBinary. A compound assignment's conversion lambda is optional; CreateSysExpression unconditionally dereferences it. Preserve an absent conversion when forwarding the arguments to System MakeBinary, as the base binary-expression conversion already does.

Changes

  • Preserve the optional conversion rather than dereferencing null.
  • Add maintained round-trip tests with and without a conversion lambda, registered in LightExpressionTests.Run().

Validation

  • The no-conversion regression throws on master 1d2a4bd364bd5271f5594074f76e298f5afa53db; both regression methods pass with the fix on .NET 8.0.22 and 10.0.0.
  • A bounded synthetic consumer fixture passed 108 result checks on both runtimes (including closures, calls, control flow, assignment and System/Light conversion).
  • Release build of FastExpressionCompiler.LightExpression.UnitTests for net10.0 passed without warnings/errors.
  • BuildScripts/MakeInternal.ps1 completed successfully; generated internal copies are ignored.
  • Full upstream runner and net472/netstandard/cross-platform matrix were not run locally. No performance claim.

@dadhi dadhi added the bug label Oct 17, 2026
@dadhi dadhi added this to the v6.1.0 milestone Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants