-
Notifications
You must be signed in to change notification settings - Fork 5k
Add custom exception handlers to generated COM wrappers #114828
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
base: main
Are you sure you want to change the base?
Conversation
This is an initial iteration to add custom exception handlers to generated COM wrappers, through a new ExceptionToUnmanagedMarshaller property in GeneratedComWrappersAttribute Fix dotnet#109522
Note regarding the
|
1 similar comment
Note regarding the
|
Tagging subscribers to this area: @dotnet/interop-contrib |
@dotnet-policy-service agree |
src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs
Outdated
Show resolved
Hide resolved
Resolves an issue with previous implementation, highlighted by Jeremy. The previous implementation is overcomplicated and hooks into the default exception logic, which is not ideal. This commit copies the existing custom exception marshalling logic from the VTable stub generator.
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.
Just a few nits, but looking much better!
...s/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/ISymbolExtensions.cs
Outdated
Show resolved
Hide resolved
...ies/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/BoundGenerators.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Jeremy Koritzinsky <jkoritzinsky@gmail.com>
Co-authored-by: Jeremy Koritzinsky <jkoritzinsky@gmail.com>
...stem.Runtime.InteropServices/gen/ComInterfaceGenerator/GeneratedComInterfaceAttributeData.cs
Outdated
Show resolved
Hide resolved
{ | ||
if (exceptionToUnmanagedMarshaller.Value is not INamedTypeSymbol) | ||
{ | ||
return null; |
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.
We generally don't use null
for logical flow. This would mean the return type should be updated. I don't think we should be failing in this case as it will silent and difficult to reason about. A diagnostic would be more appropriate.
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.
This was based on InteropAttributeDataExtensions.WithValuesFromNamedArguments
, which returns null
if an invalid StringMarshalling
type is passed. I'm not sure how this is handled particularly by GeneratedComInterfaceAttributeData
.
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.
I think this is another one of the cases where we use null
as our "unable to parse" sentinel (and in the caller we check for null and emit a diagnostic and replace with a sentinel). Can you confirm?
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.
Unfortunately, it looks like there's no regards to null-checking here. Even today if you pass in an invalid type for StringMarshallingCustomType
, i.e.
[GeneratedComInterface(StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(string*))]
public partial interface Foo;
the generator fails with a generic error message from Roslyn:
Generator 'ComInterfaceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'NullReferenceException' with message 'Object reference not set to an instance of an object.'
...stem.Runtime.InteropServices/gen/ComInterfaceGenerator/GeneratedComInterfaceAttributeData.cs
Show resolved
Hide resolved
Co-authored-by: Aaron Robinson <arobins@microsoft.com>
Fixes #109522