Skip to content

Ref locals before C# 7 #3468

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

Open
ds5678 opened this issue May 5, 2025 · 2 comments
Open

Ref locals before C# 7 #3468

ds5678 opened this issue May 5, 2025 · 2 comments
Labels
Bug Decompiler The decompiler engine itself

Comments

@ds5678
Copy link
Contributor

ds5678 commented May 5, 2025

Input code

public struct StructWithStringField
{
	public string Id;
	public static void ModifyArray(StructWithStringField[] array)
	{
		int num = 0;
		while (num < 10)
		{
			array[0].Id = (++num).ToString();
		}
	}
}

Erroneous output

public struct StructWithStringField
{
	public string Id;
	public static void ModifyArray(StructWithStringField[] array)
	{
		int num = 0;
		while (num < 10)
		{
			ref StructWithStringField reference = ref array[0];
			int num2 = ++num;
			reference.Id = num2.ToString();
		}
	}
 }

This produces an error before C# 7:

CS1525: Unexpected symbol 'ref'

Details

  • Product in use: pretty unit test
  • Version in use: latest commit (aff9649)
  • Any other relevant information to the issue, or your interest in contributing a fix.
    • I, of course, am willing to work on this.
    • I think I've seen other situations with similar ref local issues, but I don't have any examples at this time. I'm checking to see if I can find some.
@ds5678 ds5678 added Bug Decompiler The decompiler engine itself labels May 5, 2025
@siegfriedpammer
Copy link
Member

This is an ILInlining problem.

@ds5678
Copy link
Contributor Author

ds5678 commented May 6, 2025

I narrowed the problem down with tests that are more specific. However, I'm struggling to figure out how to resolve the problem.

public static string Test2(int value)
{
	return (++value).ToString();
}
public static string Test3(int value)
{
	M(++value);
	return value.ToString();
}
private static void M(in int value)
{
}

I did a lot of debugging, both in ILInlining and TransformAssignment. I could not find a clean way to solve the problem in ILInlining, and my modifications to TransformAssignment (#3472) feel like they're the wrong way to solve this. I could use some guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Decompiler The decompiler engine itself
Projects
None yet
Development

No branches or pull requests

2 participants