Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jkoritzinsky
Copy link
Member

@jkoritzinsky jkoritzinsky commented May 28, 2025

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 dotnet/runtime#100896 and has the first part of runtime implementation (the TypeAttributes bit and the LayoutKind value) in dotnet/runtime#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 jkoritzinsky requested a review from a team as a code owner May 28, 2025 23:50
@jaredpar
Copy link
Member

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

@jkoritzinsky
Copy link
Member Author

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

@jaredpar
Copy link
Member

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?

@jkoritzinsky
Copy link
Member Author

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

@333fred
Copy link
Member

333fred commented May 29, 2025

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?

@jaredpar
Copy link
Member

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.

@jkoritzinsky
Copy link
Member Author

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

@jkoritzinsky
Copy link
Member Author

I've added a spec and added VB support

Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done an initial review pass.

@jkoritzinsky jkoritzinsky requested a review from 333fred December 11, 2025 02:04
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@jcouv jcouv Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkoritzinsky Add the following line beneath "extension indexers" in the feature status page.
| [ExtendedLayoutAttribute](https://github.com/dotnet/runtime/issues/100896) | main | [Merged into 18.3](https://github.com/dotnet/roslyn/pull/78741) | [jkoritzinsky](https://github.com/jkoritzinsky) | [333fred](https://github.com/333fred), [jcouv](https://github.com/jcouv) | | |

Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done review pass (commit 25)

public float f;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: extra whitespace

@jkoritzinsky jkoritzinsky requested a review from 333fred December 15, 2025 21:14
…nch 'main' of github.com:dotnet/roslyn into extended-layout
@333fred
Copy link
Member

333fred commented Dec 18, 2025

This is generally looking ok to me. @dotnet/roslyn-compiler for a second review.

@jcouv jcouv self-assigned this Dec 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C# compiler integration with new System.Runtime.InteropServices.ExtendedLayoutAttribute

5 participants