Skip to content
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

Cannot use local variable 'var' before it is declared #1628

Closed
greenozon opened this issue Aug 9, 2019 · 9 comments
Closed

Cannot use local variable 'var' before it is declared #1628

greenozon opened this issue Aug 9, 2019 · 9 comments
Labels
Bug C# Decompiler The decompiler engine itself

Comments

@greenozon
Copy link

greenozon commented Aug 9, 2019

ILSpy version 5.0.0.5057-preview4

ref to method

public void RemoveConsole()

it is having issue wtih local var
It is declared nowhere..

VS error:

error CS0841: Cannot use local variable 'installation' before it is declared
@siegfriedpammer
Copy link
Member

siegfriedpammer commented Aug 10, 2019

I cannot find an undeclared variable in the source code. Could you please post the snippet showing the error? Thank you!

@siegfriedpammer siegfriedpammer added the Feedback Needed This issue requires user feedback label Aug 10, 2019
@greenozon
Copy link
Author

wrong input, it's here

https://www.sendspace.com/file/0iis4h

please also pay attention that RemoveConsole() method has way too many goto's...

@siegfriedpammer
Copy link
Member

In the assembly you provided there is no method called "RemoveConsole". Can you please clarify? Thank you!

@greenozon
Copy link
Author

pardon again

code snippet

					if (Program.Storage.DirectoryExists(_customPath))
					{
						Program.Out.WriteLine();
						Program.Out.WriteLine($"Removing from custom path: '{_customPath}'.");
						if (Program.Where.FindGitInstallation(_customPath, KnownDistribution.GitForWindows64v2, out installation) || Program.Where.FindGitInstallation(_customPath, KnownDistribution.GitForWindows32v2, out installation) || Program.Where.FindGitInstallation(_customPath, KnownDistribution.GitForWindows32v1, out Installation installation))
						{
							Program.Trace.WriteLine($"Git found: '{installation.Path}'.", "E:\\A\\_work\\319\\s\\Cli\\Manager\\Installer.cs", 530, "RemoveConsole");
							installations = new List<Installation>
							{
								installation
							};
						}
						goto IL_01f2;
					}

see the "out installation" - it is used without declaration

BTW, ILSpy does not show body of inside types!?

image

https://www.sendspace.com/file/usqvp1

@siegfriedpammer
Copy link
Member

The assembly looks like it only contains internal types. You will have to activate the "Show internal types and members" option. (The toolbar button next to "Reload assemblies").

@siegfriedpammer
Copy link
Member

Can you please provide all assemblies that are referenced by this one?

I cannot reproduce this, because the method in question is defined in a referenced assembly.

            if (Program.Where.FindGitInstallation(_customPath, 2, ref val)
                            || Program.Where.FindGitInstallation(_customPath, 1, ref val)
                            || Program.Where.FindGitInstallation(_customPath, 0, ref val))

out parameters cannot be detected, if the definition cannot be found.

From the code you posted:

       if (Program.Where.FindGitInstallation(_customPath, KnownDistribution.GitForWindows64v2, out installation)
                     || Program.Where.FindGitInstallation(_customPath, KnownDistribution.GitForWindows32v2, out installation)
                     || Program.Where.FindGitInstallation(_customPath, KnownDistribution.GitForWindows32v1, out Installation installation))

Seems like the out var declaration is added in the wrong place.

@greenozon
Copy link
Author

Thanks for tip about internal types! forget about it at all :)
it worked like a charm

  1. here is a full set of ref assemblies
    issue still present if having them all resolved
    similar issue in another method - DeployConsole()

https://www.sendspace.com/file/3xetv8

@siegfriedpammer siegfriedpammer added Bug C# Decompiler The decompiler engine itself and removed Feedback Needed This issue requires user feedback labels Aug 12, 2019
@siegfriedpammer
Copy link
Member

As already mentioned in #1634 the problem is that DeclareVariables visits the nodes of the syntax tree in the order of the linked list used internally. Some AST transforms however, change the order of their children in a way that the order of the nodes in the linked list no longer reflects the order in which they will be written when printed as source code. But DeclareVariables assumes this and due to that the transform ends up declaring the variable in the wrong place.

@dgrunwald I think, DeclareVariables will either have to traverse the nodes the same way CSharpOutputVisitor does (but this is a major rewrite using an explicit visitor pattern, which will make the code bloated) or we fix this in the C# AST. The linked list approach has caused problems in the past (#1202).

@siegfriedpammer
Copy link
Member

I have added a simple fix for this particular problem. Note that this is not yet included in the just released preview4. I was too slow :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2020
ElektroKill pushed a commit to dnSpyEx/ILSpy that referenced this issue Jul 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug C# Decompiler The decompiler engine itself
Projects
None yet
Development

No branches or pull requests

2 participants