Skip to content

Commit

Permalink
Consistent exception on empty block expression
Browse files Browse the repository at this point in the history
Fixes #3882

Fixes #3908
  • Loading branch information
JonHanna committed Oct 16, 2015
1 parent c9c79fe commit 4452dec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ public static BlockExpression Block(IEnumerable<ParameterExpression> variables,
}

var expressionList = expressions.ToReadOnly();
return BlockCore(expressionList.Last().Type, variableList, expressionList);
return BlockCore(null, variableList, expressionList);
}

/// <summary>
Expand Down Expand Up @@ -887,29 +887,30 @@ private static BlockExpression BlockCore(Type type, ReadOnlyCollection<Parameter
RequiresCanRead(expressionList, "expressions");
ValidateVariables(variableList, "variables");

Expression last = expressionList.Last();
if (type != typeof(void))
if (type != null)
{
if (!TypeUtils.AreReferenceAssignable(type, last.Type))
Expression last = expressionList.Last();
if (type != typeof(void))
{
throw Error.ArgumentTypesMustMatch();
if (!TypeUtils.AreReferenceAssignable(type, last.Type))
{
throw Error.ArgumentTypesMustMatch();
}
}

if (!TypeUtils.AreEquivalent(type, last.Type))
{
return new ScopeWithType(variableList, expressionList, type);
}
}

if (!TypeUtils.AreEquivalent(type, last.Type))
if (expressionList.Count == 1)
{
return new ScopeWithType(variableList, expressionList, type);
return new Scope1(variableList, expressionList[0]);
}
else
{
if (expressionList.Count == 1)
{
return new Scope1(variableList, expressionList[0]);
}
else
{
return new ScopeN(variableList, expressionList);
}
return new ScopeN(variableList, expressionList);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ public void NullExpressionList()
[Theory]
[MemberData("BlockSizes")]
[ActiveIssue(3909)]
[ActiveIssue(3908)]
public void NullExpressionInExpressionList(int size)
{
List<Expression> expressionList = Enumerable.Range(0, size).Select(i => (Expression)Expression.Constant(1)).ToList();
Expand Down Expand Up @@ -332,7 +331,6 @@ public void EmptyBlockNotAllowed()
}

[Fact]
[ActiveIssue(3882)]
public void EmptyBlockWithParametersNotAllowed()
{
Assert.Throws<ArgumentException>("expressions", () => Expression.Block(Enumerable.Repeat<ParameterExpression>(Expression.Parameter(typeof(int)), 1)));
Expand All @@ -342,7 +340,6 @@ public void EmptyBlockWithParametersNotAllowed()
}

// If https://github.com/dotnet/corefx/issues/3043 is ever actioned, this case would still be prohibited.

[Fact]
public void EmptyBlockWithNonVoidTypeNotAllowed()
{
Expand All @@ -351,7 +348,6 @@ public void EmptyBlockWithNonVoidTypeNotAllowed()
}

[Fact]
[ActiveIssue(3882)]
public void EmptyBlockWithParametersAndNonVoidTypeNotAllowed()
{
Assert.Throws<ArgumentException>("expressions", () => Expression.Block(typeof(int), Enumerable.Repeat<ParameterExpression>(Expression.Parameter(typeof(int)), 1)));
Expand Down
20 changes: 0 additions & 20 deletions src/System.Linq.Expressions/tests/Block/ParameterBlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public void NullExpressionList()

[Theory]
[MemberData("BlockSizes")]
[ActiveIssue(3908)]
public void NullExpressionInExpressionList(int size)
{
List<Expression> expressionList = Enumerable.Range(0, size).Select(i => (Expression)Expression.Constant(1)).ToList();
Expand All @@ -83,22 +82,6 @@ public void NullExpressionInExpressionList(int size)
}
}

[Theory]
[MemberData("BlockSizes")]
public void NullExpressionInExpressionListTemp(int size)
{
List<Expression> expressionList = Enumerable.Range(0, size).Select(i => (Expression)Expression.Constant(1)).ToList();
for (int i = 0; i != expressionList.Count; ++i)
{
Expression[] expressions = expressionList.ToArray();
expressions[i] = null;
Assert.ThrowsAny<Exception>(() => Expression.Block(SingleParameter, expressions));
Assert.ThrowsAny<Exception>(() => Expression.Block(SingleParameter, expressions.Skip(0)));
Assert.ThrowsAny<Exception>(() => Expression.Block(typeof(int), SingleParameter, expressions));
Assert.ThrowsAny<Exception>(() => Expression.Block(typeof(int), SingleParameter, expressions.Skip(0)));
}
}

[Theory]
[MemberData("BlockSizes")]
public void UnreadableExpressionInExpressionList(int size)
Expand Down Expand Up @@ -176,7 +159,6 @@ public void InvalidExpressionIndexVaryingExceptin(object value, int blockSize)

// See https://github.com/dotnet/corefx/issues/3043
[Fact]
[ActiveIssue(3882)]
public void EmptyBlockWithParametersNotAllowed()
{
Assert.Throws<ArgumentException>("expressions", () => Expression.Block(SingleParameter));
Expand All @@ -186,9 +168,7 @@ public void EmptyBlockWithParametersNotAllowed()
}

// If https://github.com/dotnet/corefx/issues/3043 is ever actioned, this case would still be prohibited.

[Fact]
[ActiveIssue(3882)]
public void EmptyBlockWithParametersAndNonVoidTypeNotAllowed()
{
Assert.Throws<ArgumentException>("expressions", () => Expression.Block(typeof(int), SingleParameter));
Expand Down

0 comments on commit 4452dec

Please sign in to comment.