Skip to content

Commit 18e8c90

Browse files
Add ref keyword for Array.Resize
1 parent a3a8e65 commit 18e8c90

5 files changed

Lines changed: 13 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
99

1010

1111
### VB -> C#
12-
12+
* When converting ReDim Preserve to Array.Resize, "ref" is now added
1313

1414
### C# -> VB
1515

CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ private async Task<SyntaxList<StatementSyntax>> ConvertRedimClauseAsync(VBSyntax
229229
var csTargetArrayExpression = (ExpressionSyntax) await node.Expression.AcceptAsync(_expressionVisitor);
230230
var convertedBounds = (await CommonConversions.ConvertArrayBoundsAsync(node.ArrayBounds)).Sizes.ToList();
231231
if (preserve && convertedBounds.Count == 1) {
232-
var arrayResize = SyntaxFactory.InvocationExpression(ValidSyntaxFactory.MemberAccess(nameof(Array), nameof(Array.Resize)), new[] { csTargetArrayExpression, convertedBounds.Single() }.CreateCsArgList());
232+
var argumentList = new[] { csTargetArrayExpression, convertedBounds.Single() }.CreateCsArgList(SyntaxKind.RefKeyword);
233+
var arrayResize = SyntaxFactory.InvocationExpression(ValidSyntaxFactory.MemberAccess(nameof(Array), nameof(Array.Resize)), argumentList);
233234
return SingleStatement(arrayResize);
234235
}
235236
var newArrayAssignment = CreateNewArrayAssignment(node.Expression, csTargetArrayExpression, convertedBounds, node.SpanStart);

CodeConverter/Util/ExpressionSyntaxExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ public static VBSyntax.ArgumentListSyntax CreateVbArgList<T>(this IEnumerable<T>
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
29+
public static ArgumentListSyntax CreateCsArgList<T>(this IEnumerable<T> argExpressions, params SyntaxKind[] refTokenKinds) where T : ExpressionSyntax
3030
{
31-
return SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(argExpressions.Select(SyntaxFactory.Argument)));
31+
return SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(argExpressions.Select((e, i) => {
32+
var arg = SyntaxFactory.Argument(e);
33+
if (i < refTokenKinds.Length) arg = arg.WithRefKindKeyword(SyntaxFactory.Token(refTokenKinds[i]));
34+
return arg;
35+
})));
3236
}
3337

3438
/// <summary>

Tests/CSharp/MissingSemanticModelInfo/StatementTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void Test()
4343
public async Task RedimOfUnknownVariableAsync()
4444
{
4545
await TestConversionVisualBasicToCSharpAsync(@"ReDim Preserve UnknownArray(unknownIntIdentifer)", @"{
46-
Array.Resize(UnknownArray, unknownIntIdentifer + 1);
46+
Array.Resize(ref UnknownArray, unknownIntIdentifer + 1);
4747
}
4848
4949
2 source compilation errors:

Tests/CSharp/StatementTests/StatementTests.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ public static int[] TestMethod(int[] numArray, int[] numArray2)
483483
numArray = new int[4];
484484
numArray = null;
485485
numArray2[1] = 1;
486-
Array.Resize(numArray, 6);
487-
Array.Resize(numArray2, 6);
486+
Array.Resize(ref numArray, 6);
487+
Array.Resize(ref numArray2, 6);
488488
var y = new int[7, 6];
489489
y[2, 3] = 1;
490490
var oldY = y;
@@ -494,9 +494,7 @@ public static int[] TestMethod(int[] numArray, int[] numArray2)
494494
Array.Copy(oldY, i * oldY.GetLength(1), y, i * y.GetLength(1), Math.Min(oldY.GetLength(1), y.GetLength(1)));
495495
return numArray2;
496496
}
497-
}
498-
1 target compilation errors:
499-
CS1620: Argument 1 must be passed with the 'ref' keyword");
497+
}");
500498
}
501499

502500
[Fact]

0 commit comments

Comments
 (0)