-
Notifications
You must be signed in to change notification settings - Fork 5k
Create error on LibraryImport of generic delegate #114827
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
Tagging subscribers to this area: @dotnet/interop-contrib |
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.
Pull Request Overview
This PR introduces a diagnostic error when a LibraryImport method is declared with a generic delegate parameter. The key changes include:
- Adding a new unit test verifying that generic delegate parameters trigger the proper error.
- Updating the marshalling generator resolver to explicitly check for generic delegates and return a not-supported diagnostic.
- Modifying the DelegateTypeInfo record to include an IsGeneric flag for improved type handling.
Reviewed Changes
Copilot reviewed 3 out of 17 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/CompileFails.cs | Adds a unit test for generic delegate parameter support error |
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshalAsMarshallingGeneratorResolver.cs | Updates delegate handling with an additional generic check and returns an appropriate diagnostic |
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/ManagedTypeInfo.cs | Updates DelegateTypeInfo record to include an IsGeneric flag |
Files not reviewed (14)
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/Strings.resx: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.cs.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.de.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.es.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.fr.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.it.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.ja.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.ko.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.pl.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.pt-BR.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.ru.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.tr.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.zh-Hans.xlf: Language not supported
- src/libraries/System.Runtime.InteropServices/gen/Common/Resources/xlf/Strings.zh-Hant.xlf: Language not supported
.../gen/Microsoft.Interop.SourceGeneration/Marshalling/MarshalAsMarshallingGeneratorResolver.cs
Show resolved
Hide resolved
...ies/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/ManagedTypeInfo.cs
Show resolved
Hide resolved
[Fact] | ||
public Task GenericDelegateParameterFails() | ||
{ | ||
var src = """ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
partial class Test | ||
{ | ||
[LibraryImportAttribute("DoesNotExist")] | ||
public static partial void Method1(Func<int, int> {|#0:f|}); | ||
} | ||
"""; | ||
|
||
|
||
return VerifyCS.VerifySourceGeneratorAsync( | ||
src, | ||
new DiagnosticResult[] | ||
{ | ||
VerifyCS.Diagnostic(GeneratorDiagnostics.ParameterTypeNotSupportedWithDetails) | ||
.WithLocation(0) | ||
.WithArguments("Marshalling a generic delegate is not supported. Consider using a function pointer instead.", "f") | ||
}); | ||
} | ||
|
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 know I said that I'd be fine with a custom test here, but seeing what you're writing now, can you move this test to use CodeSnippets.BasicParametersAndModifiers("System.Func<int, int>")
for consistency with the other cases we have below? That way we test all the different types of parameter passing as well as return types.
Fixes #113590