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
30 changes: 24 additions & 6 deletions src/ExpressiveSharp/Transformers/ConvertLoopsToLinq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ private bool TryMatchMinMaxPattern(
var method = typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static)
.First(m => m.Name == linqMethodName && m.IsGenericMethodDefinition
&& m.GetParameters().Length == 2
&& m.GetGenericArguments().Length == 2);
&& m.GetGenericArguments().Length == 2
&& m.GetParameters()[1].ParameterType.GetGenericArguments().Length == 2);
result = Expression.Call(
method.MakeGenericMethod(elementType, selectorBody.Type),
collection,
Expand Down Expand Up @@ -461,7 +462,8 @@ private bool TryMatchSelectPattern(
var selectMethod = typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static)
.First(m => m.Name == "Select" && m.IsGenericMethodDefinition
&& m.GetParameters().Length == 2
&& m.GetGenericArguments().Length == 2);
&& m.GetGenericArguments().Length == 2
&& m.GetParameters()[1].ParameterType.GetGenericArguments().Length == 2);
var selectCall = Expression.Call(
selectMethod.MakeGenericMethod(elementType, resultElementType),
collection,
Expand Down Expand Up @@ -504,7 +506,8 @@ private bool TryMatchConditionalSelect(
var selectMethod = typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static)
.First(m => m.Name == "Select" && m.IsGenericMethodDefinition
&& m.GetParameters().Length == 2
&& m.GetGenericArguments().Length == 2);
&& m.GetGenericArguments().Length == 2
&& m.GetParameters()[1].ParameterType.GetGenericArguments().Length == 2);
var selectCall = Expression.Call(
selectMethod.MakeGenericMethod(elementType, resultElementType),
whereCall,
Expand Down Expand Up @@ -537,9 +540,23 @@ private static bool IsConstant(Expression expr, object? value)

private static bool IsNumericZero(Expression expr)
{
if (expr is ConstantExpression constant && constant.Value is not null)
if (expr is ConstantExpression { Value: not null } constant)
{
return Convert.ToDouble(constant.Value) == 0.0;
return constant.Value switch
{
byte v => v == 0,
sbyte v => v == 0,
short v => v == 0,
ushort v => v == 0,
int v => v == 0,
uint v => v == 0,
long v => v == 0,
ulong v => v == 0,
float v => v == 0f,
double v => v == 0d,
decimal v => v == 0m,
_ => false,
};
}

return expr is DefaultExpression;
Expand Down Expand Up @@ -568,7 +585,8 @@ private static MethodInfo GetEnumerableMethod(string name, Type elementType, boo
{
// Generic overload with predicate/selector (e.g., Count<T>(IEnumerable<T>, Func<T, bool>))
var method = typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static)
.First(m => m.Name == name && m.IsGenericMethodDefinition && m.GetParameters().Length == 2);
.First(m => m.Name == name && m.IsGenericMethodDefinition && m.GetParameters().Length == 2
&& m.GetParameters()[1].ParameterType.GetGenericArguments().Length == 2);
return method.MakeGenericMethod(elementType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,35 @@ public void ForEach_Min_ExecutesCorrectly()
var result = compiled(new List<int> { 5, 2, 8, 1, 9 });
Assert.AreEqual(1, result);
}

[TestMethod]
public void ForEach_StringAccumulation_ThrowsInvalidOperationException()
{
var items = Expression.Parameter(typeof(List<string>), "items");
var acc = Expression.Variable(typeof(string), "s");
var concat = typeof(string).GetMethod(nameof(string.Concat), new[] { typeof(string), typeof(string) })!;

var loopExpr = BuildForEachLoop(items, acc, Expression.Constant(""),
typeof(string),
(iter, a) => Expression.Assign(a, Expression.Add(a, iter, concat)));

Assert.ThrowsExactly<InvalidOperationException>(() => _sut.Transform(loopExpr));
}

[TestMethod]
public void EnumerableOverloadPredicates_MatchExactlyOneMethod()
{
var whereMatches = typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static)
.Count(m => m.Name == "Where" && m.IsGenericMethodDefinition && m.GetParameters().Length == 2
&& m.GetParameters()[1].ParameterType.GetGenericArguments().Length == 2);

var selectMatches = typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static)
.Count(m => m.Name == "Select" && m.IsGenericMethodDefinition
&& m.GetParameters().Length == 2
&& m.GetGenericArguments().Length == 2
&& m.GetParameters()[1].ParameterType.GetGenericArguments().Length == 2);

Assert.AreEqual(1, whereMatches);
Assert.AreEqual(1, selectMatches);
}
}
Loading