Repository navigation
Closed
dotnet/corefx
#15318Labels
area-System.Linq.ExpressionsenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributors
Milestone
Description
Consider the following test class I put together of all instances. I will submit a PR fixing these bugs if it you guys approve, so please let me know. Here I've used RefEmit to produce the global fields/methods, as C# doesn't support them
public class GlobalMethodGlobalField
{
[Fact]
public void AndAlso_GlobalMethod_ThrowsNullReferenceException()
{
MethodInfo method = GlobalMethod(typeof(int), new Type[] { typeof(int), typeof(int) });
Assert.Throws<NullReferenceException>(() => Expression.AndAlso(Expression.Constant(1), Expression.Constant(2), method));
}
[Fact]
public void Bind_GlobalMethod_ThrowsNullReferenceException()
{
Assert.Throws<NullReferenceException>(() => Expression.Bind(GlobalMethod(typeof(int), new Type[0]), Expression.Constant("abc")));
}
[Fact]
public void Default_TypeBuilder_CreateLambda_ThrowsNotSupportedException()
{
AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Assembly"), AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("Module");
TypeBuilder typeBuilder = moduleBuilder.DefineType("Type");
DefaultExpression defaultExpression = Expression.Default(typeBuilder.AsType());
Assert.Throws<NotSupportedException>(() => Expression.Lambda(defaultExpression));
LambdaExpression genericLambda = Expression.Lambda<Func<object>>(defaultExpression);
Assert.Null(genericLambda.Compile());
}
[Fact]
public void ListBind_GlobalMethod_ThrowsNullReferenceException()
{
Assert.Throws<NullReferenceException>(() => Expression.ListBind(GlobalMethod(typeof(int), new Type[0]), new ElementInit[0]));
Assert.Throws<NullReferenceException>(() => Expression.ListBind(GlobalMethod(typeof(int), new Type[0]), (IEnumerable<ElementInit>)new ElementInit[0]));
}
[Fact]
public void MemberBind_GlobalMethod_ThrowsNullReferenceException()
{
Assert.Throws<NullReferenceException>(() => Expression.MemberBind(GlobalMethod(typeof(int), new Type[0]), new MemberBinding[0]));
Assert.Throws<NullReferenceException>(() => Expression.MemberBind(GlobalMethod(typeof(int), new Type[0]), (IEnumerable<MemberBinding>)new MemberBinding[0]));
}
[Fact]
public void MemberBind_GlobalFieldInBindings_ThrowsNullReferenceException()
{
MemberBinding[] bindings = new MemberBinding[] { Expression.MemberBind(GlobalField()) };
Assert.Throws<NullReferenceException>(() => Expression.MemberBind(GlobalField(), bindings));
Assert.Throws<NullReferenceException>(() => Expression.MemberBind(GlobalField(), (IEnumerable<MemberBinding>)bindings));
}
[Fact]
public void New_GlobalMethodInMembers_ThrowsNullReferenceException()
{
ConstructorInfo constructor = typeof(ClassWithConstructor).GetConstructor(new Type[] { typeof(int) });
Expression[] arguments = new Expression[] { Expression.Constant(5) };
MemberInfo[] members = new MemberInfo[] { GlobalMethod(typeof(int), new Type[0]) };
Assert.Throws<NullReferenceException>(() => Expression.New(constructor, arguments, members));
}
[Fact]
public void New_GlobalFieldInMembers_ThrowsNullReferenceException()
{
ConstructorInfo constructor = typeof(ClassWithConstructor).GetConstructor(new Type[] { typeof(int) });
Expression[] arguments = new Expression[] { Expression.Constant(5) };
MemberInfo[] members = new MemberInfo[] { GlobalField() };
Assert.Throws<NullReferenceException>(() => Expression.New(constructor, arguments, members));
}
[Fact]
public void OrElse_GlobalMethod_ThrowsNullReferenceException()
{
MethodInfo method = GlobalMethod(typeof(int), new Type[] { typeof(int), typeof(int) });
Assert.Throws<NullReferenceException>(() => Expression.OrElse(Expression.Constant(1), Expression.Constant(2), method));
}
private class ClassWithConstructor
{
public ClassWithConstructor(int i) { }
}
private static MethodInfo GlobalMethod(Type returnType, Type[] parameterTypes)
{
AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("ABC"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assembly.DefineDynamicModule("Module");
MethodBuilder globalMethod = module.DefineGlobalMethod("GlobalMethod", MethodAttributes.Public | MethodAttributes.Static, returnType, parameterTypes);
globalMethod.GetILGenerator().Emit(OpCodes.Ret);
module.CreateGlobalFunctions();
return module.GetMethod(globalMethod.Name);
}
private static FieldInfo GlobalField()
{
AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("ABC"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assembly.DefineDynamicModule("Module");
FieldBuilder fieldBuilder = module.DefineInitializedData("Name", new byte[1], FieldAttributes.Public);
module.CreateGlobalFunctions();
return module.GetField(fieldBuilder.Name);
}
}
Metadata
Metadata
Assignees
Labels
area-System.Linq.ExpressionsenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributors
Activity
bartdesmet commentedon Oct 24, 2016
Interesting behavior... Where does the
NullReferenceExceptionoriginate from? Is it assuming that something likeDeclaringTypeis non-null maybe?hughbe commentedon Oct 24, 2016
Correct - we assume DeclaringType is nonnull
bartdesmet commentedon Oct 25, 2016
I don't see any compelling reason why these shouldn't work just fine.
MethodInfoinstances withDeclaringTypeset tonullare valid, and our API claims to deal with allMethodInfoinstances just fine. I'd be curious to see how many places we have this assumption of a non-nullDeclaringTypethough (same for otherMemberInfosubtypes); the fix may show up in quite a few places.hughbe commentedon Oct 25, 2016
FieldInfo, MethodInfo and ConstructorInfo are the only MemberInfos that can have a null
DeclaringTypeI'll go through the ones I found:
Let me know if you disagree with any of these - I'll submit a PR for each adding tests etc. just to make sure we're allowing/disallowing the proper scenarios
hughbe commentedon Oct 25, 2016
Actually, I'm wrong about
AndAlsoandOrElse, as apparentlyop_trueandop_falsehave to be declared on the declaring type ofmethodJonHanna commentedon Jan 11, 2017
I don't think there's any reason a method or field should have to have a
DeclaringTypeunless there's some inherent reason for a restriction on what thatDeclaringTypecan be.With the
*Bindmethods though the objects produced are allMemberBindings rather than completeExpressions and used to produceMemberInitExpressions which have to be validated so that theDeclaringTypeof the binding is assignable from the type being initialised. Since any use of the object (unless there's another I can't think of) would hence fail (taking a look at the validation method, there's an assumption that theDeclaringTypeisn't null) throwing early is better than throwing late. It should be anArgumentExceptionrather than an NRE though.JonHanna commentedon Jan 18, 2017
@hughbe are you still intending to work on this? While I don't think it makes sense to allow such methods on bindings, we shouldn't have NRE thrown.
hughbe commentedon Jan 18, 2017
@JonHanna not currently! feel free to grab this if you'd like.
JonHanna commentedon Jan 18, 2017
Will do.
Throw ArgumentException on global method in AndAlso/OrElse
Throw ArgumentException on global member in Expression.New
Throw ArgumentException on global member in Expression.*Bind
Throw ArgumentException on global field in Expression.*Bind
Throw ArgumentException on global field in Expression.*Bind
Throw ArgumentException on global field in Expression.*Bind
Throw ArgumentException on global field in Expression.*Bind