-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #902 from Noah1989/issue-897
Fix Issue #897 (VB -> C#: variable is initialized inside loop)
- Loading branch information
Showing
5 changed files
with
243 additions
and
6 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
CodeConverter/CSharp/HoistedDefaultInitializedLoopVariable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Diagnostics; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace ICSharpCode.CodeConverter.CSharp; | ||
|
||
internal class HoistedDefaultInitializedLoopVariable : IHoistedNode | ||
{ | ||
public string OriginalVariableName { get; } | ||
public string Id { get; } | ||
public ExpressionSyntax Initializer { get; } | ||
public TypeSyntax Type { get; } | ||
public bool Nested { get; } | ||
|
||
public HoistedDefaultInitializedLoopVariable(string originalVariableName, ExpressionSyntax initializer, TypeSyntax type, bool nested) | ||
{ | ||
Debug.Assert(initializer is DefaultExpressionSyntax); | ||
OriginalVariableName = originalVariableName; | ||
Id = $"ph{Guid.NewGuid():N}"; | ||
Initializer = initializer; | ||
Type = type; | ||
Nested = nested; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
Tests/TestData/SelfVerifyingTests/VBToCS/VariableScopeTests.vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
Imports System | ||
Imports System.Linq | ||
Imports System.Xml.Linq | ||
Imports Xunit | ||
|
||
Public Class VariableScopeTests | ||
|
||
<Fact> | ||
Sub TestDeclarationInsideForLoop() | ||
For i = 1 To 2 | ||
Dim b As Boolean | ||
If i = 1 Then | ||
Assert.False(b) | ||
Else | ||
Assert.True(b) | ||
End If | ||
|
||
b = True | ||
Assert.True(b) | ||
Next | ||
End Sub | ||
|
||
<Fact> | ||
Sub TestDeclarationsInsideNestedLoops() | ||
Dim results As New List(Of String) | ||
Dim i = 1 | ||
Do | ||
Dim b As Integer | ||
b += 1 | ||
results.Add("b=" & b) | ||
For j = 1 To 3 | ||
Dim c As Integer | ||
c += 1 | ||
results.Add("c1=" & c) | ||
Next | ||
For j = 1 To 3 | ||
Dim c As Integer | ||
c += 1 | ||
results.Add("c2=" & c) | ||
Next | ||
Dim k = 1 | ||
Do While k <= 3 | ||
Dim c As Integer | ||
c += 1 | ||
results.Add("c3=" & c) | ||
k += 1 | ||
Loop | ||
i += 1 | ||
Loop Until i > 3 | ||
Assert.Equal( | ||
"b=1, c1=1, c1=2, c1=3, c2=1, c2=2, c2=3, c3=1, c3=2, c3=3, " & | ||
"b=2, c1=4, c1=5, c1=6, c2=4, c2=5, c2=6, c3=4, c3=5, c3=6, " & | ||
"b=3, c1=7, c1=8, c1=9, c2=7, c2=8, c2=9, c3=7, c3=8, c3=9", | ||
String.Join(", ", results)) | ||
End Sub | ||
|
||
End Class |