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

Emit Unsafe.AsRef #2144

Open
wwh1004 opened this issue Sep 3, 2020 · 1 comment
Open

Emit Unsafe.AsRef #2144

wwh1004 opened this issue Sep 3, 2020 · 1 comment
Labels
Enhancement Areas for improvement

Comments

@wwh1004
Copy link
Contributor

wwh1004 commented Sep 3, 2020

Class3.Method6

public unsafe static ref T Method6(ref T a)
{
	return ref *(T*)(long)(IntPtr)InterpreterStub.Dispatch(0, 100663305, new object[1]
	{
		(IntPtr)System.Runtime.CompilerServices.Unsafe.AsPointer(ref a)
	}, new Type[1]
	{
		typeof(T)
	}, null);
}

public static class InterpreterStub
{
	public static object Dispatch(int moduleId, int methodToken, object[] arguments, Type[] typeInstantiation, Type[] methodInstantiation)
	{
	}
}

how about emit Unsafe.AsRef instead of ref *(T*)(long)

stub.zip

@wwh1004 wwh1004 added the Enhancement Areas for improvement label Sep 3, 2020
@dgrunwald
Copy link
Member

Unsafe.AsRef expects a pointer type so we'd have to emit ref Unsafe.AsRef<T>((void*)(long)(IntPtr)InterpreterStub.Dispatch(...)).
That doesn't make the code any more readable.

I guess using Unsafe.AsRef makes sense when converting to a reference to a managed type, where forming the pointer type T* isn't valid. But ILSpy currently doesn't have logic to detect whether a type would satisfy the T: unmanaged constraint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Areas for improvement
Projects
None yet
Development

No branches or pull requests

2 participants