-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Report a diagnostic for return types with HResult-like named structures and provide a code-fix to do the correct marshalling #90282
Report a diagnostic for return types with HResult-like named structures and provide a code-fix to do the correct marshalling #90282
Conversation
…ther assemblies (i.e. a shared interop assembly that is used by various different projections)
Tagging subscribers to this area: @dotnet/interop-contrib Issue DetailsMany developers use struct based HRESULT types to provide a more helpful API interface on top of HRESULT-based errors. This is technically incorrect per the ABI, but due to prexisting behavior, we couldn't fix this in bulit-in COM. In source-generated COM, we fixed this from the outset, but we didn't provide a good way for developers to determine that this change is the cause of runtime failures. This PR implements a feature where Fixes #89603
|
...libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/Marshallers/Marshaller.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime.InteropServices/gen/Common/ConvertToSourceGeneratedInteropFixer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime.InteropServices/gen/Common/ConvertToSourceGeneratedInteropFixer.cs
Outdated
Show resolved
Hide resolved
...em.Runtime.InteropServices/gen/ComInterfaceGenerator/Analyzers/AddMarshalAsToElementFixer.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, my only other thought is if we want to add a warning that the struct should be 4 bytes for the bitcast.
I don't think we need to add a warning here as likely everyone that will use this will have a struct that just has one int field in it. I'd be shocked if anyone used any other HRESULT type shape. A runtime error is fine here I think. |
Many developers use struct based HRESULT types to provide a more helpful API interface on top of HRESULT-based errors. This is technically incorrect per the ABI, but due to prexisting behavior, we couldn't fix this in bulit-in COM. In source-generated COM, we fixed this from the outset, but we didn't provide a good way for developers to determine that this change is the cause of runtime failures.
This PR implements a feature where
MarshalAs(UnmanagedType.Error)
on a value type in a COM interface marshals the type as an int at the ABI boundary. We will provide a diagnostic when this may be useful and a code fix to add the attribute. Additionally, we also enlighten the "Convert to source-generated COM" fixer to add the attribute when it is likely to be helpful.Fixes #89603