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

Code generated by VB's On Error Resume Next causes "unassigned variable" compile errors #2039

Closed
siegfriedpammer opened this issue Jun 14, 2020 · 1 comment
Labels
Bug C# Decompiler The decompiler engine itself

Comments

@siegfriedpammer
Copy link
Member

siegfriedpammer commented Jun 14, 2020

This piece of code is from VB's My-namespace implementation of a Windows Forms project. (for the binary sample see #2033)

[DebuggerHidden]
private static T Create__Instance__<T>(T Instance) where T : Form, new()
{
	if (Instance?.IsDisposed ?? true)
	{
		if (m_FormBeingCreated != null)
		{
			if (m_FormBeingCreated.ContainsKey(typeof(T)))
			{
				throw new InvalidOperationException(Utils.GetResourceString("WinForms_RecursiveFormCreate"));
			}
		}
		else
		{
			m_FormBeingCreated = new Hashtable();
		}
		m_FormBeingCreated.Add(typeof(T), null);
		TargetInvocationException ex2; // <------- ex2 is probably missing the init flag
		try
		{
			return new T();
		}
		catch (TargetInvocationException ex) when (((Func<bool>)delegate
		{
			// Could not convert BlockContainer to single expression
			ProjectData.SetProjectError(ex);
			ex2 = ex;
			return ex2.InnerException != null;
		}).Invoke())
		{
			string resourceString = Utils.GetResourceString("WinForms_SeeInnerException", ex2.InnerException.Message);
			throw new InvalidOperationException(resourceString, ex2.InnerException);
		}
		finally
		{
			m_FormBeingCreated.Remove(typeof(T));
		}
	}
	return Instance;
}
@siegfriedpammer siegfriedpammer added Bug C# Decompiler The decompiler engine itself labels Jun 14, 2020
@dgrunwald
Copy link
Member

This doesn't look like On Error Resume Next.
Maybe this is just a regular VB exception filter?

The issue is that "// Could not convert BlockContainer to single expression" -- the ex2 = ex; assignment definitely happens in the exception filter, but due to our immediately-invoked-lambda hack, the C# compiler isn't clever enough to notice that ex2 is initialized.
Maybe ExpressionBuilder.VisitBlockContainer() needs to re-enable the init flag for any variables assigned within the block?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 30, 2020
ElektroKill added a commit to dnSpyEx/ILSpy that referenced this issue Aug 9, 2021
…ression" workaround produces uncompilable code - potentially unassigned variables.
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