Skip to content

Commit 72bd4b7

Browse files
Use Array.Resize - fixes #501
1 parent beb085a commit 72bd4b7

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1414
* Add AsEnumerable where needed in linq "in" clause - [#544](https://github.com/icsharpcode/CodeConverter/issues/544)
1515
* Remove redundant empty string coalesce in string comparison - [#500](https://github.com/icsharpcode/CodeConverter/issues/500)
1616
* Convert VB comparison operators - [#396](https://github.com/icsharpcode/CodeConverter/issues/396)
17+
* Convert Redim Preserve of 1D array to Array.Resize - [#501](https://github.com/icsharpcode/CodeConverter/issues/501)
1718

1819
### C# -> VB
1920

CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ private async Task<SyntaxList<StatementSyntax>> ConvertRedimClauseAsync(VBSyntax
228228

229229
var csTargetArrayExpression = (ExpressionSyntax) await node.Expression.AcceptAsync(_expressionVisitor);
230230
var convertedBounds = (await CommonConversions.ConvertArrayBoundsAsync(node.ArrayBounds)).Sizes.ToList();
231-
231+
if (preserve && convertedBounds.Count == 1) {
232+
var arrayResize = SyntaxFactory.InvocationExpression(ValidSyntaxFactory.MemberAccess(nameof(Array), nameof(Array.Resize)), new[] { csTargetArrayExpression, convertedBounds.Single() }.CreateCsArgList());
233+
return SingleStatement(arrayResize);
234+
}
232235
var newArrayAssignment = CreateNewArrayAssignment(node.Expression, csTargetArrayExpression, convertedBounds, node.SpanStart);
233236
if (!preserve) return SingleStatement(newArrayAssignment);
234237

CodeConverter/Util/ExpressionSyntaxExtensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@ internal static class ExpressionSyntaxExtensions
1313
{
1414
public static ArgumentListSyntax CreateArgList(params ExpressionSyntax[] args)
1515
{
16-
return SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(args.Select(SyntaxFactory.Argument)));
16+
return CreateCsArgList(args);
1717
}
1818

1919
public static VBSyntax.ArgumentListSyntax CreateArgList<T>(params T[] args) where T : VBSyntax.ExpressionSyntax
2020
{
21-
return CreateArgList((IEnumerable<T>) args);
21+
return CreateVbArgList((IEnumerable<T>) args);
2222
}
2323

24-
public static VBSyntax.ArgumentListSyntax CreateArgList<T>(this IEnumerable<T> argExpressions) where T : VBSyntax.ExpressionSyntax
24+
public static VBSyntax.ArgumentListSyntax CreateVbArgList<T>(this IEnumerable<T> argExpressions) where T : VBSyntax.ExpressionSyntax
2525
{
2626
return VBasic.SyntaxFactory.ArgumentList(VBasic.SyntaxFactory.SeparatedList(argExpressions.Select(e => (VBSyntax.ArgumentSyntax) VBasic.SyntaxFactory.SimpleArgument(e))));
2727
}
2828

29+
public static ArgumentListSyntax CreateCsArgList<T>(this IEnumerable<T> argExpressions) where T : ExpressionSyntax
30+
{
31+
return SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(argExpressions.Select(SyntaxFactory.Argument)));
32+
}
33+
2934
/// <summary>
3035
/// This is a conversion heavily based on Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax.SyntaxExtensions.ExtractAnonymousTypeMemberName from 1bbbfc28a8e4493b4057e171310343a4c7ba826c
3136
/// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0

CodeConverter/VB/NodesVisitor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ public override VisualBasicSyntaxNode VisitEventDeclaration(CSS.EventDeclaration
634634
var invocationExpression =
635635
SyntaxFactory.InvocationExpression(
636636
SyntaxFactory.ParseExpression(eventFieldIdentifier.Identifier.ValueText + "?"), //I think this syntax tree is the wrong shape, but using the right shape causes the simplifier to fail
637-
raiseEventParameters.Select(x => SyntaxFactory.IdentifierName(x.Identifier.Identifier)).CreateArgList()
637+
raiseEventParameters.Select(x => SyntaxFactory.IdentifierName(x.Identifier.Identifier)).CreateVbArgList()
638638
);
639639
riseEventAccessor = riseEventAccessor.WithStatements(SyntaxFactory.SingletonList((StatementSyntax)SyntaxFactory.ExpressionStatement(invocationExpression)));
640640
}
@@ -1005,7 +1005,7 @@ public override VisualBasicSyntaxNode VisitPostfixUnaryExpression(CSS.PostfixUna
10051005
ExpressionSyntaxExtensions.CreateArgList((ExpressionSyntax)node.Operand.Accept(TriviaConvertingVisitor))
10061006
),
10071007
SyntaxFactory.BinaryExpression(op, (ExpressionSyntax)node.Operand.Accept(TriviaConvertingVisitor), SyntaxFactory.Token(VBUtil.GetExpressionOperatorTokenKind(op)), _commonConversions.Literal(1))
1008-
}.CreateArgList()
1008+
}.CreateVbArgList()
10091009
);
10101010
}
10111011
}
@@ -1334,7 +1334,7 @@ public override VisualBasicSyntaxNode VisitArrayCreationExpression(CSS.ArrayCrea
13341334
SyntaxFactory.Token(SyntaxKind.NewKeyword),
13351335
SyntaxFactory.List<AttributeListSyntax>(),
13361336
(TypeSyntax)node.Type.ElementType.Accept(TriviaConvertingVisitor),
1337-
upperBoundArguments.Any() ? upperBoundArguments.CreateArgList() : null,
1337+
upperBoundArguments.Any() ? upperBoundArguments.CreateVbArgList() : null,
13381338
upperBoundArguments.Any() ? SyntaxFactory.List(rankSpecifiers.Skip(1)) : SyntaxFactory.List(rankSpecifiers),
13391339
(CollectionInitializerSyntax)node.Initializer?.Accept(TriviaConvertingVisitor) ?? SyntaxFactory.CollectionInitializer()
13401340
);

