Skip to content

Commit

Permalink
Merge pull request dotnet#8953 from hughbe/linq-params
Browse files Browse the repository at this point in the history
Add a few more parameter names to System.Linq.Expressions exceptions
  • Loading branch information
stephentoub authored Jun 26, 2016
2 parents 342c3c3 + f049e6a commit c959bae
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ public static BinaryExpression MakeBinary(ExpressionType binaryType, Expression
case ExpressionType.MultiplyAssignChecked:
return MultiplyAssignChecked(left, right, method, conversion);
default:
throw Error.UnhandledBinary(binaryType);
throw Error.UnhandledBinary(binaryType, nameof(binaryType));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private void EmitUnliftedBinaryOp(ExpressionType op, Type leftType, Type rightTy
}
break;
default:
throw Error.UnhandledBinary(op);
throw Error.UnhandledBinary(op, nameof(op));
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/System.Linq.Expressions/src/System/Linq/Expressions/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,9 @@ internal static Exception IncorrectNumberOfArgumentsForMembers()
/// <summary>
/// ArgumentException with message like "Lambda type parameter must be derived from System.MulticastDelegate"
/// </summary>
internal static Exception LambdaTypeMustBeDerivedFromSystemDelegate()
internal static Exception LambdaTypeMustBeDerivedFromSystemDelegate(string paramName)
{
return new ArgumentException(Strings.LambdaTypeMustBeDerivedFromSystemDelegate);
return new ArgumentException(Strings.LambdaTypeMustBeDerivedFromSystemDelegate, paramName);
}
/// <summary>
/// ArgumentException with message like "Member '{0}' not field or property"
Expand Down Expand Up @@ -576,9 +576,9 @@ internal static Exception PropertyDoesNotHaveAccessor(object p0, string paramNam
/// <summary>
/// ArgumentException with message like "'{0}' is not a member of type '{1}'"
/// </summary>
internal static Exception NotAMemberOfType(object p0, object p1)
internal static Exception NotAMemberOfType(object p0, object p1, string paramName)
{
return new ArgumentException(Strings.NotAMemberOfType(p0, p1));
return new ArgumentException(Strings.NotAMemberOfType(p0, p1), paramName);
}
/// <summary>
/// PlatformNotSupportedException with message like "The instruction '{0}' is not supported for type '{1}'"
Expand All @@ -604,16 +604,16 @@ internal static Exception ParameterExpressionNotValidAsDelegate(object p0, objec
/// <summary>
/// ArgumentException with message like "Property '{0}' is not defined for type '{1}'"
/// </summary>
internal static Exception PropertyNotDefinedForType(object p0, object p1)
internal static Exception PropertyNotDefinedForType(object p0, object p1, string paramName)
{
return new ArgumentException(Strings.PropertyNotDefinedForType(p0, p1));
return new ArgumentException(Strings.PropertyNotDefinedForType(p0, p1), paramName);
}
/// <summary>
/// ArgumentException with message like "Instance property '{0}' is not defined for type '{1}'"
/// </summary>
internal static Exception InstancePropertyNotDefinedForType(object p0, object p1)
internal static Exception InstancePropertyNotDefinedForType(object p0, object p1, string paramName)
{
return new ArgumentException(Strings.InstancePropertyNotDefinedForType(p0, p1));
return new ArgumentException(Strings.InstancePropertyNotDefinedForType(p0, p1), paramName);
}
/// <summary>
/// ArgumentException with message like "Instance property '{0}' that takes no argument is not defined for type '{1}'"
Expand Down Expand Up @@ -697,9 +697,9 @@ internal static Exception InvalidCast(object p0, object p1)
/// <summary>
/// ArgumentException with message like "Unhandled binary: {0}"
/// </summary>
internal static Exception UnhandledBinary(object p0)
internal static Exception UnhandledBinary(object p0, string paramName)
{
return new ArgumentException(Strings.UnhandledBinary(p0));
return new ArgumentException(Strings.UnhandledBinary(p0), paramName);
}
/// <summary>
/// ArgumentException with message like "Unhandled binding "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public static Expression<TDelegate> Lambda<TDelegate>(Expression body, String na
public static Expression<TDelegate> Lambda<TDelegate>(Expression body, String name, bool tailCall, IEnumerable<ParameterExpression> parameters)
{
var parameterList = parameters.ToReadOnly();
ValidateLambdaArgs(typeof(TDelegate), ref body, parameterList);
ValidateLambdaArgs(typeof(TDelegate), ref body, parameterList, nameof(TDelegate));
return new Expression<TDelegate>(body, name, tailCall, parameterList);
}

Expand Down Expand Up @@ -512,7 +512,7 @@ public static LambdaExpression Lambda(Expression body, string name, bool tailCal
public static LambdaExpression Lambda(Type delegateType, Expression body, string name, IEnumerable<ParameterExpression> parameters)
{
var paramList = parameters.ToReadOnly();
ValidateLambdaArgs(delegateType, ref body, paramList);
ValidateLambdaArgs(delegateType, ref body, paramList, nameof(delegateType));

return CreateLambda(delegateType, body, name, false, paramList);
}
Expand All @@ -529,19 +529,19 @@ public static LambdaExpression Lambda(Type delegateType, Expression body, string
public static LambdaExpression Lambda(Type delegateType, Expression body, string name, bool tailCall, IEnumerable<ParameterExpression> parameters)
{
var paramList = parameters.ToReadOnly();
ValidateLambdaArgs(delegateType, ref body, paramList);
ValidateLambdaArgs(delegateType, ref body, paramList, nameof(delegateType));

return CreateLambda(delegateType, body, name, tailCall, paramList);
}

private static void ValidateLambdaArgs(Type delegateType, ref Expression body, ReadOnlyCollection<ParameterExpression> parameters)
private static void ValidateLambdaArgs(Type delegateType, ref Expression body, ReadOnlyCollection<ParameterExpression> parameters, string paramName)
{
ContractUtils.RequiresNotNull(delegateType, nameof(delegateType));
RequiresCanRead(body, nameof(body));

if (!typeof(MulticastDelegate).IsAssignableFrom(delegateType) || delegateType == typeof(MulticastDelegate))
{
throw Error.LambdaTypeMustBeDerivedFromSystemDelegate();
throw Error.LambdaTypeMustBeDerivedFromSystemDelegate(paramName);
}

MethodInfo mi;
Expand Down Expand Up @@ -585,7 +585,7 @@ private static void ValidateLambdaArgs(Type delegateType, ref Expression body, R
}
if (!set.Add(pex))
{
throw Error.DuplicateVariable(pex, $"parameters[{i}]");
throw Error.DuplicateVariable(pex, $"{nameof(parameters)}[{i}]");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static MemberExpression Property(Expression expression, string propertyNa
}
if (pi == null)
{
throw Error.InstancePropertyNotDefinedForType(propertyName, expression.Type);
throw Error.InstancePropertyNotDefinedForType(propertyName, expression.Type, nameof(propertyName));
}
return Property(expression, pi);
}
Expand All @@ -265,7 +265,7 @@ public static MemberExpression Property(Expression expression, Type type, string
}
if (pi == null)
{
throw Error.PropertyNotDefinedForType(propertyName, type);
throw Error.PropertyNotDefinedForType(propertyName, type, nameof(propertyName));
}
return Property(expression, pi);
}
Expand Down Expand Up @@ -311,7 +311,7 @@ public static MemberExpression Property(Expression expression, PropertyInfo prop
RequiresCanRead(expression, nameof(expression));
if (!TypeUtils.IsValidInstanceType(property, expression.Type))
{
throw Error.PropertyNotDefinedForType(property, expression.Type);
throw Error.PropertyNotDefinedForType(property, expression.Type, nameof(property));
}
}

Expand Down Expand Up @@ -393,7 +393,7 @@ public static MemberExpression PropertyOrField(Expression expression, string pro
if (fi != null)
return Field(expression, fi);

throw Error.NotAMemberOfType(propertyOrFieldName, expression.Type);
throw Error.NotAMemberOfType(propertyOrFieldName, expression.Type, nameof(propertyOrFieldName));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static void ValidateMemberInitArgs(Type type, ReadOnlyCollection<MemberB
ContractUtils.RequiresNotNull(b, nameof(bindings));
if (!b.Member.DeclaringType.IsAssignableFrom(type))
{
throw Error.NotAMemberOfType(b.Member.Name, type);
throw Error.NotAMemberOfType(b.Member.Name, type, $"{nameof(bindings)}[{i}]");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public static IEnumerable<object[]> NonBinaryTypesIncludingInvalidData()
[MemberData(nameof(NonBinaryTypesIncludingInvalidData))]
public void MakeBinaryInvalidType(ExpressionType type)
{
Assert.Throws<ArgumentException>(null, () => Expression.MakeBinary(type, Expression.Constant(0), Expression.Constant(0)));
Assert.Throws<ArgumentException>(null, () => Expression.MakeBinary(type, Expression.Constant(0), Expression.Constant(0), false, null));
Assert.Throws<ArgumentException>(null, () => Expression.MakeBinary(type, Expression.Constant(0), Expression.Constant(0), false, null, null));
Assert.Throws<ArgumentException>("binaryType", () => Expression.MakeBinary(type, Expression.Constant(0), Expression.Constant(0)));
Assert.Throws<ArgumentException>("binaryType", () => Expression.MakeBinary(type, Expression.Constant(0), Expression.Constant(0), false, null));
Assert.Throws<ArgumentException>("binaryType", () => Expression.MakeBinary(type, Expression.Constant(0), Expression.Constant(0), false, null, null));
}

[Theory]
Expand Down
38 changes: 19 additions & 19 deletions src/System.Linq.Expressions/tests/Lambda/LambdaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,27 @@ public void HighArityDelegate(bool useInterpreter)
[Fact]
public void LambdaTypeMustBeDelegate()
{
Assert.Throws<ArgumentException>(null, () => Expression.Lambda<object>(Expression.Constant(0)));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda<int>(Expression.Constant(0)));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda<object>(Expression.Constant(0), true));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda<object>(Expression.Constant(0), true, Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda<object>(Expression.Constant(0), "foo", Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda(typeof(object), Expression.Constant(0)));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda(typeof(int), Expression.Constant(0)));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda(typeof(object), Expression.Constant(0), true));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda(typeof(object), Expression.Constant(0), true, Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda(typeof(object), Expression.Constant(0), "foo", Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>("TDelegate", () => Expression.Lambda<object>(Expression.Constant(0)));
Assert.Throws<ArgumentException>("TDelegate", () => Expression.Lambda<int>(Expression.Constant(0)));
Assert.Throws<ArgumentException>("TDelegate", () => Expression.Lambda<object>(Expression.Constant(0), true));
Assert.Throws<ArgumentException>("TDelegate", () => Expression.Lambda<object>(Expression.Constant(0), true, Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>("TDelegate", () => Expression.Lambda<object>(Expression.Constant(0), "foo", Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>("delegateType", () => Expression.Lambda(typeof(object), Expression.Constant(0)));
Assert.Throws<ArgumentException>("delegateType", () => Expression.Lambda(typeof(int), Expression.Constant(0)));
Assert.Throws<ArgumentException>("delegateType", () => Expression.Lambda(typeof(object), Expression.Constant(0), true));
Assert.Throws<ArgumentException>("delegateType", () => Expression.Lambda(typeof(object), Expression.Constant(0), true, Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>("delegateType", () => Expression.Lambda(typeof(object), Expression.Constant(0), "foo", Enumerable.Empty<ParameterExpression>()));

// Note, be derived from MulticastDelegate, not merely actually MulticastDelegate or Delegate.
Assert.Throws<ArgumentException>(null, () => Expression.Lambda<Delegate>(Expression.Constant(0), true, Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda<Delegate>(Expression.Constant(0), "foo", Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda(typeof(Delegate), Expression.Constant(0)));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda(typeof(Delegate), Expression.Constant(0), true));

Assert.Throws<ArgumentException>(null, () => Expression.Lambda<MulticastDelegate>(Expression.Constant(0), true, Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda<MulticastDelegate>(Expression.Constant(0), "foo", Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda(typeof(MulticastDelegate), Expression.Constant(0)));
Assert.Throws<ArgumentException>(null, () => Expression.Lambda(typeof(MulticastDelegate), Expression.Constant(0), true));
Assert.Throws<ArgumentException>("TDelegate", () => Expression.Lambda<Delegate>(Expression.Constant(0), true, Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>("TDelegate", () => Expression.Lambda<Delegate>(Expression.Constant(0), "foo", Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>("delegateType", () => Expression.Lambda(typeof(Delegate), Expression.Constant(0)));
Assert.Throws<ArgumentException>("delegateType", () => Expression.Lambda(typeof(Delegate), Expression.Constant(0), true));

Assert.Throws<ArgumentException>("TDelegate", () => Expression.Lambda<MulticastDelegate>(Expression.Constant(0), true, Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>("TDelegate", () => Expression.Lambda<MulticastDelegate>(Expression.Constant(0), "foo", Enumerable.Empty<ParameterExpression>()));
Assert.Throws<ArgumentException>("delegateType", () => Expression.Lambda(typeof(MulticastDelegate), Expression.Constant(0)));
Assert.Throws<ArgumentException>("delegateType", () => Expression.Lambda(typeof(MulticastDelegate), Expression.Constant(0), true));
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/System.Linq.Expressions/tests/MemberInit/BindTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void MustBeMemberOfType()
typeof(PropertyAndFields).GetProperty(nameof(PropertyAndFields.StringProperty)),
Expression.Constant("value")
);
Assert.Throws<ArgumentException>(null, () => Expression.MemberInit(newExp, bind));
Assert.Throws<ArgumentException>("bindings[0]", () => Expression.MemberInit(newExp, bind));
}

[Theory, ClassData(typeof(CompilationTypes))]
Expand Down

0 comments on commit c959bae

Please sign in to comment.