dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.24k stars 4.73k forks source link

Improve SYSLIB1051 by marking the fields that are incompatible with LibraryImport #99850

Open silkfire opened 9 months ago

silkfire commented 9 months ago

Analyzer

Diagnostic ID: SYSLIB1051

Describe the improvement

The analyzer tells you if you're trying to marshal a struct whose fields are incompatible with the LibraryImport (source generated P/Invoke added in .NET 7) attribute.

It would be extremely useful if the analyzer informed the developer which specific fields are incompatible for marshalling.

Right now it just says The type '<struct type>' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'pstruct'. If you remove the offending field(s) from the struct, the error goes away.

Describe suggestions on how to achieve the rule

Add to the description of the analyzer a list of let's say max 5 fields that are incompatible for marshalling. These are types that are not blittable, i.e. reference types, string, arrays and bool.

Just as the current implementation, this should work recursively, i.e. be able to support nested structs; an unsupported field may be found in a deeper struct hierarchy but should still be reported.

dotnet-policy-service[bot] commented 7 months ago

Tagging subscribers to this area: @dotnet/interop-contrib See info in area-owners.md if you want to be subscribed.

jkoritzinsky commented 3 months ago

Due to how we have structured our analysis engine, this is actually much more difficult to implement than it seems. (Our system is pluggable and more of an opt-in/"determine type is marshallable" model than an opt-out/"determine type is unmarshallable" model).

Moving this to .NET 10 for now.