Tests/CSharp/MissingSemanticModelInfo/StatementTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,20 @@ public void Test()
3838
1 target compilation errors:
3939
CS0246: The type or namespace name 'Asadf' could not be found (are you missing a using directive or an assembly reference?)", missingSemanticInfo: true);
4040
}
41+
42+
[Fact]
43+
public async Task RedimOfUnknownVariableAsync()
44+
{
45+
await TestConversionVisualBasicToCSharpAsync(@"ReDim Preserve UnknownArray(unknownIntIdentifer)", @"{
46+
Array.Resize(UnknownArray, unknownIntIdentifer + 1);
47+
}
48+
49+
2 source compilation errors:
50+
BC30451: 'UnknownArray' is not declared. It may be inaccessible due to its protection level.
51+
BC30451: 'unknownIntIdentifer' is not declared. It may be inaccessible due to its protection level.
52+
2 target compilation errors:
53+
CS0103: The name 'UnknownArray' does not exist in the current context
54+
CS0103: The name 'unknownIntIdentifer' does not exist in the current context", missingSemanticInfo: true);
55+
}
4156
}
4257
}

Tests/CSharp/StatementTests/StatementTests.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,8 @@ public static int[] TestMethod(int[] numArray, int[] numArray2)
483483
numArray = new int[4];
484484
numArray = null;
485485
numArray2[1] = 1;
486-
var oldNumArray = numArray;
487-
numArray = new int[6];
488-
if (oldNumArray is object)
489-
Array.Copy(oldNumArray, numArray, Math.Min(6, oldNumArray.Length));
490-
var oldNumArray2 = numArray2;
491-
numArray2 = new int[6];
492-
if (oldNumArray2 is object)
493-
Array.Copy(oldNumArray2, numArray2, Math.Min(6, oldNumArray2.Length));
486+
Array.Resize(numArray, 6);
487+
Array.Resize(numArray2, 6);
494488
var y = new int[7, 6];
495489
y[2, 3] = 1;
496490
var oldY = y;
@@ -500,7 +494,9 @@ public static int[] TestMethod(int[] numArray, int[] numArray2)
500494
Array.Copy(oldY, i * oldY.GetLength(1), y, i * y.GetLength(1), Math.Min(oldY.GetLength(1), y.GetLength(1)));
501495
return numArray2;
502496
}
503-
}");
497+
}
498+
1 target compilation errors:
499+
CS1620: Argument 1 must be passed with the 'ref' keyword");
504500
}
505501

506502
[Fact]

0 commit comments

Comments
 (0)