-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Better support for unmanaged-like types #76928
Comments
I've realized that |
I don't know how we'd implement |
Compiler doesn't define or enforce type layout. Anyone can implement what they think runtime is going to do in terms of layout for a particular type (I think some of the aspects aren't documented and some are runtime version dependent). From the language point of view and for the purpose of compiling a program, compiler doesn't need to figure this out. Therefore, I do not think it is appropriate to provide an API like that directly on a type symbol. |
Thanks Aleksey. I'm going to close this out as not planned. |
I'm asking for Roslyn to expose the layout kind from IL metadata, as specified in ECMA-335 I.9.5. How the runtime determines the final layout is a separate matter. From the point of view of a library writer: unmanaged types have predictable layout since .NET 7, which I rely for critical optimizations in serialization / interop. The only pitfall is |
Thank you for the clarification. I guess we have at least two options:
@vmilea Please confirm that either option is going to address your scenario. |
Thanks, the first option is sufficient.
|
@vmilea can you please update your post by spelling out the exact API shape you're proposing? |
Background
Recent efforts have improved the predictability of layout in managed memory (for example, non-blittable
bool
/char
fields no longer break sequential layout on the managed side). This enables high-performance serialization and interop without runtime marshalling or explicit layout attributes.Motivation
I'm building an API that that takes blittable types:
"Blittable" in this case means
unmanaged
or nullableunmanaged
. Yes,Nullable<T> where T : unmanaged
is in fact blittable, let's move on. :)Because
Nullable<T>
doesn't satisfy thestruct
/unmanaged
constraints, you end up defining multiple overloads:This becomes exponentially worse the more blittable parameters there are:
So I decided to leave
T
unconstrained and instead throw an exception when theRuntimeHelpers.IsReferenceOrContainsReferences<T>()
intrinsic returns false. This approach detects issues at runtime with zero overhead, but I'd like to add an analyzer error in case of misuse.Proposed API
A couple of additions to the Roslyn API would help:
ITypeSymbol.IsReferenceOrContainsReferences
: Equivalent toRuntimeHelpers.IsReferenceOrContainsReferences
. Can already be done by checking all fields in a struct recursively, but should be faster through a dedicated API.INamedTypeSymbol.LayoutKind
: So the analyzer can warn against auto layout, which is currently tricky to infer (e.g. can't assume sequential layout for a struct lackingStructLayoutAttribute
because it might be a forwarded type likeValueTuple
and it has aTypeForwardedFromAttribute
instead of the original attributes).Risks
Can't see any.
The text was updated successfully, but these errors were encountered: