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

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

Open
jkoritzinsky opened this issue Aug 9, 2024 · 0 comments
Labels
Area-Compilers Feature Request untriaged Issues and PRs which have not yet been triaged by a lead
Milestone

Comments

@jkoritzinsky
Copy link
Member

Summary

Provide compiler support for the new ExtendedLayoutAttribute approved in dotnet/runtime#100896 (to be implemented in .NET 10) by emitting [StructLayout(LayoutKind.Extended)] automatically in metadata and not showing [StructLayout(LayoutKind.Extended)] when decompiling metadata.

Background and Motivation

For .NET 10, the interop team has a new API to provide an extensible model for type layout of struct types. We will implement similarly to how we extended DllImportAttribute.CallingConvention, by using the last available bit in StructLayoutAttribute​'s LayoutKind​ to specify "look at this other attribute".

As part of this design, we noticed that the C# (and possibly VB) compilers validate the LayoutKind​ value that is passed into the StructLayoutAttribute​ to ensure it is one of the known values. As a result, we'd need to update Roslyn to allow this bit. To provide a cleaner experience, we'd like to do something slightly more involved. We'd like to enable the C# compiler to emit the correct metadata bit (1​, which we'll define as LayoutKind.Extended​) on the type when the new System.Runtime.InteropServices.ExtendedLayoutAttribute​ is applied.

Proposed Feature

When System.Runtime.InteropServices.ExtendedLayoutAttribute is applied to a type definition, the System.Runtime.InteropServices.StructLayoutAttribute pseudo-attribute is considered applied and the layoutKind is set to 1.

If a System.Runtime.InteropServices.StructLayoutAttribute is applied to a type with a layoutKind of 1 or LayoutKind.Extended explicitly, an error is issued.

If a type is imported with this layout kind from a NoPIA scenario, it the compiler will emit an error and not embed the type.

Alternative Designs

Alternatively, we only enable Roslyn to allow the LayoutKind.Extended value to be passed to System.Runtime.InteropServices.StructLayoutAttribute. In this alternative, any time the new attribute is used, StructLayoutAttribute would also have to be applied for it to have any effect. This would provide a worse UX but be cheaper to implement.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Aug 9, 2024
@jaredpar jaredpar added this to the .NET 10 milestone Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature Request untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants