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

Feature 'byref locals and returns' is not available in C# 6. #2115

Closed
greenozon opened this issue Aug 22, 2020 · 4 comments
Closed

Feature 'byref locals and returns' is not available in C# 6. #2115

greenozon opened this issue Aug 22, 2020 · 4 comments
Labels
Bug Decompiler The decompiler engine itself

Comments

@greenozon
Copy link

greenozon commented Aug 22, 2020

ILSpy uses byref feature even in older C# lang versions, which does not know about it.

Proposal is to use this feature only if C#7 and higher is selected, if not - do not use byref feature

Example

Erroneous output

image

	Vector2[] array = new Vector2[points.Count - 1];
	for (int i = 1; i < points.Count; i++)
	{
		ref Vector2 reference = ref array[i - 1];
		reference = new Vector2(GetCoord(points[i].X), GetCoord(points[i].Y));
	}

If the output fails to re-compile, provide the compiler error message.
If the output has the wrong behavior, explain how it differs from the expected behavior.

image

Details

  • Product in use: ILSpy version 6.1.0.5902
@greenozon greenozon added Bug Decompiler The decompiler engine itself labels Aug 22, 2020
@dgrunwald
Copy link
Member

And what code do you propose ILSpy should emit instead?

Note that the obvious

array[i - 1] = new Vector2(GetCoord(points[i].X), GetCoord(points[i].Y));

would be incorrect: the IL code does not call GetCoord in case of an null reference or index out of bounds exception, whereas the C# code with the direct assignment would call GetCoord prior to the array access.

@greenozon
Copy link
Author

greenozon commented Aug 22, 2020

Looked into dnSPy, it produced following result

image

Vector2 as well as other types are from opensource library SharpDX

GetCoord returns float:

private float GetCoord(float coord)

@dgrunwald
Copy link
Member

dnSpy has a bug there, we fixed that in #2050.

@dgrunwald
Copy link
Member

Maybe we should reintroduce the #2050 bug when a language version <C# 6 is selected?
We'll emit incorrect code but at least it's compilable; and the difference rarely matters.

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

No branches or pull requests

2 participants