Skip to content

Commit fd27e51

Browse files
Fix type for externally declared control variable - fixes #609
1 parent 38ed478 commit fd27e51

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
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
* Convert bitwise negation [#599](https://github.com/icsharpcode/CodeConverter/issues/599)
1515
* No longer adds incorrect "base" qualification for virtual method calls [#600](https://github.com/icsharpcode/CodeConverter/issues/600)
1616
* Don't generate unnecessary properties for WithEvents fields [#572](https://github.com/icsharpcode/CodeConverter/issues/572)
17+
* Add type conversion where needed for externally declared loop control variable [#609](https://github.com/icsharpcode/CodeConverter/issues/609)
1718

1819
### C# -> VB
1920

CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,13 @@ private async Task<StatementSyntax[]> ConvertStatementsAsync(SyntaxList<VBSyntax
500500
public override async Task<SyntaxList<StatementSyntax>> VisitForBlock(VBSyntax.ForBlockSyntax node)
501501
{
502502
var stmt = node.ForStatement;
503-
var startValue = await stmt.FromValue.AcceptAsync<ExpressionSyntax>(_expressionVisitor);
504503
VariableDeclarationSyntax declaration = null;
505504
ExpressionSyntax id;
506-
var controlVarOp = _semanticModel.GetOperation(stmt.ControlVariable) as IVariableDeclaratorOperation;
507-
var controlVarSymbol = controlVarOp?.Symbol;
508-
var controlVarType = controlVarSymbol?.Type;
505+
var controlVarSymbol = _semanticModel.GetSymbolInfo(stmt.ControlVariable).Symbol;
506+
var controlVarType = controlVarSymbol?.GetSymbolType();
507+
var startValue = await stmt.FromValue.AcceptAsync<ExpressionSyntax>(_expressionVisitor);
508+
startValue = CommonConversions.TypeConversionAnalyzer.AddExplicitConversion(stmt.FromValue, startValue?.SkipIntoParens(), forceTargetType: controlVarType);
509+
509510
var initializers = new List<ExpressionSyntax>();
510511
if (stmt.ControlVariable is VBSyntax.VariableDeclaratorSyntax) {
511512
var v = (VBSyntax.VariableDeclaratorSyntax)stmt.ControlVariable;

Tests/CSharp/StatementTests/LoopStatementTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,41 @@ public static void Main(string[] args)
382382
}");
383383
}
384384

385+
[Fact]
386+
public async Task Int16ForLoopAsync()
387+
{
388+
await TestConversionVisualBasicToCSharpAsync(@" Sub DummyMethod()
389+
Dim someArray = New Integer() { 1, 2, 3}
390+
For index As Int16 = 0 To someArray.Length - 1
391+
Console.WriteLine(index)
392+
Next
393+
End Sub", @"public void DummyMethod()
394+
{
395+
var someArray = new int[] { 1, 2, 3 };
396+
for (short index = 0, loopTo = Conversions.ToShort(someArray.Length - 1); index <= loopTo; index++)
397+
Console.WriteLine(index);
398+
}");
399+
}
400+
401+
[Fact]
402+
public async Task ExternallyDeclaredLoopVariableAsync()
403+
{
404+
await TestConversionVisualBasicToCSharpAsync(@"Sub Main()
405+
Dim foo As Single = 3.5
406+
Dim index As Integer
407+
For index = Int(foo) To Int(foo * 3)
408+
Console.WriteLine(index)
409+
Next
410+
End Sub", @"public void Main()
411+
{
412+
float foo = 3.5F;
413+
int index;
414+
var loopTo = Conversions.ToInteger(Conversion.Int(foo * 3));
415+
for (index = Conversions.ToInteger(Conversion.Int(foo)); index <= loopTo; index++)
416+
Console.WriteLine(index);
417+
}");
418+
}
419+
385420
[Fact]
386421
public async Task ForNonNegativeStepAsync()
387422
{

0 commit comments

Comments
 (0)