-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VB -> C#: Incorrect conversion of If
immediately preceded by Else
#823
Comments
Thanks for the report - changes to output logic like this are indeed high priority to fix. I tried this: Public Class VisualBasicClass
Public Sub X(OUTER As Boolean, FOO1 As Boolean, Bar1 As Action, FOO2 As Boolean, Bar2 As Action)
If OUTER Then
If FOO1 Then Bar1() : Exit Sub
Else 'NOT OUTER
If FOO2 Then
Bar2()
Exit Sub
End If
End If 'OUTER
End Sub
End Class which converts (correctly) to this in the VS extension: public class VisualBasicClass
{
public void X(bool OUTER, bool FOO1, Action Bar1, bool FOO2, Action Bar2)
{
if (OUTER)
{
if (FOO1)
{
Bar1();
return;
}
}
else if (FOO2) // NOT OUTER
{
Bar2();
return;
} // OUTER
}
} I've also tried some similar runnable snippets to ensure the same output comes out before and after conversion such VB input -> CS code converter output Can you provide a similar repro which provides unexpected behaviour? (It may be convenient to use https://codeconverter.icsharpcode.net/ to quickly try converting snippets) Thanks! |
Thank you for looking into this. Strangely I cannot reproduce it with a fuller example using online code converter either. However I did notice that the online converter is very explicit about If FIRST Then
If OUTER Then
If FOO1 Then Bar1() : Exit Sub
Else 'NOT OUTER
If FOO2 Then
Bar2()
Exit Sub
End If
End If 'OUTER
End If 'FIRST The first 2 if (FIRST) // <- No { for FIRST
if (OUTER) // <- No { for OUTER
if (FOO1) {
Bar1();
return;
} else if (FOO2) {
Bar2();
return;
}
// <- No } for OUTER
// <- No } for FIRST The problem arises in the
So the question is why VS Extension applied a different short-hand braceless formatting during the conversion which led to the incorrect result, while the Online Converter always used explicit brace formatting and thud converted correctly. |
Also I could reproduce this erroneous behavior is VS 2022. See the attached sample project which includes both the vb source and cs output. Here's the log:
So, maybe something happens in the 2nd - Simplification stage? |
Interesting. I run the conversion on your sample project (VS 2022 17.0.5, extension: 8.4.5.0) and I got the correct result: public static void TestMe(bool FIRST, bool OUTER, bool FOO1, bool FOO2)
{
if (FIRST)
{
if (OUTER)
{
if (FOO1)
{
Bar1();
return;
}
}
else if (FOO2) // NOT OUTER
{
Bar2();
return;
} // OUTER
} // FIRST
} Are you sure your extension is up to date? |
Hmmm. Interesting indeed! Yes, I use VS 2022 17.1 latest preview and Code Converter 8.4.5.0. And I did include in the zipped repro the exact output I got after the conversion. Might it have something to do with Net Framework vs Dot Net 6 which I now target? Or it has to do with |
Thanks both. The "simplification" stage is mostly just a call to the codeanalysis (AKA roslyn) library, so the Visual Studio version can have an impact. I'll try with the latest preview and see if I can repro when I get the chance. There is an open issue #649 for using the editorconfig which I've never figured out how to do, but it's possible it's now being automatically picked up by more recent versions. One thing you could try, is to take the correct output, put it in a csharp document in the same solution (i.e. with your editorconfig), and see if it erroneously offers the auto-fix option of removing the braces, or whether running a format command on the document causes the problem. |
I think I found the problem, which is related to the code style but is not necessarily due to So, when the setting under Text Editor, C#, Code Style, General, Code block preference, Prefer braces is set to Yes, then everything works correctly - the converted C# code always has braces. But when it is set to No, it produces erroneous code as I reported. I confirmed this by first changing the setting to Yes and running the conversion which produced proper results. Then changed it back to No (the way I prefer) and it produced erroneous output once again. Formatting the document does not affect this likely because the setting is set to be applied as a Refactoring-only. So, as a workaround, I guess i should make sure the setting is at Yes. However this raises a few questions:
|
Thanks for that, I can confirm that changing the setting does allow me to reproduce the issue converting the example I gave here: I'll see if I can find out why this is happening |
Debugging through, I can see that the issue is indeed introduced here: CodeConverter/CodeConverter/Shared/DocumentExtensions.cs Lines 110 to 112 in 068c7dd
This looks like a bug in roslyn. I'll try to repro purely against that library and file/fix upstream if I can. Even if the fix was made today, we couldn't rely on everyone using a version of VS containing it for years, so it's worth a fix/workaround given the severity. In the file I referenced you can see it does grab the options from the Document (which Visual Studio controls). Originally I'd hoped this would include editorconfig stuff, though I've seen no evidence of that ever working. |
I did make an attempt at reproing within roslyn, but got bogged down by trying to find an existing type of test to fit this in with and ran out of time. Will try again when I have more time. |
Thank you for your efforts in this area! For my part I have temporarily changed the Prefer Braces option to Yes to avoid this issue. It's just that until this is properly fixed, perhaps a warning message should be displayed to the users informing them that they may need to change this option. Also, I am still not sure if other code style options can have adverse impact on the conversion too, but so far at least I have not encountered anything. |
Seems like a pretty serious bug, as it may screw up control flow significantly and lead to completely unexpected results.
VB.Net input code
Erroneous output
Expected output
Details
The text was updated successfully, but these errors were encountered: