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

Report a diagnostic for return types with HResult-like named structures and provide a code-fix to do the correct marshalling #90282

Merged
merged 10 commits into from
Aug 10, 2023

Conversation

jkoritzinsky
Copy link
Member

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

@ghost
Copy link

ghost commented Aug 9, 2023

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

Issue Details

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

Author: jkoritzinsky
Assignees: -
Labels:

area-System.Runtime.InteropServices

Milestone: 8.0.0

@ghost ghost assigned jkoritzinsky Aug 9, 2023
Copy link
Member

@jtschuster jtschuster left a 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.

@jkoritzinsky
Copy link
Member Author

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.

@jkoritzinsky jkoritzinsky merged commit d303781 into dotnet:main Aug 10, 2023
113 checks passed
@jkoritzinsky jkoritzinsky deleted the preservesig-hresult-struct branch August 10, 2023 18:14
@ghost ghost locked as resolved and limited conversation to collaborators Sep 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ComInterfaceGenerator should warn on PreserveSig methods that return a struct named HResult
2 participants