Skip to content

Commit 7fc90c0

Browse files
committed
some fixes and better DiagInfo aware PrintIL
1 parent 6d61ec7 commit 7fc90c0

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

src/FastExpressionCompiler/ILReader.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ public static ILReader GetILReaderOrNull(MethodBase source)
6262
return null;
6363
}
6464

65-
public static StringBuilder ToILString(this MethodInfo method, StringBuilder s = null) => ToILString(GetILReaderOrNull(method), s);
65+
public static StringBuilder ToILString(this MethodInfo method, StringBuilder s = null)
66+
{
67+
var il = GetILReaderOrNull(method);
68+
return il != null
69+
? il.ToILString(s)
70+
: (s ?? new StringBuilder()).AppendLine($"ILReader for {method} is not supported");
71+
}
6672

6773
public static StringBuilder ToILString(this IEnumerable<ILInstruction> ilInstructions, StringBuilder s = null)
6874
{

src/FastExpressionCompiler/TestTools.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,28 +134,27 @@ public static void PrintCSharp(this Expression expr, ref string result)
134134
Console.WriteLine(result = expr.ToCSharpString());
135135
}
136136

137-
public static void PrintIL(this Delegate @delegate, [CallerMemberName] string tag = null)
137+
public static void PrintIL(this Delegate dlg, [CallerMemberName] string tag = null)
138138
{
139139
if (!AllowPrintIL) return;
140-
@delegate.Method.PrintIL(tag);
140+
if (dlg.TryGetDebugInfo() is { } diagInfo)
141+
diagInfo.PrintIL(tag);
142+
else
143+
dlg.Method.PrintIL(tag);
141144
}
142145

143-
public static void PrintIL(this MethodInfo method, string tag = null)
144-
{
145-
if (!AllowPrintIL) return;
146-
var s = new StringBuilder();
147-
s.Append(tag == null ? "<il>" : "<" + tag + ">").AppendLine();
148-
method.ToILString(s);
149-
s.AppendLine().Append(tag == null ? "</il>" : "</" + tag + ">");
150-
Console.WriteLine(s);
151-
}
146+
public static void PrintIL(this MethodInfo method, [CallerMemberName] string tag = null) =>
147+
PrintIL(tag, method, static (m, s) => m.ToILString(s));
148+
149+
public static void PrintIL(this IDelegateDebugInfo diagInfo, [CallerMemberName] string tag = null) =>
150+
PrintIL(tag, diagInfo, static (di, s) => s.Append(di.ILString));
152151

153-
public static void PrintIL(this IDelegateDebugInfo delegateDebugInfo, string tag = null)
152+
private static void PrintIL<A>(string tag, A state, Action<A, StringBuilder> printIL)
154153
{
155154
if (!AllowPrintIL) return;
156155
var s = new StringBuilder();
157156
s.Append(tag == null ? "<il>" : "<" + tag + ">").AppendLine();
158-
s.Append(delegateDebugInfo.ILString);
157+
printIL(state, s);
159158
s.AppendLine().Append(tag == null ? "</il>" : "</" + tag + ">");
160159
Console.WriteLine(s);
161160
}
@@ -475,8 +474,7 @@ public static bool IsInstanceOf<T>(object actual,
475474
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
476475
Justification = "The method is used for the testing purposes only.")]
477476
public static E Throws<E>(Action action,
478-
[CallerArgumentExpression(nameof(action))]
479-
string actionName = "<action to throw>")
477+
[CallerArgumentExpression(nameof(action))] string actionName = "<action to throw>")
480478
where E : Exception
481479
{
482480
try

test/FastExpressionCompiler.IssueTests/Issue316_in_parameter.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ public void Test_constructor_in_struct_parameter_constant()
6060
var position = new TextPosition { Position = 42 };
6161
var program = Throw(New(typeof(ParseException).GetConstructors()[0], Constant("314"), Constant(position)));
6262

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

66-
var fSys = expr.CompileSys();
67-
fSys.PrintIL("sys");
68-
Asserts.Throws<ParseException>(() => fSys());
66+
var fs = expr.CompileSys();
67+
fs.PrintIL("sys");
68+
Asserts.Throws<ParseException>(() => fs());
6969

70-
var fFast = expr.CompileFast();
71-
fFast.PrintIL("fast");
72-
Asserts.Throws<ParseException>(() => fFast());
70+
var ff = expr.CompileFast();
71+
ff.PrintIL("fast");
72+
Asserts.Throws<ParseException>(() => ff());
7373
}
7474

7575
#if LIGHT_EXPRESSION

test/FastExpressionCompiler.TestsRunner/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static void Main()
1414
// ILGeneratorTools.DisableILGeneratorPooling = true;
1515
// LightExpression.ILGeneratorTools.DisableILGeneratorPooling = true;
1616

17+
new IssueTests.Issue316_in_parameter().Run();
1718
new LightExpression.IssueTests.Issue55_CompileFast_crash_with_ref_parameter().Run();
1819
new LightExpression.IssueTests.Issue341_Equality_comparison_between_nullable_and_null_inside_Any_produces_incorrect_compiled_expression().Run();
1920
new LightExpression.IssueTests.Issue347_InvalidProgramException_on_compiling_an_expression_that_returns_a_record_which_implements_IList().Run();

0 commit comments

Comments
 (0)