Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,9 @@ public enum Result
/// <summary>TypeEqual is not supported </summary>
NotSupported_TypeEqual = 1009,
/// <summary>`when` in catch is not supported yet</summary>
NotSupported_ExceptionCatchFilter = 1010
NotSupported_ExceptionCatchFilter = 1010,
/// <summary>A managed reference or byref-like value cannot escape into closure storage.</summary>
NotSupported_HoistedVariable = 1012
}

/// <summary>Return value is ignored</summary>
Expand Down Expand Up @@ -1423,6 +1425,13 @@ public static Result TryCollectInfo(ref CompilerContext context, Expression expr
{
if (nestedLambda == null) // means that we are in the root lambda
return Result.ParameterIsNotVariableNorInPassedParameters;
var parameterType = parExpr.Type;
if (parExpr.IsByRef || parameterType.IsByRef || parameterType.IsPointer
#if !NETFRAMEWORK
|| parameterType.IsByRefLike
#endif
)
return Result.NotSupported_HoistedVariable;
context.Status |= ClosureStatus.HasClosure;
_ = nestedLambda.NonPassedParameters.GetIndexOrAdd(parExpr, default(RefEq<ParameterExpression>));
}
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner.Net472/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ static void RunLightExpressionTests(object state)
t.Run(new LightExpression.UnitTests.IntegerSwitchDispatchRegressionTests());
t.Run(new LightExpression.UnitTests.ParityTests());
t.Run(new LightExpression.UnitTests.CatchEntryStackTests());
t.Run(new LightExpression.UnitTests.UnrepresentableCaptureTests());

Console.WriteLine($"Just LightExpression tests are passing in {justLightTestsStopwatch.ElapsedMilliseconds} ms.");
}
Expand Down Expand Up @@ -229,6 +230,7 @@ static void RunLightExpressionTests(object state)
fecTests.Run(new IntegerSwitchDispatchRegressionTests());
fecTests.Run(new ParityTests());
fecTests.Run(new CatchEntryStackTests());
fecTests.Run(new UnrepresentableCaptureTests());


Console.WriteLine($"FEC tests are passing in {fecTestsStopwatch.ElapsedMilliseconds} ms.");
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public static void Main(string[] args)
lt.Run(new LightExpression.UnitTests.IntegerSwitchDispatchRegressionTests());
lt.Run(new LightExpression.UnitTests.ParityTests());
lt.Run(new LightExpression.UnitTests.CatchEntryStackTests());
lt.Run(new LightExpression.UnitTests.UnrepresentableCaptureTests());
ltTime = ltStopwatch.ElapsedMilliseconds;
});

Expand Down Expand Up @@ -248,6 +249,7 @@ public static void Main(string[] args)
st.Run(new IntegerSwitchDispatchRegressionTests());
st.Run(new ParityTests());
st.Run(new CatchEntryStackTests());
st.Run(new UnrepresentableCaptureTests());
st.Run(new Issue498_InvalidProgramException_when_using_loop());
st.Run(new Issue495_Incomplete_pattern_detection_for_NotSupported_1007_Return_goto_from_TryCatch_with_Assign_generates_invalid_IL());
st.Run(new Issue480_CLR_detected_an_invalid_program_exception());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using S = System.Linq.Expressions.Expression;
#if LIGHT_EXPRESSION
namespace FastExpressionCompiler.LightExpression.UnitTests;
#else
namespace FastExpressionCompiler.UnitTests;
#endif

public struct UnrepresentableCaptureTests : ITestX
{
public delegate Func<int> RefFactory(ref int value);
#if NET8_0_OR_GREATER
public delegate Func<int> SpanFactory(Span<int> value);
#endif
public void Run(TestRun t) { Escaping_ref_captures_are_rejected(t); Inlined_ref_access_is_still_supported(t);
#if NET8_0_OR_GREATER
Escaping_byref_like_captures_are_rejected(t);
#endif
}
public void Escaping_ref_captures_are_rejected(TestContext t)
{
var value=S.Parameter(typeof(int).MakeByRefType());var tree=S.Lambda<RefFactory>(S.Lambda<Func<int>>(value),value);
t.Throws<Exception>(()=>tree.Compile());
foreach(var flags in new[]{CompilerFlags.Default,CompilerFlags.NoInvocationLambdaInlining,CompilerFlags.EnableDelegateDebugInfo})
t.AreEqual(null,tree.FromSysExpression().CompileFast(true,flags));
t.Throws<NotSupportedExpressionException>(()=>tree.FromSysExpression().CompileFast(true,CompilerFlags.ThrowOnNotSupportedExpression));
}
public delegate int Read(ref int value);
public void Inlined_ref_access_is_still_supported(TestContext t)
{
var value=S.Parameter(typeof(int).MakeByRefType());var tree=S.Lambda<Read>(S.Invoke(S.Lambda<Func<int>>(value)),value);
var fast=tree.FromSysExpression().CompileFast(true);if(!t.IsNotNull(fast))return;var n=7;t.AreEqual(tree.Compile()(ref n),fast(ref n));
}
#if NET8_0_OR_GREATER
public void Escaping_byref_like_captures_are_rejected(TestContext t)
{
var value=S.Parameter(typeof(Span<int>));var tree=S.Lambda<SpanFactory>(S.Lambda<Func<int>>(S.Property(value,nameof(Span<int>.Length))),value);
t.Throws<Exception>(()=>tree.Compile());t.AreEqual(null,tree.FromSysExpression().CompileFast(true));
t.Throws<NotSupportedExpressionException>(()=>tree.FromSysExpression().CompileFast(true,CompilerFlags.ThrowOnNotSupportedExpression));
}
#endif
}
Loading