Skip to content

Commit

Permalink
Fix type for externally declared control variable - fixes #609
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Aug 15, 2020
1 parent 38ed478 commit fd27e51
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* Convert bitwise negation [#599](https://github.com/icsharpcode/CodeConverter/issues/599)
* No longer adds incorrect "base" qualification for virtual method calls [#600](https://github.com/icsharpcode/CodeConverter/issues/600)
* Don't generate unnecessary properties for WithEvents fields [#572](https://github.com/icsharpcode/CodeConverter/issues/572)
* Add type conversion where needed for externally declared loop control variable [#609](https://github.com/icsharpcode/CodeConverter/issues/609)

### C# -> VB

Expand Down
9 changes: 5 additions & 4 deletions CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,13 @@ private async Task<StatementSyntax[]> ConvertStatementsAsync(SyntaxList<VBSyntax
public override async Task<SyntaxList<StatementSyntax>> VisitForBlock(VBSyntax.ForBlockSyntax node)
{
var stmt = node.ForStatement;
var startValue = await stmt.FromValue.AcceptAsync<ExpressionSyntax>(_expressionVisitor);
VariableDeclarationSyntax declaration = null;
ExpressionSyntax id;
var controlVarOp = _semanticModel.GetOperation(stmt.ControlVariable) as IVariableDeclaratorOperation;
var controlVarSymbol = controlVarOp?.Symbol;
var controlVarType = controlVarSymbol?.Type;
var controlVarSymbol = _semanticModel.GetSymbolInfo(stmt.ControlVariable).Symbol;
var controlVarType = controlVarSymbol?.GetSymbolType();
var startValue = await stmt.FromValue.AcceptAsync<ExpressionSyntax>(_expressionVisitor);
startValue = CommonConversions.TypeConversionAnalyzer.AddExplicitConversion(stmt.FromValue, startValue?.SkipIntoParens(), forceTargetType: controlVarType);

var initializers = new List<ExpressionSyntax>();
if (stmt.ControlVariable is VBSyntax.VariableDeclaratorSyntax) {
var v = (VBSyntax.VariableDeclaratorSyntax)stmt.ControlVariable;
Expand Down
35 changes: 35 additions & 0 deletions Tests/CSharp/StatementTests/LoopStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,41 @@ public static void Main(string[] args)
}");
}

[Fact]
public async Task Int16ForLoopAsync()
{
await TestConversionVisualBasicToCSharpAsync(@" Sub DummyMethod()
Dim someArray = New Integer() { 1, 2, 3}
For index As Int16 = 0 To someArray.Length - 1
Console.WriteLine(index)
Next
End Sub", @"public void DummyMethod()
{
var someArray = new int[] { 1, 2, 3 };
for (short index = 0, loopTo = Conversions.ToShort(someArray.Length - 1); index <= loopTo; index++)
Console.WriteLine(index);
}");
}

[Fact]
public async Task ExternallyDeclaredLoopVariableAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Sub Main()
Dim foo As Single = 3.5
Dim index As Integer
For index = Int(foo) To Int(foo * 3)
Console.WriteLine(index)
Next
End Sub", @"public void Main()
{
float foo = 3.5F;
int index;
var loopTo = Conversions.ToInteger(Conversion.Int(foo * 3));
for (index = Conversions.ToInteger(Conversion.Int(foo)); index <= loopTo; index++)
Console.WriteLine(index);
}");
}

[Fact]
public async Task ForNonNegativeStepAsync()
{
Expand Down

0 comments on commit fd27e51

Please # to comment.