Skip to content

Cannot use a result of 'MemoryMarshal.CreateSpan<char>(ref char, int)' in this context because it may expose variables referenced by parameter 'reference' outside of their declaration scope #637

@virzak

Description

@virzak

Actual behavior

Compilation fails when using C# preview and msbuild.

  CompilerServer: server failed - server rejected the request due to analyzer / generator issues 'analyzer assembly 'C:\Users\virzak\.nuget\packages\microsoft.windows.cswin32\0.2.10-beta\analyzers\cs\System.Runtime.CompilerServices.Unsafe.dll' has MVID 'd22a1fa6-8134-4c26-bb36-e67aceee55b8' but loaded assembly 'System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' has MVID 'bc84029b-1123-4266-bc37-9616e5f03999'' - 3ab8afc4-73d7-4553-b014-16f9a787539b
Q:\Code\Sandbox\SetupDiGetDeviceInterfaceDetail\Microsoft.Windows.CsWin32\Microsoft.Windows.CsWin32.SourceGenerator\Windows.Win32.SP_DEVICE_INTERFACE_DETAIL_DATA_W.g.cs(47,66): error CS8170: Struct members cannot return 'this' or other instance members by reference [Q:\Code\Sandbox\SetupDiGetDeviceInterfaceDetail\SetupDiGetDeviceInterfaceDetail.csproj]
Q:\Code\Sandbox\SetupDiGetDeviceInterfaceDetail\Microsoft.Windows.CsWin32\Microsoft.Windows.CsWin32.SourceGenerator\Windows.Win32.SP_DEVICE_INTERFACE_DETAIL_DATA_W.g.cs(47,37): error CS8347: Cannot use a result of 'MemoryMarshal.CreateSpan<char>(ref char, int)' in this context because it may expose variables referenced by parameter 'reference' outside of their declaration scope [Q:\Code\Sandbox\SetupDiGetDeviceInterfaceDetail\SetupDiGetDeviceInterfaceDetail.csproj]
Done Building Project "Q:\Code\Sandbox\SetupDiGetDeviceInterfaceDetail\SetupDiGetDeviceInterfaceDetail.csproj" (default targets) -- FAILED.

Expected behavior

A clear and concise description of what you expected to happen.

Repro steps

  1. NativeMethods.txt content:
SetupDiGetDeviceInterfaceDetail

Run the following to reproduce:

dotnet new classlib -o SetupDiGetDeviceInterfaceDetail --langVersion preview
dotnet add SetupDiGetDeviceInterfaceDetail package Microsoft.Windows.CsWin32 --version 0.2.10-beta
echo "SetupDiGetDeviceInterfaceDetail" > SetupDiGetDeviceInterfaceDetail/NativeMethods.txt
msbuild .\SetupDiGetDeviceInterfaceDetail\ /p:Runtimeidentifier=win-x64 /restore
  1. Any of your own code that should be shared?

Context

  • CsWin32 version: 0.2.10-beta
  • Target Framework: net6.0, net7.0
  • LangVersion (if explicitly set by project): 11 preview

Either removing preview flag, or compiling with dotnet doesn't produce this issue

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions