Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new System.Diagnostics.UnreachableException polyfill #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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