Create error on LibraryImport of generic delegate#114827
Merged
agocke merged 3 commits intodotnet:mainfrom May 14, 2025
Merged
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/interop-contrib |
Contributor
There was a problem hiding this comment.
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
Comment on lines
25
to
49
| [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") | ||
| }); | ||
| } | ||
|
|
Member
There was a problem hiding this comment.
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.
This was referenced Apr 19, 2025
jkoritzinsky
approved these changes
Apr 23, 2025
This was referenced Apr 24, 2025
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #113590