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
1.99k stars 84 forks source link

CsWin32 should avoid Unsafe.SkipInit when it doesn't exist in the compilation #1199

Closed AArnott closed 2 weeks ago

AArnott commented 4 weeks ago

Discussed in https://github.com/microsoft/CsWin32/discussions/1191

Originally posted by **thxkiwi** May 31, 2024 I have a project that I wrote using targeting framework net80 and trying to get it working with net481/LangVersion 10. CsWin32: 0.3.106 Using Visual Studio 17.10.0. I've modified the SDK project files to be ``` net481 enable enable x64 10 true ``` CsWin32 is generating Windows.Win32.char.InlineArrays.g.cs:84 - `Unsafe.SkipInit(out __char_260 result);` ``` public static unsafe implicit operator __char_260(ReadOnlySpan value) { Unsafe.SkipInit(out __char_260 result); Span target = new Span(result.Value, SpanLength); value.CopyTo(target); int initLength = value.Length; target.Slice(initLength, SpanLength - initLength).Clear(); return result; } ``` That seems to be .NET 8 only still. Am I missing something (including "the point") here? It seemed possible and reasonable to target net481 and LangVersion 10. Help and guidance greatly appreciated.
AArnott commented 4 weeks ago

Per the discussion this came from, the issue seems to be that Unsafe.SkipInit comes from a package that wasn't referenced in the compilation. CsWin32 should be more cautious about this.