Skip to content

[LibraryImport] throws NotImplementedException on ref readonly parameters #97706

@colejohnson66

Description

@colejohnson66

Description

I am P/Invoking into a method taking a const T* parameter. Historically, this would be typed in the import as a plain T* or an in T. I wanted to use ref readonly to require the "ref" from the caller, so I used ref readonly. However, this causes the source generator to completely fail and generate nothing. If ref readonly is not supported, then it should generate an error, not crash.

Reproduction Steps

[LibraryImport(LIBC, EntryPoint = "cfgetospeed")]
public static partial uint CFGetOutputSpeed(ref readonly TermIOs termios_p);

Expected behavior

An implementation being generated, or at least an error message about unsupported parameter types.

Actual behavior

The source generator throws an exception and generates nothing.

4>CSC: Warning CS8785 : Generator 'LibraryImportGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'NotImplementedException' with message 'Support for some RefKind'.

Regression?

No response

Known Workarounds

Don't use ref readonly, but just ref or in

Configuration

.NET SDK:
 Version:           8.0.101
 Commit:            6eceda187b
 Workload version:  8.0.100-manifests.30fce108

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22631
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.101\

.NET workloads installed:
 Workload version: 8.0.100-manifests.30fce108
There are no installed workloads to display.

Host:
  Version:      8.0.1
  Architecture: x64
  Commit:       bf5e279d92

.NET SDKs installed:
  8.0.101 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  C:\Users\<>\global.json

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions