Repository navigation
Expand file tree
/
Copy pathIssue561_NonBooleanEqualityOperator.cs
More file actions
281 lines (254 loc) · 13.9 KB
/
Copy pathIssue561_NonBooleanEqualityOperator.cs
File metadata and controls
281 lines (254 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// The safety guard uses the dynamic-method decoder supported on modern .NET.
// The fatal repro itself is also exercised in a separate child-process harness.
#if NET8_0_OR_GREATER
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
#if LIGHT_EXPRESSION
using FastExpressionCompiler.LightExpression.ILDecoder;
using FastExpressionCompiler.LightExpression.ImTools;
using static FastExpressionCompiler.LightExpression.ExpressionCompiler;
namespace FastExpressionCompiler.LightExpression.IssueTests;
#else
using FastExpressionCompiler.ILDecoder;
namespace FastExpressionCompiler.IssueTests;
#endif
public struct Issue561_NonBooleanEqualityOperator : ITestX
{
public void Run(TestRun t)
{
Equal_returns_the_interface_from_the_selected_operator(t);
NotEqual_returns_the_interface_from_the_selected_operator(t);
Reversed_equal_returns_the_interface_from_the_selected_operator(t);
Reversed_not_equal_returns_the_interface_from_the_selected_operator(t);
Ignored_result_still_calls_the_operator(t);
Operands_execute_once_in_order_with_nested_try(t);
Non_lifted_nullable_parameters_reach_the_selected_method(t);
Selected_methods_keep_non_boolean_value_results(t);
Boolean_method_is_not_replaced_by_constant_comparison(t);
Converted_simple_right_operands_reach_the_selected_method(t);
#if LIGHT_EXPRESSION
Converted_intrinsic_right_operand_can_emit_exception_regions(t);
#endif
}
public interface ICustomObject { void Ping(); }
private static string Trace;
public sealed class CustomObject : ICustomObject
{
public static int OperatorCalls;
public static int PingCalls;
public static ICustomObject operator ==(double left, CustomObject right) => Result();
public static ICustomObject operator !=(double left, CustomObject right) => Result();
public static ICustomObject operator ==(CustomObject left, double right) => Result();
public static ICustomObject operator !=(CustomObject left, double right) => Result();
private static ICustomObject Result() { ++OperatorCalls; Trace += "O"; return new CustomObject(); }
public void Ping() { ++PingCalls; }
public override bool Equals(object obj) => ReferenceEquals(this, obj);
public override int GetHashCode() => base.GetHashCode();
}
public void Equal_returns_the_interface_from_the_selected_operator(TestContext t) =>
Check(t, () => 1 == new CustomObject());
public void NotEqual_returns_the_interface_from_the_selected_operator(TestContext t) =>
Check(t, () => 1 != new CustomObject());
public void Reversed_equal_returns_the_interface_from_the_selected_operator(TestContext t) =>
Check(t, () => new CustomObject() == 1);
public void Reversed_not_equal_returns_the_interface_from_the_selected_operator(TestContext t) =>
Check(t, () => new CustomObject() != 1);
private static void Check(TestContext t, System.Linq.Expressions.Expression<Func<ICustomObject>> expression)
{
var binary = (System.Linq.Expressions.BinaryExpression)expression.Body;
t.AreEqual(typeof(ICustomObject), binary.Type);
t.IsNotNull(binary.Method);
t.IsTrue(!binary.IsLifted);
CustomObject.OperatorCalls = CustomObject.PingCalls = 0;
var normal = expression.Compile()();
t.AreEqual(1, CustomObject.OperatorCalls);
normal.Ping();
t.AreEqual(1, CustomObject.PingCalls);
foreach (var flags in new[] { CompilerFlags.Default, CompilerFlags.NoInvocationLambdaInlining,
CompilerFlags.EnableDelegateDebugInfo })
{
var fast = Compile(expression, flags);
if (!t.IsNotNull(fast)) return;
if (!CheckSelectedCall(t, fast, binary.Method)) return;
CustomObject.OperatorCalls = CustomObject.PingCalls = 0;
var result = fast();
t.AreEqual(1, CustomObject.OperatorCalls);
if (!t.IsNotNull(result)) return;
t.IsTrue(result is CustomObject);
result.Ping();
t.AreEqual(1, CustomObject.PingCalls);
}
}
private static TDelegate Compile<TDelegate>(System.Linq.Expressions.Expression<TDelegate> expression,
CompilerFlags flags = CompilerFlags.Default) where TDelegate : Delegate =>
#if LIGHT_EXPRESSION
expression.ToLightExpression().CompileFast(true, flags);
#else
expression.CompileFast(true, flags);
#endif
private static bool CheckSelectedCall(TestContext t, Delegate fast, MethodInfo method)
{
// On affected compilers the returned reference is corrupt. Check the
// selected call before invocation so this regression fails without
// taking down the whole runner. This does not assert an IL layout.
var instructions = fast.TryGetDebugInfo()?.ILInstructions ?? fast.Method.ReadAllInstructions();
if (instructions.OfType<InlineMethodInstruction>().Any(i => i.Method.Equals(method))) return true;
t.Fail("Issue561: selected operator call is missing; corrupt-reference delegate was not invoked.");
return false;
}
public void Ignored_result_still_calls_the_operator(TestContext t)
{
System.Linq.Expressions.Expression<Func<ICustomObject>> original = () => 1 == new CustomObject();
var method = ((System.Linq.Expressions.BinaryExpression)original.Body).Method;
var expression = System.Linq.Expressions.Expression.Lambda<Action>(
System.Linq.Expressions.Expression.Block(original.Body, System.Linq.Expressions.Expression.Empty()));
var fast = Compile(expression);
if (!t.IsNotNull(fast) || !CheckSelectedCall(t, fast, method)) return;
CustomObject.OperatorCalls = 0;
fast();
t.AreEqual(1, CustomObject.OperatorCalls);
}
public static double ReadLeft() { Trace += "L"; return 1; }
public static int ReadArgument() { Trace += "A"; return 2; }
public static void FinishArgument() { Trace += "F"; }
public static CustomObject ReadRight(int value) { Trace += "R"; return new CustomObject(); }
public void Operands_execute_once_in_order_with_nested_try(TestContext t)
{
var owner = typeof(Issue561_NonBooleanEqualityOperator);
var left = System.Linq.Expressions.Expression.Call(owner.GetMethod(nameof(ReadLeft)));
var right = System.Linq.Expressions.Expression.Call(owner.GetMethod(nameof(ReadRight)),
System.Linq.Expressions.Expression.TryFinally(
System.Linq.Expressions.Expression.Call(owner.GetMethod(nameof(ReadArgument))),
System.Linq.Expressions.Expression.Call(owner.GetMethod(nameof(FinishArgument)))));
var body = System.Linq.Expressions.Expression.Equal(left, right);
var expression = System.Linq.Expressions.Expression.Lambda<Func<ICustomObject>>(body);
var system = expression.Compile();
Trace = ""; system().Ping();
var expected = Trace;
t.AreEqual("LAFRO", expected);
var fast = Compile(expression);
if (!t.IsNotNull(fast) || !CheckSelectedCall(t, fast, body.Method)) return;
Trace = ""; CustomObject.OperatorCalls = CustomObject.PingCalls = 0;
fast().Ping();
t.AreEqual(expected, Trace);
t.AreEqual(1, CustomObject.OperatorCalls);
t.AreEqual(1, CustomObject.PingCalls);
}
public static ICustomObject CompareNullable(double? left, CustomObject right)
{
Trace += left.HasValue ? "value" : "null";
++CustomObject.OperatorCalls;
return new CustomObject();
}
public void Non_lifted_nullable_parameters_reach_the_selected_method(TestContext t)
{
var left = System.Linq.Expressions.Expression.Parameter(typeof(double?));
var body = System.Linq.Expressions.Expression.Equal(left,
System.Linq.Expressions.Expression.New(typeof(CustomObject)), false,
typeof(Issue561_NonBooleanEqualityOperator).GetMethod(nameof(CompareNullable)));
t.IsTrue(!body.IsLifted);
var expression = System.Linq.Expressions.Expression.Lambda<Func<double?, ICustomObject>>(body, left);
var system = expression.Compile();
var fast = Compile(expression);
if (!t.IsNotNull(fast) || !CheckSelectedCall(t, fast, body.Method)) return;
foreach (var value in new double?[] { null, 2 })
{
Trace = ""; system(value).Ping(); var expected = Trace;
Trace = ""; CustomObject.OperatorCalls = CustomObject.PingCalls = 0;
fast(value).Ping();
t.AreEqual(expected, Trace);
t.AreEqual(1, CustomObject.OperatorCalls);
t.AreEqual(1, CustomObject.PingCalls);
}
}
public static int CompareValue(int left, int right) { ++CustomObject.OperatorCalls; return left * 10 + right; }
public static bool CompareBoolean(int left, int right) { ++CustomObject.OperatorCalls; return left != right; }
public void Selected_methods_keep_non_boolean_value_results(TestContext t)
{
var left = System.Linq.Expressions.Expression.Parameter(typeof(int));
var method = typeof(Issue561_NonBooleanEqualityOperator).GetMethod(nameof(CompareValue));
foreach (var kind in new[] { ExpressionType.Equal, ExpressionType.NotEqual, ExpressionType.LessThan,
ExpressionType.LessThanOrEqual, ExpressionType.GreaterThan, ExpressionType.GreaterThanOrEqual })
{
var body = System.Linq.Expressions.Expression.MakeBinary(kind, left,
System.Linq.Expressions.Expression.Constant(2), false, method);
var expression = System.Linq.Expressions.Expression.Lambda<Func<int, int>>(body, left);
var fast = Compile(expression);
if (!t.IsNotNull(fast) || !CheckSelectedCall(t, fast, method)) return;
var expected = expression.Compile()(3);
CustomObject.OperatorCalls = 0;
t.AreEqual(expected, fast(3));
t.AreEqual(1, CustomObject.OperatorCalls);
}
}
public void Boolean_method_is_not_replaced_by_constant_comparison(TestContext t)
{
var method = typeof(Issue561_NonBooleanEqualityOperator).GetMethod(nameof(CompareBoolean));
var body = System.Linq.Expressions.Expression.Equal(System.Linq.Expressions.Expression.Constant(1),
System.Linq.Expressions.Expression.Constant(2), false, method);
var expression = System.Linq.Expressions.Expression.Lambda<Func<bool>>(body);
var fast = Compile(expression);
if (!t.IsNotNull(fast) || !CheckSelectedCall(t, fast, method)) return;
var expected = expression.Compile()();
CustomObject.OperatorCalls = 0;
t.AreEqual(expected, fast());
t.AreEqual(1, CustomObject.OperatorCalls);
}
public void Converted_simple_right_operands_reach_the_selected_method(TestContext t)
{
var parameter = System.Linq.Expressions.Expression.Parameter(typeof(int));
var method = typeof(Issue561_NonBooleanEqualityOperator).GetMethod(nameof(CompareValue));
foreach (var right in new System.Linq.Expressions.Expression[] { parameter,
System.Linq.Expressions.Expression.Constant(2), System.Linq.Expressions.Expression.Default(typeof(int)) })
{
var converted = System.Linq.Expressions.Expression.Convert(
System.Linq.Expressions.Expression.Convert(right, typeof(long)), typeof(int));
var body = System.Linq.Expressions.Expression.Equal(
System.Linq.Expressions.Expression.Constant(3), converted, false, method);
var expression = System.Linq.Expressions.Expression.Lambda<Func<int, int>>(body, parameter);
var fast = Compile(expression);
if (!t.IsNotNull(fast) || !CheckSelectedCall(t, fast, method)) return;
var expected = expression.Compile()(4);
CustomObject.OperatorCalls = 0;
t.AreEqual(expected, fast(4));
t.AreEqual(1, CustomObject.OperatorCalls);
}
}
#if LIGHT_EXPRESSION
private sealed class TryFinallyConversion : UnaryExpression
{
private readonly Expression _body;
public TryFinallyConversion(Expression operand, Expression body) : base(operand) => _body = body;
public override ExpressionType NodeType => ExpressionType.Convert;
public override bool IsIntrinsic => true;
public override Result TryCollectInfo(ref CompilerContext context, NestedLambdaInfo nestedLambda,
ref SmallList<NestedLambdaInfo> rootNestedLambdas) =>
ExpressionCompiler.TryCollectInfo(ref context, _body, nestedLambda, ref rootNestedLambdas);
public override bool TryEmit(ref CompilerContext context, System.Reflection.Emit.ILGenerator il,
ParentFlags parent, int byRefIndex = -1) =>
EmittingVisitor.TryEmit(_body, il, ref context, parent, byRefIndex);
}
public void Converted_intrinsic_right_operand_can_emit_exception_regions(TestContext t)
{
var owner = typeof(Issue561_NonBooleanEqualityOperator);
var parameter = Expression.Parameter(typeof(int));
var read = Expression.Call(owner.GetMethod(nameof(ReadArgument)));
var intrinsic = new TryFinallyConversion(parameter,
Expression.TryFinally(read, Expression.Call(owner.GetMethod(nameof(FinishArgument)))));
// The intrinsic is hidden inside two ordinary conversion wrappers.
var right = Expression.Convert(Expression.Convert(intrinsic, typeof(long)), typeof(int));
var method = owner.GetMethod(nameof(CompareValue));
var body = Expression.Equal(read, right, false, method);
var fast = Expression.Lambda<Func<int, int>>(body, parameter).CompileFast(true);
if (!t.IsNotNull(fast) || !CheckSelectedCall(t, fast, method)) return;
Trace = "";
CustomObject.OperatorCalls = 0;
t.AreEqual(22, fast(4));
t.AreEqual("AAF", Trace);
t.AreEqual(1, CustomObject.OperatorCalls);
}
#endif
}
#endif