Skip to content
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

Returning a ReadOnlySpan<byte> from a LibraryImport fails #96525

Closed
ThadHouse opened this issue Jan 5, 2024 · 4 comments · Fixed by #101095
Closed

Returning a ReadOnlySpan<byte> from a LibraryImport fails #96525

ThadHouse opened this issue Jan 5, 2024 · 4 comments · Fixed by #101095
Assignees
Labels
area-System.Runtime.InteropServices in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@ThadHouse
Copy link
Contributor

Description

Returning a ReadOnlySpan from a call generated by LibraryImport fails to compile, even though it should. It correctly works with a byte[] as the return type instead of ReadOnlySpan

Reproduction Steps

Compile the following code definition

    [LibraryImport("wpiutil", EntryPoint = "WPI_GetResource_wpilib_128_png")]
    [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
    [return: MarshalUsing(CountElementName = nameof(len))]
    public static unsafe partial ReadOnlySpan<byte> GetResourceWpilib128Png(out nuint len);

Expected behavior

The code should compile

Actual behavior

The code fails with the following error

C:\Users\thadh\Documents\GitHub\robotdotnet\WPILib\src\wpiutil\Natives\ImagesNative.cs(13,53): error SYSLIB1051: The specified parameter needs to be marshalled from unmanaged to managed, but the marshaller type 'global::System.Runtime.InteropServices.Marshalling.ReadOnlySpanMarshaller<,>' does not support it. The generated source will not handle marshalling of the return value of method 'GetResourc
eWpilib128Png'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1051) [C:\Users\thadh\Documents\GitHub\robotdotnet\WPILib\src\wpiutil\wpiutil.csproj]
C:\Users\thadh\Documents\GitHub\robotdotnet\WPILib\src\wpiutil\Microsoft.Interop.LibraryImportGenerator\Microsoft.Interop.LibraryImportGenerator\LibraryImports.g.cs(574,68): error CA1420: Managed parameter or return types require runtime marshalling to be enabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1420) [C:\Users\thadh\Documents\GitHub\robotdotnet\WPILib\ 
src\wpiutil\wpiutil.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jan 5, 2024
@ghost
Copy link

ghost commented Jan 5, 2024

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

Issue Details

Description

Returning a ReadOnlySpan from a call generated by LibraryImport fails to compile, even though it should. It correctly works with a byte[] as the return type instead of ReadOnlySpan

Reproduction Steps

Compile the following code definition

    [LibraryImport("wpiutil", EntryPoint = "WPI_GetResource_wpilib_128_png")]
    [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
    [return: MarshalUsing(CountElementName = nameof(len))]
    public static unsafe partial ReadOnlySpan<byte> GetResourceWpilib128Png(out nuint len);

Expected behavior

The code should compile

Actual behavior

The code fails with the following error

C:\Users\thadh\Documents\GitHub\robotdotnet\WPILib\src\wpiutil\Natives\ImagesNative.cs(13,53): error SYSLIB1051: The specified parameter needs to be marshalled from unmanaged to managed, but the marshaller type 'global::System.Runtime.InteropServices.Marshalling.ReadOnlySpanMarshaller<,>' does not support it. The generated source will not handle marshalling of the return value of method 'GetResourc
eWpilib128Png'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1051) [C:\Users\thadh\Documents\GitHub\robotdotnet\WPILib\src\wpiutil\wpiutil.csproj]
C:\Users\thadh\Documents\GitHub\robotdotnet\WPILib\src\wpiutil\Microsoft.Interop.LibraryImportGenerator\Microsoft.Interop.LibraryImportGenerator\LibraryImports.g.cs(574,68): error CA1420: Managed parameter or return types require runtime marshalling to be enabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1420) [C:\Users\thadh\Documents\GitHub\robotdotnet\WPILib\ 
src\wpiutil\wpiutil.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8

Other information

No response

Author: ThadHouse
Assignees: -
Labels:

area-System.Runtime.InteropServices

Milestone: -

@AaronRobinsonMSFT
Copy link
Member

@jtschuster Can you take a look at this?

@jtschuster jtschuster self-assigned this Jan 5, 2024
@ThadHouse
Copy link
Contributor Author

Talking with @jkoritzinsky this is expected behavior, since the ReadOnlySpan can't be written to later in the source generator. So not a bug.

@AaronRobinsonMSFT
Copy link
Member

Talking with @jkoritzinsky this is expected behavior, since the ReadOnlySpan can't be written to later in the source generator. So not a bug.

Agreed on it being by-design for now. I'm going to recommend we look at how to make this more natural in this release. The model should have a way to express both a read-only and read-write concept with respect to types. This is a low-priority issue, but definitely will a win for expressing intent for interop scenarios.

@AaronRobinsonMSFT AaronRobinsonMSFT removed the untriaged New issue has not been triaged by the area owner label Jan 5, 2024
@AaronRobinsonMSFT AaronRobinsonMSFT modified the milestones: Future, 9.0.0 Jan 5, 2024
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Apr 16, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices in-pr There is an active PR which will close this issue when it is merged
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants