roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Implement compiler support for System.Runtime.InteropServices.ExtendedLayoutAttribute

Open jkoritzinsky opened this issue 6 months ago • 8 comments

The interop team has decided to add a new mechanism to specify type layouts, designed similarly to how the UnmanagedCallersOnly feature was designed. In particular, the team has decided to use the last remaining value in the "Layout Kind" mask of a type def's attributes to represent "extended layout". When this value it set, the runtime will look for the System.Runtime.InteropServices.ExtendedLayoutAttribute to provide all layout information.

As part of the design process, we noticed that Roslyn blocks emitting a type with the last remaining value of the System.Runtime.InteropServices.LayoutKind enumeration in the StructLayout pseudo-attribute as the compiler can't map it to a valid TypeAttributes value.   The System.Runtime.InteropServices.ExtendedLayoutAttribute type was approved in https://github.com/dotnet/runtime/issues/100896 and has the first part of runtime implementation (the TypeAttributes bit and the LayoutKind value) in https://github.com/dotnet/runtime/pull/116082.

The support in C# is to allow the user to specify the ExtendedLayoutAttribute on a type. When they do so, the correct layout bit will be set. The user cannot manually specify the new "extended layout" value. The only way in C# to specify that value is to put this attribute on the type.

The ExtendedLayoutAttribute will be embedded in NoPia scenarios as the layout bits are embedded in NoPia scenarios.

This should go into .NET 11

jkoritzinsky avatar May 28 '25 23:05 jkoritzinsky

@jkoritzinsky think we'll need a small specification document for this contribution.

jaredpar avatar May 29 '25 16:05 jaredpar

I'll put one together. Where is the best place for it to go?

jkoritzinsky avatar May 29 '25 16:05 jkoritzinsky

I'll put one together. Where is the best place for it to go?

dotnet/csharplang. @333fred can you think of a good example that would be similar to this?

jaredpar avatar May 29 '25 22:05 jaredpar

I considered doing a proposal in csharplang, but there's zero specifications of StructLayoutAttribute or FieldOffset on csharplang or csharpstandard.

jkoritzinsky avatar May 29 '25 22:05 jkoritzinsky

I would agree with Jeremy, C# the language doesn't care about those attributes, just Roslyn the compiler. Perhaps under docs/features? Also, what support does VB have for these attributes, and what will we need to update in its support?

333fred avatar May 29 '25 22:05 333fred

Hmm ... for some reason I thought the spec discussed StructLayout but looking now it does not. Let's just drop a spec doc/features then.

jaredpar avatar May 29 '25 23:05 jaredpar

I'll drop a spec in docs/features and add VB support.

jkoritzinsky avatar May 30 '25 18:05 jkoritzinsky

I've added a spec and added VB support

jkoritzinsky avatar May 30 '25 21:05 jkoritzinsky