Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

agocke
Copy link
Member

@agocke agocke commented Apr 18, 2025

Fixes #113590

Copy link
Contributor

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

@agocke agocke marked this pull request as ready for review April 18, 2025 22:21
@Copilot Copilot AI review requested due to automatic review settings April 18, 2025 22:21
Copy link
Contributor

@Copilot Copilot AI left a 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

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")
});
}

Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using [LibraryImport] with a generic delegate parameter (e.g. Func<int, int>) causes an exception
2 participants