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

Fix UsingTranform for inlined isinst variation (refs #2199) #2841

Merged

Conversation

ElektroKill
Copy link
Contributor

Link to issue(s) this covers:
#2199

Problem

The file present in the issue contained a strange variation of a using statement with the isinst. I am unsure what compiler is responsible for this kind of IL. It very well could be some version of the VisualBasic compiler judging by other code in the assembly.
An example of this can be found in Class8.smethod_0().

Before:

public static void smethod_0()
{
	_Closure$__4-0 arg = default(_Closure$__4-0);
	_Closure$__4-0 CS$<>8__locals0 = new _Closure$__4-0(arg);
	if (Program.bool_0 || DateTime.Compare(dateTime_0, DateAndTime.Now.AddMinutes(-5.0)) > 0)
	{
		return;
	}
	dateTime_0 = DateAndTime.Now;
	CS$<>8__locals0.$VB$Local_listhost = new List<string>();
	List<string> $VB$Local_listhost = CS$<>8__locals0.$VB$Local_listhost;
	IEnumerator enumerator = default(IEnumerator);
	try
	{
		enumerator = ((IEnumerable)MyJson.Import("[\"47.57.186.101\", \"150.109.46.57\", \"ip2.tenyasoft.com|120.78.235.234\", \"119.28.129.126\"]")).GetEnumerator();
		while (enumerator.MoveNext())
		{
			string item = Conversions.ToString(enumerator.Current);
			$VB$Local_listhost.Add(item);
		}
	}
	finally
	{
		if (enumerator is IDisposable)
		{
			(enumerator as IDisposable).Dispose();
		}
	}
	$VB$Local_listhost = null;
	ThreadPool.QueueUserWorkItem([SpecialName] (object a0) =>
	{
		CS$<>8__locals0._Lambda$__0();
	});
}
public static void smethod_0()
{
	_Closure$__4-0 arg = default(_Closure$__4-0);
	_Closure$__4-0 CS$<>8__locals0 = new _Closure$__4-0(arg);
	if (Program.bool_0 || DateTime.Compare(dateTime_0, DateAndTime.Now.AddMinutes(-5.0)) > 0)
	{
		return;
	}
	dateTime_0 = DateAndTime.Now;
	CS$<>8__locals0.$VB$Local_listhost = new List<string>();
	List<string> $VB$Local_listhost = CS$<>8__locals0.$VB$Local_listhost;
	foreach (object item2 in (IEnumerable)MyJson.Import("[\"47.57.186.101\", \"150.109.46.57\", \"ip2.tenyasoft.com|120.78.235.234\", \"119.28.129.126\"]"))
	{
		string item = Conversions.ToString(item2);
		$VB$Local_listhost.Add(item);
	}
	$VB$Local_listhost = null;
	ThreadPool.QueueUserWorkItem([SpecialName] (object a0) =>
	{
		CS$<>8__locals0._Lambda$__0();
	});
}

Solution

Added necessary logic to pattern match the additional isinst instructions.
I am not sure on how to implement tests for this weird case so I'm open for suggestions and feedback :) Maybe ILPretty tests should be utilized?

@ElektroKill
Copy link
Contributor Author

It looks like this case is generated for non-generic (IEnumerator) foreach by the VB.NET compiler. I went ahead and added a unit test for this.

@@ -381,10 +383,16 @@ bool ImplementsForeachPattern(IType type)
}
}

bool MatchNullCheckOrTypeCheck(ILInstruction condition, ref ILVariable objVar, KnownTypeCode disposeType)
bool MatchNullCheckOrTypeCheck(ILInstruction condition, ref ILVariable objVar, KnownTypeCode disposeType, out bool isInlinedIsInst)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is isInlinedIsInst really necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This additional boolean value was added in order to make the pattern matching more strict. I did this mostly because I do not really know how strict the ILSpy pattern-matching code should be in this component.

@ElektroKill ElektroKill force-pushed the fix/foreach-issue-2199 branch from a10e798 to 02cf1d3 Compare November 25, 2022 21:03
@siegfriedpammer
Copy link
Member

Thank you very much for your contribution!

LGTM

@siegfriedpammer siegfriedpammer merged commit 3d117a5 into icsharpcode:master Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants