microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2k stars 84 forks source link

.NET 8 has CS9195 warnings for generated code #1014

Closed JeremyKuhne closed 10 months ago

JeremyKuhne commented 11 months ago

Actual behavior

.NET 8 has changed a number of APIs from ref to in. This causes warnings in CsWin32 generated code. For example:

\winforms2\src\System.Windows.Forms.Primitives\src\Microsoft.Windows.CsWin32\Microsoft.Windows.CsWin32.SourceGenerator\Windows.Win32.RGBQUAD.InlineArrays.g.cs(47,144): warning CS9195: Argument 1 should be passed with the 'in' keyword [\winforms2\src\System.Windows.Forms.Primitives\src\System.Windows.Forms.Primitives.csproj]

Expected behavior

Conditionalized generation to use in when targeting .NET or higher. Something like this (example from System.Drawing):

#if NET8_0_OR_GREATER
    internal unsafe Span<winmdroot.Graphics.Gdi.RGBQUAD> AsSpan() => MemoryMarshal.CreateSpan(in _0, SpanLength);
#else
    internal unsafe Span<winmdroot.Graphics.Gdi.RGBQUAD> AsSpan() => MemoryMarshal.CreateSpan(ref _0, SpanLength);
#endif

Repro steps

Target any daily drop of .NET 8 after 8/3 and include BITMAPINFO (there are a number of others that fail).

Context

AArnott commented 11 months ago

@JeremyKuhne Your repro steps appear to be to generate RGBQUAD, but that is merely a struct that defines no methods at all. Can you give me a better API to generate to emit the warning?

AArnott commented 11 months ago

FWIW, I can't find anything in CsWin32 that would ever generate Marshal.QueryInterface method calls.

JeremyKuhne commented 11 months ago

BITMAPINFO generates this particular RGBQUAD inline array wrapper.

AArnott commented 10 months ago

I'm able to repro this in a unit test now. Woot.