diff --git a/README.md b/README.md index 3e65b86..cfe473f 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ Here's an example of some of the new features that **PolySharp** can enable down - `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority)) - `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections)) - `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771)) +- `UnreachableException` (see [docs](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.unreachableexception)) To leverage them, make sure to bump your C# language version. You can do this by setting the `` MSBuild property in your project. For instance, by adding `13.0` (or your desired C# version) to the first `` of your .csproj file. For more info on this, [see here](https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb), but remember that you don't need to manually copy polyfills anymore: simply adding a reference to **PolySharp** will do this for you automatically. diff --git a/src/PolySharp.Package/README.md b/src/PolySharp.Package/README.md index 0cacf88..b140791 100644 --- a/src/PolySharp.Package/README.md +++ b/src/PolySharp.Package/README.md @@ -53,6 +53,7 @@ Here's an example of some of the new features that **PolySharp** can enable down - `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority)) - `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections)) - `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771)) +- `UnreachableException` (see [docs](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.unreachableexception)) To leverage them, make sure to bump your C# language version. You can do this by setting the `` MSBuild property in your project. For instance, by adding `13.0` (or your desired C# version) to the first `` of your .csproj file. For more info on this, [see here](https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb), but remember that you don't need to manually copy polyfills anymore: simply adding a reference to **PolySharp** will do this for you automatically. diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/System.Diagnostics.UnreachableException.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Diagnostics.UnreachableException.cs new file mode 100644 index 0000000..ef54263 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Diagnostics.UnreachableException.cs @@ -0,0 +1,43 @@ +// +#pragma warning disable +#nullable enable annotations + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics +{ + /// + /// Exception thrown when the program executes an instruction that was thought to be unreachable. + /// + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + internal sealed class UnreachableException : Exception + { + /// + /// Initializes a new instance of the class with the default error message. + /// + public UnreachableException() + : base("The program executed an instruction that was thought to be unreachable.") + { + } + + /// + /// Initializes a new instance of the class with a specified error message. + /// + /// The error message that explains the reason for the exception. + public UnreachableException(string? message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception. + public UnreachableException(string? message, Exception? innerException) + : base(message, innerException) + { + } + } +} \ No newline at end of file diff --git a/tests/PolySharp.Tests/LanguageFeatures.cs b/tests/PolySharp.Tests/LanguageFeatures.cs index d5df709..2ac6f7c 100644 --- a/tests/PolySharp.Tests/LanguageFeatures.cs +++ b/tests/PolySharp.Tests/LanguageFeatures.cs @@ -1,8 +1,10 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; using System.Runtime.Versioning; #if !NETSTANDARD2_1 using System.Threading.Tasks; @@ -38,6 +40,13 @@ public void Throws([DoesNotReturnIf(true)] bool value) { } + // UnreachableException + public object Unreachable() + { + ExceptionDispatchInfo.Capture(new Exception()).Throw(); + throw new UnreachableException(); + } + // MaybeNullAttribute [return: MaybeNull] public string ModifyValue()