Skip to content

Commit

Permalink
fixing the emit of the in struct parameter constant, #316
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Feb 1, 2022
1 parent b0f6d74 commit 5508da9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,7 @@ public static bool TryEmit(Expression expr, IReadOnlyList<PE> paramExprs,
}

return TryEmitConstantOfNotNullValue(closure.ContainsConstantsOrNestedLambdas(),
constExpr.Type, constExpr.Value, il, ref closure);
constExpr.Type, constExpr.Value, il, ref closure, byRefIndex);

case ExpressionType.Call:
return TryEmitMethodCall(expr, paramExprs, il, ref closure, setup, parent);
Expand Down Expand Up @@ -2989,7 +2989,7 @@ private static bool TryEmitValueConvert(Type targetType, ILGenerator il, bool is
}

private static bool TryEmitConstantOfNotNullValue(
bool considerClosure, Type exprType, object constantValue, ILGenerator il, ref ClosureInfo closure)
bool considerClosure, Type exprType, object constantValue, ILGenerator il, ref ClosureInfo closure, int byRefIndex = -1)
{
var constValueType = constantValue.GetType();
if (considerClosure && IsClosureBoundConstant(constantValue, constValueType))
Expand All @@ -3010,7 +3010,11 @@ private static bool TryEmitConstantOfNotNullValue(
EmitLoadConstantInt(il, constIndex);
il.Emit(OpCodes.Ldelem_Ref);
if (exprType.IsValueType)
{
il.Emit(OpCodes.Unbox_Any, exprType);
if (byRefIndex != -1)
EmitStoreAndLoadLocalVariableAddress(il, exprType);
}
else
{
// this is probably required only for Full CLR starting from NET45, e.g. `Test_283_Case6_MappingSchemaTests_CultureInfo_VerificationException`
Expand Down
27 changes: 24 additions & 3 deletions test/FastExpressionCompiler.IssueTests/Issue316_in_parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class Issue316_in_parameter : ITest
{
public int Run()
{
Test_in_struct_parameter();
Test_constructor_in_struct_parameter_constant();
Test_method_in_struct_parameter_constant();
return 1;
}

Expand All @@ -36,8 +37,8 @@ public struct TextPosition
public int Position;
}

//[Test]
public void Test_in_struct_parameter()
[Test]
public void Test_constructor_in_struct_parameter_constant()
{
var position = new TextPosition { Position = 42 };
var program = Throw(New(typeof(ParseException).GetConstructors()[0], Constant("314"), Constant(position)));
Expand All @@ -53,5 +54,25 @@ public void Test_in_struct_parameter()
fFast.PrintIL("fast");
Assert.Throws<ParseException>(() => fFast());
}

public static void ThrowParseException(in TextPosition position) => throw new ParseException("from method", in position);

[Test]
public void Test_method_in_struct_parameter_constant()
{
var position = new TextPosition { Position = 42 };
var program = Call(GetType().GetMethod(nameof(ThrowParseException)), Constant(position));

var expr = Expression.Lambda<Action>(program);
expr.PrintCSharp(s => s.Replace(GetType().Name + ".", ""));

var fSys = expr.CompileSys();
fSys.PrintIL("sys");
Assert.Throws<ParseException>(() => fSys());

var fFast = expr.CompileFast();
fFast.PrintIL("fast");
Assert.Throws<ParseException>(() => fFast());
}
}
}

0 comments on commit 5508da9

Please sign in to comment.