Skip to content

Silence some marshalling warnings #690

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

Merged

Conversation

MichalStrehovsky
Copy link
Member

  • Call into PInvokeMarshal from the generated marshallers. These are all analyzed at compile time and safe. Calling directly into implementation sidesteps the warnings and provides a mild performance boost. Only the GetFunctionPointerForDelegate part is actually needed. The other was done for consistency.
  • Silence warnings on generic things. I didn't see this before because these are generic and they weren't instantiated anywhere I was looking at.

* Call into PInvokeMarshal from the generated marshallers. These are all analyzed at compile time and safe. Calling directly into implementation sidesteps the warnings and provides a mild performance boost. Only the `GetFunctionPointerForDelegate` part is actually needed. The other was done for consistency.
* Silence warnings on generic things. I didn't see this before because these are generic and they weren't instantiated anywhere I was looking at.
@MichalStrehovsky MichalStrehovsky added the area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation label Feb 15, 2021
@@ -582,6 +584,8 @@ public static void PtrToStructure<T>(IntPtr ptr, [DisallowNull] T structure)
PtrToStructure(ptr, (object)structure!);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need UnconditionalSuppressMessage on void PtrToStructure<T>(IntPtr ptr, [DisallowNull] T structure) too?

@@ -582,6 +584,8 @@ public static void PtrToStructure<T>(IntPtr ptr, [DisallowNull] T structure)
PtrToStructure(ptr, (object)structure!);
}

[UnconditionalSuppressMessage("AotAnalysis", "IL9700:AotUnfriendlyApi",
Justification = "AOT compilers can see the T.")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether it would be better to call PtrToStructureHelper instead of suppressing the warning. Some of the argument checks would need to be duplicated, but we would be also able to omit some.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(similar to other places like GetDelegateForFunctionPointer)

throw new ArgumentException(SR.Arg_MustBeDelegate, nameof(TDelegate));
}

return (TDelegate)(object)GetDelegateForFunctionPointerInternal(ptr, t);
}

[RequiresDynamicCode("Marshalling code for the delegate might not be available. Use the GetFunctionPointerForDelegate<TDelegate> overload instead.")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also need to update public static IntPtr GetFunctionPointerForDelegate<TDelegate>(TDelegate d) where TDelegate : notnull to not produce warning?

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

throw new ArgumentException(SR.Arg_MustBeDelegate, nameof(TDelegate));
}

return (TDelegate)(object)GetDelegateForFunctionPointerInternal(ptr, t);
}

[RequiresDynamicCode("Marshalling code for the delegate might not be available. Use the GetFunctionPointerForDelegate<TDelegate> overload instead.")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I just tried this and noticed that there is a double-dot printed in the warning message Use the GetFunctionPointerForDelegate<TDelegate> overload instead... Should we stop appending . to the message in ReflecitonMethodBodyScanner.cs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll never be consistent with this. It might be better to just fix this up when the message is generated (append the dot only if it's not there).

@MichalStrehovsky MichalStrehovsky merged commit 67816c4 into dotnet:feature/NativeAOT Feb 17, 2021
@MichalStrehovsky MichalStrehovsky deleted the marshalwarnings branch February 17, 2021 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants