Skip to content

Commit

Permalink
Fix array initialization issues
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Mar 10, 2020
1 parent 69abb43 commit dd3315d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
15 changes: 8 additions & 7 deletions CodeConverter/CSharp/ExpressionNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,8 @@ public override async Task<CSharpSyntaxNode> VisitArrayCreationExpression(VBasic
{
var bounds = await CommonConversions.ConvertArrayRankSpecifierSyntaxes(node.RankSpecifiers, node.ArrayBounds);

var allowInitializer = node.Initializer.Initializers.Any()
|| node.RankSpecifiers.Any()
|| node.ArrayBounds == null
|| (node.Initializer.Initializers.Any() && node.ArrayBounds.Arguments.All(b => b.IsOmitted || _semanticModel.GetConstantValue(b.GetExpression()).HasValue));
var allowInitializer = node.ArrayBounds?.Arguments.Any() != true ||
node.Initializer.Initializers.Any() && node.ArrayBounds.Arguments.All(b => b.IsOmitted || _semanticModel.GetConstantValue(b.GetExpression()).HasValue);

var initializerToConvert = allowInitializer ? node.Initializer : null;
return SyntaxFactory.ArrayCreationExpression(
Expand All @@ -513,15 +511,18 @@ public override async Task<CSharpSyntaxNode> VisitCollectionInitializer(VBasic.S
var initializers = (await node.Initializers.SelectAsync(i => i.AcceptAsync(TriviaConvertingExpressionVisitor))).Cast<ExpressionSyntax>();
var initializer = SyntaxFactory.InitializerExpression(initializerKind, SyntaxFactory.SeparatedList(initializers));
if (isExplicitCollectionInitializer) return initializer;

if (!initializers.Any() && _semanticModel.GetTypeInfo(node).ConvertedType is IArrayTypeSymbol arrayType) {

if (!(_semanticModel.GetTypeInfo(node).ConvertedType is IArrayTypeSymbol arrayType)) return SyntaxFactory.ImplicitArrayCreationExpression(initializer);

if (!initializers.Any()) {

var arrayTypeArgs = SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList(CommonConversions.GetTypeSyntax(arrayType.ElementType)));
var arrayEmpty = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName(nameof(Array)), SyntaxFactory.GenericName(nameof(Array.Empty)).WithTypeArgumentList(arrayTypeArgs));
return SyntaxFactory.InvocationExpression(arrayEmpty);
}
return SyntaxFactory.ImplicitArrayCreationExpression(initializer);
var commas = Enumerable.Repeat(SyntaxFactory.Token(SyntaxKind.CommaToken), arrayType.Rank - 1);
return SyntaxFactory.ImplicitArrayCreationExpression(SyntaxFactory.TokenList(commas), initializer);
}

public override async Task<CSharpSyntaxNode> VisitQueryExpression(VBasic.Syntax.QueryExpressionSyntax node)
Expand Down
13 changes: 7 additions & 6 deletions Tests/CSharp/StatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -904,12 +904,11 @@ private void TestMethod()
var rankSpecifiers2 = new double[2, 2];
// Declare a jagged array
var sales = new double[12][] { };
var sales = new double[12][];
}
}
2 target compilation errors:
CS8751: Internal error in the C# compiler.
CS0847: An array initializer of length '12' is expected");
1 target compilation errors:
CS8751: Internal error in the C# compiler.");
}

[Fact]
Expand Down Expand Up @@ -941,7 +940,7 @@ internal partial class TestClass
{
private void TestMethod()
{
var b = new[] { { 1, 2 }, { 3, 4 } };
var b = new[,] { { 1, 2 }, { 3, 4 } };
}
}
1 target compilation errors:
Expand Down Expand Up @@ -978,6 +977,7 @@ Dim c as Integer(,,) = New Integer(,,) {{{1}}}
Dim d as Integer(,,) = New Integer(0, 0, 0) {{{1}}}
Dim e As Integer()(,) = New Integer()(,) {}
Dim f As Integer()(,) = New Integer(-1)(,) {}
Dim g As Integer()(,) = New Integer(0)(,) {}
End Sub
End Class", @"
internal partial class TestClass
Expand All @@ -989,7 +989,8 @@ private void TestMethod()
var c = new int[,,] { { { 1 } } };
var d = new int[1, 1, 1] { { { 1 } } };
var e = new int[][,] { };
var f = new int[0][,] { };
var f = new int[0][,];
var g = new int[1][,];
}
}
1 target compilation errors:
Expand Down

0 comments on commit dd3315d

Please sign in to comment.