Skip to content

Commit

Permalink
Add new System.Diagnostics.UnreachableException polyfill
Browse files Browse the repository at this point in the history
The System.Diagnostics.UnreachableException.cs file was not actually auto-generated (see Sergio0694#88).

Fixes Sergio0694#60
  • Loading branch information
0xced committed Nov 22, 2024
1 parent 02753db commit a2c4dd2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>13.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` 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.

Expand Down
1 change: 1 addition & 0 deletions src/PolySharp.Package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>13.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` 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.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// <auto-generated/>
#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
{
/// <summary>
/// Exception thrown when the program executes an instruction that was thought to be unreachable.
/// </summary>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class UnreachableException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="UnreachableException"/> class with the default error message.
/// </summary>
public UnreachableException()
: base("The program executed an instruction that was thought to be unreachable.")
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UnreachableException"/> class with a specified error message.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
public UnreachableException(string? message)
: base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="UnreachableException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
/// </summary>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception.</param>
public UnreachableException(string? message, Exception? innerException)
: base(message, innerException)
{
}
}
}
9 changes: 9 additions & 0 deletions tests/PolySharp.Tests/LanguageFeatures.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a2c4dd2

Please sign in to comment.