Skip to content

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

@ThadHouse

Description

@ThadHouse

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions