dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.69k stars 1.06k forks source link

Unsafe.AsRef<ReadOnlyStruct>(in ReadOnlyStructInstance) missing in .NET 8 RC1, No documentation regarding the breaking change #35427

Closed Aniobodo closed 1 year ago

Aniobodo commented 1 year ago

Describe the bug

In .NET 7, there is the method ref T AsRef(in T source); in the Unsafe class. I use this method basically for large readonly structs. Now in .NET 8 RC1, that method is missing and instead there is ref T AsRef(scoped ref readonly T source); This new method in .NET 8 is empty and throws PlatformNotSupportedException exception.

In VS, the compiler displays Error CS1620 when the new method in .NET is used like this: Unsafe.AsRef(ref ReadOnlyStructInstance)

I did not find any information regarding this change, so:

  1. Is this an intentional change? If yes what are the reasons?
  2. What is the replacement for the missing method?

To Reproduce

Exceptions (if any)

Further technical details

VS 2022 Version 17.7.4

KalleOlaviNiemitalo commented 1 year ago

The API change from public static ref T AsRef<T>(in T source) to public static ref T AsRef<T>(ref readonly T source) was approved in https://github.com/dotnet/runtime/issues/85911#issuecomment-1624189321 and merged in https://github.com/dotnet/runtime/pull/89736 on 2023-08-02.

scoped had already been added between .NET 6.0 and .NET 7.0.

Is a PlatformNotSupportedException actually thrown at run time? Although the C# code includes throw new PlatformNotSupportedException();, the C++ code should replace the method body with a different one so that the throw is never executed.

KalleOlaviNiemitalo commented 1 year ago

https://dotnet.microsoft.com/en-us/download/dotnet/8.0 says .NET SDK 8.0.100-rc.1 supports "Visual Studio 2022 (v17.8 latest Preview)".

https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/8.0/version-requirements however says "Versions 8.0.1xx of the .NET SDK require Visual Studio version 17.7 and MSBuild version 17.7."

Aniobodo commented 1 year ago

@KalleOlaviNiemitalo thanks for the citations. Now with VS 2022 17.8.0 Preview: I did not get an exception at runtime. Also, the compiler Error CS1620 does not show up. So I hope that the final release of .NET 8/VS 2022 17.8.0 will be okay.

baronfel commented 1 year ago

Going to close this as it's fixed when using a supported VS version for the SDK in use.